Skip to main content

Structure

Introductionโ€‹

The structure of C programs consists of the header files, source files, main function, and comments. Along with these, you have functions, variables, statements, and expressions.

Compileโ€‹

C compiles instructions. This means - C takes instructions from the programmer and converts them to machine readable language to help the computer understand and follow the instructions. Therefore, to run a C program, it needs to be compiled first.

Data typeโ€‹

The data types are declared as variables or functions of different types. They have a specific associated memory and specific operations that are performed. These are the common data types:

  • int - The value ranges from โˆ’32,767 to +32,767
  • char - Any letter from A to Z or a to z
  • float - single precision floating point number
  • double - double precision floating point number

Header fileโ€‹

Header files are files with .h extension. These contain the declaration of functions that are used in the program. For example, including the <stdio.h> allows you to use the printf() function. The header files define data types, function prototypes, and C pre-processor directives. You can also have custom header files with .h extensions to define the functions that can be directly used in the application.

Source fileโ€‹

The file that includes the code and uses the header files is a source file with extension .c. By including the header file, the source file directs the C compiler to process the header file before compilation. The source file also includes the data type, variables, functions and all the code required for functional application.

Main functionโ€‹

The main() function starts the execution of the program and the entry point of the program. Once you compile and run the program, the execution control enters the main().

  • The void defines the return type of a function. It is nothing and states that the main() does not return any value after executing your code.
  • C executes the main() function first.
int main() {
...
...
return 0;
}

Return statementโ€‹

A return statement stops execution of the function and gives back control to the calling function. In the above example, the calling function is main().

This returns an integer:

return 0;

Commentโ€‹

Comments in C language allow you to describe the code or the logic written for the program. You can have single or multiple lines of comments.

A single line comment can be declared using //

// This is a comment

A multi line comment can be declared using /* */

/*
First line
Second line
third line
...
...
...
*/

Compiling a C programโ€‹

We need compilers to compile a C program. Famous C compilers include, gcc and clang.

In a Unix system, to compile a .c file:

user@theprogrammingfoundation:~$ gcc your_program.c

Once compiled, you can see the binary file by typing ls:

user@theprogrammingfoundation:~$ ls
Desktop Documents Downloads Pictures Music Public Videos a.out

By default the compiled binary file is called, a.out. To run the binary file:

user@theprogrammingfoundation:~$ ./a.out
Hello Earth