Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
    • CS & IT Engg.
      • C programming Tests
      • C++ programming Tests
      • Java Programming Tests
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
    • About us
    • Privacy Policy
  • Cart
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
    • CS & IT Engg.
      • C programming Tests
      • C++ programming Tests
      • Java Programming Tests
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
    • About us
    • Privacy Policy
  • Cart
  • User Login

Preprocessor Directives tutorial

Preprocessor Directives in C

Learn C Programming Preprocessor Directives MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests. A Preprocessor in a C programming language is a Program that processes source code of a C program, expanding Macro Definitions, File Inclusions and more.

Only through Preprocessor directives, you are able to include library files in your C program. Preprocessing in C happens before compilation. Part of the Source code is replaced with Preprocessing expressions where ever encountered. So the size of Total code increases before passing to the Compiler.

Preprocessor Directives

All preprocessor directives start with a Pound Symbol #. There are 3 types of Preprocessor Directives

  1. Macro Expansion
  2. File Inclusion
  3. Conditional Compilation and Run

1. Macro Expansion Preprocessor Directives

Macro is like a function or shortcut that can be used to achieve code reuse. Syntax of defining a macro is as follows.

#define NAME EXPRESSION

Example 1:

#define AREA(x) x*x
int main()
{
  int side=20;
  int area = AREA(side);
  printf("AREA=%d", area);

  return 0;
}
// AREA(side) replaced with side * side

Example 2: Proper use of Parantheses avoids unexpected results.

#define AREA(x) x*x
int main()
{
  int side=2;
  float total = 20 / AREA(side);
  printf("AREA=%d", area);

  return 0;
}
// AREA(side) replaced with side * side
// 20 / side * side
// (20/side) * side
// 10 * 2
// 20
//What you expected ???
// 20 / (2*2)
// 5
// Correction: ???
//#define AREA(x) (x*x)

2. File Inclusion C Programming Preprocessor Directives

As the name suggests, using a C Precessor directives of File Inclusion type, you are including either Library header files or User defined files. We usually include STDIO.H in our C program to use Printf and Scanf functions.

Note: Preprocessor copies all code of the included file into our C program and sends the combined single copy of code to the compiler.

C editor like TURBO C allows defining list of directories that can be searched for files in a later time. These directories fall into two types.

  1. Current Directory
  2. Extra Specified Directories

Syntax:

#include "filename"
(or)
#include <filename>

There are two ways of including a file using preprocessor directive #include.

1. #include “filename”

This notation searches file in the CURRENT directory and Extra Specified Directory list.

2. #include <filename>

This notation searches files in only CURRENT directory. If the file is not available no error is raised.

Example: Including a header file

#include<math.h>
int main()
{
  float a = 10.3f;
  float b = sin(a);
  printf("SIN(%f)=%f", a, b);
  return 9;
}

3. Conditional Compilation Preprocessor Directives

Unlike Macro Expansion and File Inclusion preprocessor directives, these Conditional Compilation preprocessor directives are written and used inside our C Program itself.

Condition check is done using IF, ELSE and ELSE IF statements. For Conditional Compilation, directives like #ifdef, #endif, #else, #elif and #ifndef are defined is C language. These help in implementing multiple source codes and version control management in Hardware and Software environments.

The conditional directives check for EXISTENCE of a MACRO and compiles code accordingly. Find examples below for good understanding.

define AREA printf("SUCCESS");
int main()
{
  #ifdef AREA
    printf("EUROPE");
  #else
    printf("AMAZON");
  #endif
  return 9;
}

In the above example, if the MACRO AREA exists, then EUROPE is printed. Otherwise, AMAZON is printed.

#IFNDEF is the reverse of #IFDEF. IFNDEF works if the MACRO does not exist.

#UNDEF Preprocessor Directive

#undef is a special preprocessor directive in C language to Nullify or Remove Definition of existing Macros.

#define AREA printf("SUCCESS");
int main()
{
  #undef AREA

  #ifdef AREA
    printf("EUROPE");
  #else
    printf("AMAZON");
  #endif
  return 9;
}
//OUTPUT
//AMAZON
//Macro AREA is undefined. So #ifdef fails.

[WpProQuiz 42]


For More Free online Quizzes of C Programming Click here


For More Free online Test of Aptitude Click here

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

About us

Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Select Language

Follow us

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.