C Concepts
C Concepts
Object-like Macros
Here, PI is the macro name which will be replaced by the value 3.14.
Function-like Macros
The function-like macro looks like function call. For example:
1. #define MIN(a,b) ((a)<(b)?(a):(b))
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.
Output:
File :simple.c
Date :Dec 6 2015
Time :12:28:46
Line :6
STDC :1
C Preprocessor Directives
The C preprocessor is a micro processor that is used by compiler to transform your code before
compilation.
It is called micro preprocessor because it allows us to add macros.
o #undef
o #ifdef
o #ifndef
o #if
o #else
o #elif
o #endif
o #error
o #pragma
C #include
1.> The #include preprocessor directive is used to paste code of given file (filename) into current file
(where we are writing program) .
2.> It is used include system-defined and user-defined header files.
3.> If included file is not found, compiler renders error.
By the use of #include directive, we provide information to the preprocessor where to look for the
header files. There are two variants to use #include directive.
1. #include <filename>
2. #include "filename"
The #include <filename> tells the compiler to look for the directory where system header files are held.
The #include "filename" tells the compiler to look in the current directory from where program is
running.
#include notes:
Note 1: In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is
treated as filename.
Note 2: In #include directive, backslash is considered as normal text not escape sequence. So in case of
#include <a\nb>, a\nb is treated as filename.
Note 3: You can use only comment after filename otherwise it will give error.
C #ifdef
1.) The #ifdef preprocessor directive checks if macro is defined by #define.
2.) If yes, it executes the code
3.) otherwise #else code is executed, if present.
#include <stdio.h> #include <stdio.h> Syntax:
void main() #ifdef MACRO
#define NOINPUT { //code
void main() int a=0; #endif
{ #ifdef NOINPUT
int a=0; a=2; Syntax with #else:
#ifdef NOINPUT #else #ifdef MACRO
a=2; printf("Enter a:"); //successful code
#else scanf("%d", &a); #else
printf("Enter a:"); #endif //unsuccessfull code
scanf("%d", &a); printf("Value of a: %d\n", a); #endif
#endif }
printf("Value of a:
%d\n", a);
}
Output: 1.) if macro NOINPUT is not
Value of a: 2 defined…you will be prompted
to enter value of a
2) Output:
Enter a: 5
Value of a: 5
C #ifndef
1.> The #ifndef preprocessor directive checks if macro is not defined by #define.
2.> If yes, it executes the code
3.> otherwise #else code is executed, if present.
Syntax:
1. #if expression
2. //code
3. #endif
C #if example
Let's see a simple example to use #if preprocessor directive.
1. #include <stdio.h>
2. #include <conio.h>
3. #define NUMBER 0
4. void main()
5. {
6. #if (NUMBER==0)
7. printf("Value of Number is: %d",NUMBER);
8. #endif
9. getch();
10. }
Output:
Value of Number is: 0
C #undef
The #undef preprocessor directive is used to undefine the constant or macro defined by #define.
Syntax:
1. #undef MACRO_NAME
The #undef directive is used to define the preprocessor constant to a limited scope so that you can
declare constant again.
Let's see an example where we are defining and undefining number variable. But before being
undefined, it was used by square variable.
1. #include <stdio.h>
2. #define number 15
3. int square=number*number;
4. #undef number
5. Int main()
6. {
7. printf("%d",square);
8. Printf("%d",number);//Compile Time Error : Pi undeclared
9. }
Output:
225
1) When we use include directive, the contents of included header file (after preprocessing) are copied
to the current file.
Angular brackets < and > instruct the preprocessor to look in the standard folder where all header
files are held.
Double quotes “ and “ instruct the preprocessor to look into the current folder (current directory).
3) The macros can take function like arguments, the arguments are not checked for data type.
For example, the following macro INCREMENT(x) can be used for x of any data type.
#include <stdio.h>
#define INCREMENT(x) ++x
int main()
{
char *ptr = "GeeksQuiz";
int x = 10;
float y=10.0f;
double z=10.0;
printf("%s\n ",INCREMENT(ptr));
printf("%d\n",INCREMENT(x));
printf("%f\n",INCREMENT(y));
printf("%f\n",INCREMENT(z));
return 0;
}
Output:
eeksQuiz
11
11.000000
11.000000
Output: Output:
16 40
#include <stdio.h>
#define merge(a, b) a##b
int main()
{
printf("%d ", merge(12, 34));
}
Output:
1234
6) A token passed to macro can be converted to a string literal by using # before it.
#include <stdio.h>
#define get(a) #a
int main()
{
printf("%s\n", get(GeeksQuiz));//GeeksQuiz is converted to string i.e "geeksQuiz"
printf("%u\n", get(123));//123 is converted to string "123" and base address of this string is printed
printf("%u\n", get(Ali is Great)); //Ali is Great is converted to string "Ali is Great" and base address of this string is pr
printf("%s\n", get(Muhamad bin Qasim)); // Muhamad bin Qasim is converted to string i.e " Muhamad bin Qasim "
}
Output:
GeeksQuiz
4206638
4206646
Muhammad Bin Qasim
7) The macros can be written in multiple lines using ‘\’. The last line doesn’t need to have ‘\’.
#include <stdio.h>
#define PRINT(i, limit) while (i < limit) \
{ \
printf("GeeksQuiz\n "); \
i++; \
}
int main()
{
int i = 0;
PRINT(i, 3);
return 0;
}
Output:
GeeksQuiz
GeeksQuiz
GeeksQuiz
Output:- 100
1) The operator ## is called “Token-Pasting” or 1) After the pre-processing of a C program, a .i file is
“Merge” Operator generated which is passed to the compiler for
2) It merges two tokens into one compilation.
2) It concatenates all the parameters into one
token.
3) printf("%d ",f(var,12)); printf("%d ",var12);
Q3. Predict the output
#include <stdio.h> #include <stdio.h>
#define macro(n, a, i, m) m##a##i##n #define get(s) #s
#define MAIN macro(n, a, i, m)
int main()
int MAIN() {
{ char str[] = get(GeeksQuiz);
printf("GeeksQuiz"); printf("%s", str); //GeeksQuiz
return 0; return 0;
} }