Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - An Overview Page - 2

Page 2 of 5
Author: Exforsys Inc.     Published on: 2nd Mar 2006    |   Last Updated on: 29th Jul 2011

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:

Ads

Sample Code
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. printf("Hello Worldn");
  6. return 0;
  7. }
Copyright exforsys.com


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):

Sample Code
  1. $ gcc hello.c
Copyright exforsys.com


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:

Sample Code
  1. $ ./a.out Hello World!
Copyright exforsys.com


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:

Sample Code
  1. int main( int argc, char *argv[] )
Copyright exforsys.com


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:

Sample Code
  1. void main(void)
  2. void main( int ac, char *av[] )
Copyright exforsys.com


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:

Sample Code
  1. sum = a+b;
Copyright exforsys.com


could have written like this:

Sample Code
  1. sum
  2. =
  3. a +
  4. b ;
Copyright exforsys.com


Ads

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.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments