Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
Now Reading
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Preprocessor programs, also known as the preprocessor derivatives, are statements used in C/C++ programming language that are processed before the preprocessor executes the compilation of the code. These directives usually start with a ‘#’ and are implemented to alter source codes before their compilation occurs.
Having understood what is preprocessor in C and C++, let’s look at the briefs of various preprocessor directives.
Here is the list of commonly used directives of C/C++ preprocessors.
#define: Helps define a macro
#undef: It undefines a macro
#include: It lets you include a file in the source code program
#ifdef: It is used to include a code section if a specific macro is defined by #define
#ifndef: It includes a code section if a specific macro is not defined by #define
#endif: It indicates the end of #endif
#if: It checks for the specified condition
#else: It executes the alternate code when #if fails
#error: It stops program compilation
#warning: It continues the compilation process by displaying warning messages in the console window.
#region and #endregion: They increase the readability of code sections using the expansion and collapse features.
Process flow of preprocessor directives
Step-1: First, a developer writes a C program, and the program checks for the availability of any preprocessor directives.
Step-2: If the preprocessor directives are available, the program will undergo the action event of the pre-processor, and the C compiler would generate the object code. Subsequently, the linker executes the code.
Step-3: If preprocessor directives are unavailable, the process flow reaches the compiler. After the linker executes the code, the compiler will generate the object code.
Here’s the list of 4 major types of preprocessor directives in C.
Let’s discuss them in detail.
The macros in C/C++ are parts of code that are assigned some name. Whenever the compiler encounters this name, it substitutes the name with the actual code piece. The ‘#define’ directive helps you to define a macro. You can’t redefine those macros in C++ programs.
Here’s the syntax of the macro:
#define token value |
A few examples of macros are __cplusplus, __DATE__, __TIME__, __FILE__, __LINE__, __STDC__, and __STDC_HOSTED__.
Let’s look at the following example to understand how the macro works as one of the C/C++ preprocessors.
#include <stdio.h> |
Output:
The minimum number between 5 and 10 is 5 |
In the above program, we define macro MIN(x, y), which accepts two arguments x and y. It implements the ternary operator to return the minimum of two values. In the main function, two variables, n1 and n2, are declared. The MIN macro is implemented to find the minimum of those two numbers and assign its value to min_numb. Lastly, the program displays the output.
The file inclusion preprocessor directives in C inform the compiler to include a file inside the source code program.
A user can include two types of files in a C/C++ program:
i. Standard header files
These files hold definitions of pre-defined functions such as printf(), scanf(), etc. Various functions are declared in various header files. For example, the #include preprocessor directive is useful to include the header files in a C/C++ program. The standard I/O functions are contained in the ‘iostream’ file.
Here’s the syntax of #include preprocessor directive:
#include <file_name> |
where file_name represents the name of the header file to be included in the program. The ‘<‘ and ‘>’ brackets inform the compiler to search for the file in this standard directory.
You can include an input-output stream header file with the following syntax.
#include <iostream> |
or
#include "iostream" |
Note: You can utilise any input output stream objects after including the header file <iostream> within your source code. This may include cin and cout.
Here’s an example program that demonstrates the use of standard header files:
Example:
#include <iostream> |
Output:
The sorted numbers in descending order are: 9 8 5 4 2 |
The above program includes three header files. It then sorts the input numbers in descending order and displays the output using cout.
ii. User-defined header files
A program becomes difficult to read if it becomes too large. In such cases, it is recommended to divide it into tinier files and include them whenever required. Such files are known as user-defined header files. You can include such files as:
#include "filename" |
where the double quotes ( ” ” ) inform the compiler to search for the corresponding header file in the source file’s directory.
You can use conditional compilation directives in C/C++ to compile a specific portion of the program or to pass over the compilation of a specific portion of the program, depending on certain conditions. Here’s the list of preprocessor directives that helps you to insert conditional code:
#if Directive
#ifdef Directive
#ifndef Directive
#elif Directive
#else Directive
#endif Directive
The following syntax demonstrates the use of some of the conditional compilation directives in C/C++:
Syntax:
#ifdef macro_name |
Here’s an example program that implements conditional compilation directives in C++.
#include <iostream> |
Output:
The mode is enabled |
The first step in the above program defines a macro MODE and assigns 1 to it. We use preprocessor directives in the main() function to conditionally compile various code blocks as per the value of MODE.
If MODE is declared as 1, the code block within #if MODE == 1 and #endif will be compiled. So, it prints the message "The mode is enabled" to the output console.
If MODE is defined with some other value or not being defined, then the code block written in the #else and #endif will be compiled. So, it prints the message "The mode is disabled".
Irrespective of the value of MODE, the line std::cout << "The program continues to execute" << std::endl; will be executed.
In addition to the above-discussed directives, two more directives exist, but they are not commonly used. They are:
#undef Directive
#pragma Directive
Let’s look at their details.
i. #undef Directive
It undefines an existing macro. Its syntax is:
#undef PI |
It undefines the macro PI.
ii. #pragma Directive
They are special-purpose directives that are used to turn on or off certain features. They offer some extra information to the supported compilers when compiling a C/C++ Program. They are compiler specific.
The following section discusses a few of the #pragma directives.
#pragma startup: It allows you to declare the functions that are required to run before the program startup. It means you can use them to declare functions before the control passes to the main() function.
#pragma exit: It allows you to declare the functions that are required to run immediately before the program exit. You can use them for function declaration before the control returns from the main() function.
Here’s an example program that demonstrates #pragma startup.
void func1(); |
Output:
Inside main() |
Note: The above program doesn’t work with GCC compilers.
As seen from the above program, #pragma startup func1 is used to declare void func1().
Usually, C/C++ preprocessors commands are executed as the foremost step in the compilation of a program. These preprocessors make your C/C++ program easy to develop, easy to read, and easy to modify. Moreover, they make your code more transportable between various machine architectures.
Tutorials serve as one of the most effective ways to get acquainted with various aspects of C and C++. Along with going through the tutorials, pursuing upGrad’s Full Stack Software Development Bootcamp enables career advancement in the cutting-edge tech industry. It encompasses in-demand topics imparted by industry experts to make you stand out in the industry. Moreover, the extraordinary aspects like industry projects, Cloud Labs, career guidance sessions, performance reports, etc., ensures upGrad elevates your career growth in STEM!
1. What is the usefulness of the #pragma warn directive in C?
The #pragma warn directive in C hides the warning message that is displayed during compilation. Using different #pragma commands, depending on the type of compiler you use, you can disable specific warning messages.
2. Where are preprocessor directives written in C/C++?
The preprocessor directives written in C/C++ are written in source code. We write preprocessor directives like macros definition and file inclusion outside the main() function at the top of a C/C++ program.
3. How is the bits/stdc++.h header file useful?
The bits/stdc++.h header file helps you to include all the standard library functions in C++. The syntax is #include <bits/stdc++.h>. You can make use of any standard library header files in your C program by incorporating this header file in your source code. This includes, <iostream>, <vector>, <cmath>, <algorithm>, etc.
4. What are the # and ## operators in C/C++/
The # and ## operators are preprocessor operators in C/C++. The # operator encloses the appropriate arguments passed in the corresponding macros in double quotation marks. Also, it transforms the input it precedes into a quoted string. The ## operator concatenates/merges the passed arguments or tokens to generate a new variable/token.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...