Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Similar documents
Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Computers and Computation. The Modern Computer. The Operating System. The Operating System

EL2310 Scientific Programming

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Problem Set 1: Unix Commands 1

CS354 gdb Tutorial Written by Chris Feilbach

CSE 374 Programming Concepts & Tools

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Variables and literals

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

PRINCIPLES OF OPERATING SYSTEMS

Getting started with UNIX/Linux for G51PRG and G51CSA

Lab 1: First Steps in C++ - Eclipse

EL2310 Scientific Programming

Intermediate Programming, Spring 2017*

CSE 303: Concepts and Tools for Software Development

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

COMP s1 Lecture 1

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Chapter 11 Introduction to Programming in C

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

PROGRAMMAZIONE I A.A. 2017/2018

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Intermediate Programming, Spring Misha Kazhdan

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Eclipse CDT Tutorial. Eclipse CDT Homepage: Tutorial written by: James D Aniello

CSC258: Computer Organization. Functions and the Compiler Tool Chain

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

7 Cmicro Targeting. Tutorial. Chapter

Programming in C. Pointers and Arrays

Reviewing gcc, make, gdb, and Linux Editors 1

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS3157: Advanced Programming. Outline

CSE 351. Introduction & Course Tools

QUIZ. What is wrong with this code that uses default arguments?

IS12 - Introduction to Programming. Lecture 7: Introduction to C

CS240: Programming in C

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

assembler Machine Code Object Files linker Executable File

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

An overview of Java, Data types and variables

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Fundamentals of Programming Session 4

Chapter 2, Part I Introduction to C Programming

Program Organization and Comments

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Starting to Program in C++ (Basics & I/O)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

C / C++ Coding Rules

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

G52CPP C++ Programming Lecture 9

Chapter 11 Introduction to Programming in C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Maemo Diablo GNU Make and makefiles Training Material

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Programming I Laboratory - lesson 01

Topic 6: A Quick Intro To C

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Final CSE 131B Spring 2004

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

CSE 333 Lecture 6 - data structures

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Programming in C. What is C?... What is C?

CS240: Programming in C. Lecture 2: Overview

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Lecture 2, September 4

Week 1, continued. This is CS50. Harvard University. Fall Cheng Gong

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

CSE 303 Lecture 8. Intro to C programming

Separate Compilation Model

Programming the DMCC in C

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Working with Shell Scripting. Daniel Balagué

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Lecture 03 Bits, Bytes and Data Types

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Transcription:

Hello, World! in C Johann Myrkraverk Oskarsson October 23, 2018 Contents 1 The Quintessential Example Program 1 I Printing Text 2 II The Main Function 3 III The Header Files 4 IV Compiling and Running the Program 5 1 The Quintessential Example Program This is Hello, World! An example program for testing the compiler and run time system of the C programming language. It is often the first program people try out. It s main purpose is to teach people how to use the compiler and linker (often the same program) of their choice. This is in order to get people familiar with the tools of the trade. Many newcomers don t differentiate between their text editor, compiler and the programming language when they start with an integrated development environment or IDE. The programming language is the text written on screen, or paper for that matter. And this is what the compiler will read in order to create the executable program. The environment used to create this text is immaterial to the programming language and can be switched at will. Here, the focus is on what each part of the program does, why it is the way it is, and what every part means. 1

Part I Printing Text 2 The first thing we cover here, is how C prints text to standard output. Standard output is an operating system concept, and refers to the usual text output device of programs. Because people are used to graphical user interfaces, there is often no visible place for this. On UNIX, OS X and OpenVMS, this habitually refers to the terminal being used to run the application. On Windows and OS/2 1, this is the command prompt, typically run with CMD. The C function for printing out text is called printf ( ). The f stands for format and the first argument is a format string something we don t make use of here we just print out the format string as is. At the end of the format string we have "\n" which tells the compiler to include a literal newline into the string. This makes our text, when output by the resulting program, advance the cursor to the next line. print hello, world 2 printf ("Hello, world!\n"); This code is cited in chunk 3. This code is used in chunk 3. 1 Available as ArcaOS at the time of writing 2

Part II The Main Function 3 Every C program has a main ( ) function which is the starting point of the program. What that means is when the program starts, it executes the main ( ) function and stops when it reaches the end if. The return value is then returned to the operating system. What exactly that means depends on the operating system. In UNIX and OS X terminals this is the $? shell variable, and in Windows and OS/2, this is the %ERRORLEVEL%. First we include the header files we need; more on that in the next section. Then we start the main function. The standard way of defining it is as follows, it starts with the int return type, then comes the main which is the name of the function, then the parameters follow, the first one is of type int and habitually called argc. The unused is a non-standord way to declare to the compiler we re not using that variable. The second variable is an array of pointers to char or an array of strings as C programmers may think of it. The denotes a pointer, and the [ ] denotes an array. This parameter is habitually called argv. We use unused for both arguments because modern compilers will warn about unused arguments. For the main ( ) function, the arguments are part of the runtime environment and we really should include them as is even though we don t use them. There are other and more portable tricks to achieve the same thing, but we don t cover them here. The code that the function executes is included between the open brace, {, and close brace, }. The first thing we do is to print hello, world 2 and then we end the function with return 0; which tells the operating system we have exited successfully. 2 include header files 4 int main (int unused argc, char unused argv [ ]) { print hello, world 2 } return 0; 2 It s actually system dependent what successfully means here, but most operating systems today use the UNIX convention of zero meaning success. 3

Part III The Header Files 4 There is only one header file we need for this program, it s named stdio.h and includes the declaration 3 of the printf ( ) function. On OS X, this header file will also include a macro definition of unused so we don t need to do anything about it. include header files 4 #include <stdio.h> This code is used in chunk 3. 3 We refer to any introductory text on the C programming language for the precise meaning of declaration 4

Part IV Compiling and Running the Program Clang and GCC. These compilers work on UNIX and compatible systems, including OS X where these examples are taken from. Both compilers support the -Wall and -Wextra command line flags, so we use them. On OS X, Clang masquerades as GCC so we only include one command line example here. gcc Wall Wextra o h e l l o h e l l o. c If your header files do not define the D unused= a t t r i b u t e ( ( unused ) ) on the command line. We can now run the executable as follows.. / h e l l o unused macro, you can put OpenWatcom. This compiler works on DOS, Linux, OS/2, and Windows. The example below was tested on OS/2; we expect the other system to behave very similarly. The OpenWatcom compiler doesn t understand unused so we define it to be nothing on the command line, and invoke the compile and link utility as such. wcl386 d unused= h e l l o. c Which gives us a hello.exe file we can run as such. h e l l o Visual Studio. In order to compile with Visual Studio on the command line, it s best to open up a special command prompt such as the x64 Native Tools Command Prompt for VS 2017. This can be done from the Windows menu or the search feature. The Visual Studio compiler doesn t understand unused either, so we define it to the empty string; and invoke the compiler as such. c l / D unused= h e l l o. c Which gives us a hello.exe file we can run as such. h e l l o 5

OpenVMS. VSI C does not understand and invoke the compiler as such. cc / d e f i n e= unused= h e l l o And then we link the program with, l i n k h e l l o and finally to run the program we do: run h e l l o unused either, so we define it to nothing Output. What we should now have on our screen is what follows. Hello, world! 6

Index unused : 3, 4, 5. argc: 3. argv : 3. main : 3. printf : 2, 4. 7

List of Refinements include header files 4 Used in chunk 3. print hello, world 2 Cited in chunk 3. Used in chunk 3. 8