The only way to learn a programming language is to write programs in it, so we will begin with the anatomy of your first simple C program: "Hello, world". This program simply prints the words "Hello, world" to the screen.

Note that this topic aims to teach you the features and syntax of the C language, not how to use it on any given computing system. C is a compiled language, rather than an interpreted one (like Python). This means that you can"t just type lines of code into the computer and have it execute them. Instead, you must create a file containing C code and have a "compiler" program create an executable from the code. The later section Exercise: Compile and Debug touches on this process. Note also that the line numbers shown in some code samples (including the next one) are provided for reference only. A valid C source code file cannot include them.

#include <stdio.h>	

/* 
      my first 
      C program 
      that prints "Hello, world"
*/

main()
{
	printf("Hello, world\n");
}

Line 1: Demonstrates the use of the #include directive which enables us to include external libraries or header files (*.h) in our programs. These external libraries define useful functions and variables which we might use in our program. In this program we include the "standard input/output" [stdio.h] header file. This header file contains many of the functions which are used to print to the screen or grab input from the user; it defines the function "printf" which we will use in the main program.

Line 3: This is an example of the start of a multi-line comment (also called a "block" comment). Comments are used to briefly explain what the program does, or to keep notes for the programmer. A block comment starts with "/*" and is terminated with "*/". Any characters between /* and */ are ignored by the compiler. A single-line comment starts with "//" (two forward slashes) -- everything after the "//" is ignored by the compiler until the end of the line. This is sometimes called a C++-style comment, but most all C compilers support these now.

Line 7: Ends the comment block started on Line 03.

Line 9: Here we define a function called "main". Section 6 contains a concise description of functions; at the most basic level functions are collections of statements. This collection of statements expects no arguments, which is indicated by the empty list ( ).

Line 10: The statements of a function are enclosed in braces { }, this begins the statements for the function "main".

Line 11: This calls the function "printf" and passes the argument "Hello, world\n". Printf is a function defined in the library stdio.h which we added to our code using the #include directive described in Line 01. This function prints output to the screen, in this case the string of characters between the quotes. "\n" represents the newline character. Printf can take multiple parameters, including a formatting string, which are described in I/O: printf.

Line 12: Ends the function "main".

It is interesting to note that C does not care about spaces, the same program could have been written as:

#include <stdio.h>
main
(
)
{
printf
(
"Hello, world\n"
);
}

While C does not care about spaces, humans have a tough time reading text that is unformatted. For this reason you should format your code to make it readable to yourself and other users.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement