Technical Training
C TutorialsTable of Contents
C Programming - An Overview
C Programming - An Overview - Page 2
C Programming - An Overview - Page 3
C Programming - An Overview - Page 4
C Programming - An Overview - Page 5C Programming - An Overview Page - 2
C Programming - An Overview
Hello World
Let us look at a very basic C program. We will write the canonical "Hello World" program in a file called hello.c. Here are the contents of file hello.c:
- #include <stdio.h>
- int main(void)
- {
- printf("Hello Worldn");
- return 0;
- }
I have added line numbers here to make it easier to refer to specific lines; the actual C source code does not have line numbers. We can run the C compiler on this program (the $ is the command prompt, what I type is in red):
- $ gcc hello.c
The compiler does not give any errors and has created an executable file called "a.out". As mentioned, how you run the compiler on your system and what executable file it creates depends on your system and compiler or IDE. We run the program and get the output:
- $ ./a.out Hello World!
Let us take a look at this example in detail, starting with how the file is compiled.
A C compiler translates source files into machine instructions and then links them with any libraries needed to run the program, creating an executable file. When a compiler processes a source file, one of the first things it does is carry out the preprocessor directives. These are various commands that control how the compiler processes the source file.
All preprocessor commands start with the hash (#) mark. We will take a more detailed look at preprocessor commands in another tutorial, but for now we will talk about the include directive seen on the first line of hello.c. This directive gives the name of another source file to the compiler to include in this file. The file name is surrounded by double quotes or angle brackets.
When the compiler sees this command it switches processing to the named file, then back to the original file. It is like saying to the compiler "include the contents of the file stdio.h here". Typically the include directives are put near the top of a source file and are used to include header files.
The stdio.h file is a standard header file included with all C compilers. We will talk about header files more a little later.
C is a procedural language. A procedural language can break a program up into several procedures (also called subroutines), and each procedure can issue commands and invoke other procedures. Though C's procedures are called "functions", that does not make C a "functional" programming language – that term is used for another type of programming paradigm. In this tutorial we will use C's terminology and call procedures "functions".
A C program should have one function called "main". When the program is run this is the function that gets executed first. On line 3 of hello.c we have our main function. C functions can take parameters (also called arguments) and return values, similar to functions in math. On line 3 the int signifies that the main function returns an integer value. The void indicates that main does not take any parameters.
There are two classic ways to declare the main function. One is as seen on line 3, the other is like this:
- int main( int argc, char *argv[] )
This form is used when you wish to pass parameters to the main function. In addition to these, a C compiler may allow other forms of main(). A couple of common ones are:
- void main(void)
- void main( int ac, char *av[] )
These are the same as the two main() declarations seen before except they return a void – that is, they do not return any value.
The body of a function is placed between curly braces { } like on lines 4 and 7 in hello.c. In this program the body of the main() function only calls function printf() to print the string "Hello World!" and a new line (the n at the end of the string) to the output screen on line 5, and returns the value 0 on line 6. printf() is a standard function used to write formatted output to the screen or whatever output device you have. This function is in the standard libraries that the C compiler links with programs. We will talk more about the printf() function below.
In C, every statement is terminated by a semicolon (;) character. A compound statement (also called a block) is a sequence of statements inside curly braces { }. As you have seen, the body of a function is a compound statement. In addition, most places that accept a single statement can also accept a compound statement, such as after the if, while, case statements.
In a C source file, by convention the statements in between curly braces are indented by a certain amount of space. You can see that lines 5 and 6 of hello.c above have been indented with a tab. This is only to make the code easier to read for people, it is not required by the compiler. The compiler ignores white-space (spaces, tabs, new lines) unless it occurs in a character or string. For example the statement:
- sum = a+b;
could have written like this:
- sum
- =
- a +
- b ;
The amount to indent statements inside a block is up to you; some people indent a tab stop, others indent 2 or 4 spaces, it is just whatever you prefer.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







