Write Your First Program in C

C How to Program #3

Write your first program in C

// function main begins program execution
int main( void )
{
    printf( "Hello World!\n" );
} // end function main

#include Preprocessor Directive

Lines beginning with # are processed by the preprocessor before compilation.

#include <stdio.h> is a directive to the C preprocessor.
#include <stdio.h> tells the preprocessor to include the contents of the standard input/output header in the program.

The main Function

int main( void )
The parentheses after main indicate that main is a program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main.
Functions can return information. The keyword int to the left of main indicates that main “returns” an integer (whole-number) value.
Functions also can receive information when they’re called upon to execute. The void in parentheses here means that main does not receive any information.
A left brace, {, begins the body of every function. A corresponding right brace, }, ends each function.
This pair of braces and the portion of the program between the braces is called a block. The block is an important program unit in C.

An Output Statement

printf( "Welcome to C!\n" );
Instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. A string is sometimes called a character string, a message or a literal.
The entire line, including the printf function (the “f” stands for “formatted”), its argument within the parentheses and the semicolon (;), is called a statement. Every statement must end with a semicolon (also known as the statement terminator).

Escape Sequences

Notice that the characters \n will not printed on the screen. The backslash (\) as used here printf( "Welcome to C!\n" ); is called an escape character.
It indicates that printf is supposed to do something out of the ordinary.
When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence.

Escape SequenceDescription
\nNewline. Position the cursor at the beginning of the next line.
\tHorizontal tab. Move the cursor to the next tab stop
\aAlert. Produces a sound or visible alert without changing the current cursor position.
\\Backslash. Insert a backslash character in a string.
\"Double quote. Insert a double-quote character in a string.

The Linker and Executables

Standard library functions like printf and scanf are not part of the C programming language. For example, the compiler cannot find a spelling error in printf or scanf. When the compiler compiles a printf statement, it merely provides space in the object program for a “call” to the library function. But the compiler does not know where the library functions are— the linker does.

When the linker runs, it locates the library functions and inserts the proper calls to these library functions in the object program. Now the object program is complete and ready to be executed. For this reason, the linked program is called an executable.

If the function name is misspelled, the linker will spot the error, because it will not be able to match the name in the C program with the name of any known function in the libraries.