Category Archives: C Preprocessor

C Preprocessors

Preprocessor Directives extend the power of the C programming language. C Preprocessors Directives is a special set of instructions in the program which is processed before handing over the program to the compiler. These instructions are always preceded by a pound sign (#) and NOT terminated by a semicolon. The preprocessor directives are executed before the actual compilation of code begins. Different preprocessor directives (commands) tell the preprocessor to perform different actions. We can categorize the Preprocessor Directives as follows:

  • File inclusion directives
  • Macro substitution directives
  • Conditional compilation directives

Advantages:

  • It makes program development easy
  • It enhances the readability of the program
  • It makes modification easy
  • It increases the transportability of the program between different machine architectures.

File inclusion directives

The file inclusion directive is used to include files into the current file. When c preprocessors encounters an #include directive it replaces it with the entire content of the specified file. The file inclusion directive (#include) can be used in the following two ways:

  • #include “file-name”
  • #include <file-name>

We can see that #include can be used in two ways angular brackets (<>)and inverted commas (“”). The only difference between both expressions is the location (directories) where the compiler is going to look for the file. In the first case where the file name is specified between double-quotes, the compiler will look for the in the current directory that includes the file containing the directive. In case it is not there the compiler will look at the file in the default including directories where it is configured to look for the standard header files. When #include is written with <> it means the file is searched directly where the compiler is configured to look for the standard header files. Therefore, standard header files are usually included in angle brackets, while other specific header files are included using quotes.

Take a look at the below example program. Save the following code in a file called main.c:

Then save the code below in a file called head. hand places it in the same directory as the main.c:

The variable is declared in the head.h file will be available in main.c, now we can use the variable in main().

 

Macro substitution directives

Macro substitution directives are used to define an identifier that is being replaced by a pre-defined string in the program. Macro substitution is a process in which the preprocessor replaces identifiers with one, or more program statements (like functions) and they are expanded inline.

There are two directives for Macro Definition:

  • #define – Used to define a macro
  • #undef – Used to undefine a macro

#define c preprocessors

To define preprocessor macros we can use #define.

Syntax:

Example:

The token string in above line 3.14 is replaced in every occurrence of symbolic constant PI.

C Program to find the area of a circle.

Output:

Conditional compilation directives(#ifdef, #ifndef, #if, #endif, #else and #elif)

Conditional Compilation Directives allows you to include or exclude certain part of code only when certain condition are met. These macros are evaluated on compile time.

The following directives are included in this category:

  • #if
  • #elif
  • #endif
  • #ifdef
  • #ifndef

Line control (#line) directive:

The #line directive gives us a way to control or set the value of the __LINE__ and __FILE__ macros. (Note: the filename is optional.) The __LINE__ and __FILE__ macros represent the current line being read and the current file being read.

Syntax:

Error directive (#error)

Error directive allows to aborts the compilation process when it is found, ant reports a fatal error that can be specified as its parameter.

Pragma directive (#pragma)

The #pragma directive is a compiler-specific directive used for the suppression of specific error messages.

Predefined Macros
__LINE__

It represents the line number of the current source file being compiled.

__FILE__

It represents the name of the source file being compiled.

__DATE__

It returns the date, in the form “Mmm dd yyyy”, when the compilation process began.

__TIME__

A string literal in the form “hh:mm: ss” containing the time at which the compilation process began.

__STDC__

Forces the use of standard C/CPP codes only in the program.

 

C Header Files

In C Programming Language Header Files gives us a way to reuse the external declarations, functions, or macro definition and allows us to share between other source files. When you include a header file, the compiler adds the functions, data types, and other information in the header file to the list of reserved words and commands in the language. We can include a header file in our program using C Preprocessor Directive “#include”, Header files almost always have a .h extension. Including a header file produces the same result as you copy the source code in your program.

In C Programming Language Headers files are used for the following purpose –

  • It enables you to include system or operating system-related macro definitions and functions so that you can invoke system calls and libraries.
  • You can create your own custom header files so that you can reuse and share them.
  • It makes the program more manageable.
  • It increases the portability of the program.

One of the most commonly used header files is for the standard input/output routines is called stdio. h.

We can include header files in the following two ways –

includes a header file from the current directory (the directory in which your C source code file appears), and

includes a file from a system directory.

Type Of Header Files

Standard Header Files

It is the set of header files used for performing various common and standard operations.

stdio. h – Defines core input and output functions

string.h – Defines string handling functions.

time.h – Defines date and time handling functions

math.h – Defines common mathematical functions.

User-Defined Header Files

In C Programming Language users can have their own custom header file that provides additional capabilities.

How to Create Your Own Header File in C Programming Language

Step #1

Type the below source code in a file

Put only function definition as you write in General C Program

Step #2

  • Save Above Code with [.h ] Extension.
  • Let the name of our header file be my head [ my head.h ]
  • Compile Code if required.

Step #3

Write Main Program

  • Include Our New Header File.
  • Instead of writing < my head.h> use this terminology “my head.h”
  • All the Functions are defined in the head.h header files are now ready for use.
  • Directly call function to add(); [ Provide proper parameter and take care of return type ]

While running your program both files (my head. h and sample. c) should be in the same directory.