Understanding Hello World

1. #include <stdio.h>
2.
3. int main(void)
4. {
5.     int n;
6.     printf("Hello world! Give me a number:\n");
7.	scanf("%d", &n);
8.	printf("Thanks! I've always been fond of %d\n", n);
9.     return 0;
10 }
  1. The standard input/output library is included. Later on in the course, you'll learn more about what happens when a program is compiled. The meaning of the include statement will be explained in more detail then. For now, you should just remember that whenever you want to use printf or scanf in your programs, you need to include this library.
  2. Your program would work without this space here. In general, you should add extra lines and spaces to make your programs more readable.
  3. This is where the execution of the program begins. Main is a special kind of function in C. You will learn about functions soon. You may have noticed some variations of this line like: void main(), main(), or int main(). With some compilers, you will get a warning about some of these forms. For now, don't worry about that or even about the difference between these different forms. The only thing that's important for you to know right now is that this is where the program starts. When you want to read through code to trace its execution, this is where you should start.
  4. This "curly bracket" indicates the start of the main function. All the code between this curly bracket and the one on line 10 is part of the main function. Curly brackets are used to group together "blocks" of code.
  5. This is the declaration of the variable named "n" which is a variable of type int. It does not have a value right now. (Some compilers will give it the value 0, by default, but you shouldn't rely on this.) A variable must always be initialized before it is used. There is no special reason why this variable was named 'n'. You could change it to an other legal variable name. If you changed it, you would also have to change the n after the & on line 7 and the last n in statement 8.
  6. This line prints a single line of text on the screen. The backslash character is a special character used within print statements. It 'escapes' the character which follows it. What this means is that '\n' is not printed. Instead, a carriage return is printed (the next text printed will appear on the next line.) 'n' is the code used for carriage returns. Other characters are used for other 'invisibles' like the 'tab' character.
  7. This line reads a number from the keyboard. The '%d' indicates the type of input that is expected; in this case, an integer is expected. Like backslash, the percent sign is a character that, when used with a printf or scanf statement, has special meaning. The ampersand preceding the n also has special meaning. However, we won't learn about 'pointers' until later in the course. For now, you should just know that this line has the effect of giving the variable n (that was declared in line 5) the value of whatever number is typed by the user. n has now been initialized.
  8. Finally, the number is printed. We see the '%d' here again. After the closing quotes of the text to be printed, the integer value that should be printed in place of the %d appears. The %d acts as a place holder for an integer value.