Home > Tutorials > C Tutorials > Programming in C: Directives and Functions

Programming in C: Directives and Functions

When you write a C program in an editor (such as vi on Unix or Notepad on Windows), you are creating a source code. Every C source code has these parts:

  1. preprocessor directives
  2. main function
  3. body functions

Let’s learn what each of these means and how they work.

Preprocessor Directives

preprocessor directive tells the compiler to add (include) a header file before compiling the source code. A header file contains information about how the compiler should interpret the functions written in the program. Here is a simple C program that begins with a preprocessor directive:

#include <stdio.h>
void main( )
{
printf(“We are learning C today.”);
 

 

This preprocessor directive #include <stdio.h> tells the compiler to look in the stdio.h file for instructions on how the printf function works. The header files are stored in the C library, which comes with your compiler.

Main Function

The computer will begin executing your program from the main function. For simple programs which do not return any value, we use void main ( ), like this:


#include <stdio.h>
void main( )
{
printf(“We are learning C today.”);
}

Warning: Different compilers handle the main function differently.

For example, if you are using the gcc compiler on Unix, simply type main( ) without using void. Check your compiler’s notes for further instructions.

 

Body Functions

Inside the main function, we write the functions that will DO something. These functions are called body functions because they are enclosed in curly braces: { } inside the body of the main function. One popular function is printf which tells the computer to print a line of text on the computer screen:

#include <stdio.h>
void main( )
{
printf(“We are learning C today.”);
} 
 

 

Notice that the text is enclosed in parentheses and double-quotes: (” “). This tells the computer the difference between text and functions. Notice also that the line ends in a semicolon: ; which tells the computer to stop processing this line.

Open your plain text editor and type this source code:

#include &lt;stdio.h&gt;
void main( )
{
printf("We are learning C today.");
}

 

 

Save your file as first.c and then run it through the compiler. Then link your object file to the C libraries. Then run your executable file. You should see this appear on your screen:

We are learning C today.

Did it work? If you got error messages during the compilation, go back into your editor and fix your mistakes. If you got other error messages or the program did not run, then you probably made a mistake when installing the compiler. Go back and try to figure out what went wrong. If you can’t find the problem, then delete your compiler and re-install.

Continue on to Escape Characters & Tabs.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...