#include<stdio.h> #define MAX(a, b, c) (a>b? a>c? a : c: b>c? b : c) int main() { int x; x = MAX(3+2, 2+7, 3+7); printf("%d\n", x); return 0; }
Step 1: int x; The variable x is declared as an integer type.
Step 2: x = MAX(3+2, 2+7, 3+7); becomes,
=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)
=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )
=> x = (5 >9 ? (10): (10) )
=> x = 10
Step 3: printf("%d\n", x); It prints the value of 'x'.
Hence the output of the program is "10".
#include<stdio.h> #define MAN(x, y) ((x)>(y))? (x):(y); int main() { int i=10, j=5, k=0; k = MAN(++i, j++); printf("%d, %d, %d\n", i, j, k); return 0; }
Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.
Step 2: k = MAN(++i, j++); becomes,
=> k = ((++i)>(j++)) ? (++i):(j++);
=> k = ((11)>(5)) ? (12):(6);
=> k = 12
Step 3: printf("%d, %d, %d\n", i, j, k); It prints the variable i, j, k.
In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.
Hence the output of the program is 12, 6, 12
#include<stdio.h> #define FUN(i, j) i##j int main() { int va1=10; int va12=20; printf("%d\n", FUN(va1, 2)); return 0; }
#include<stdio.h>
#define FUN(i, j) i##j
int main()
{
int First = 10;
int Second = 20;
char FirstSecond[] = "CuriousTab";
printf("%s\n", FUN(First, Second) );
return 0;
}
Output:
-------
CuriousTab
The preprocessor will replace FUN(First, Second) as FirstSecond.
Therefore, the printf("%s\n", FUN(First, Second) ); statement will become as printf("%s\n", FirstSecond );
Hence it prints CuriousTab as output.
Like the same, the line printf("%d\n", FUN(va1, 2)); given in the above question will become as printf("%d\n", va12 );.
Therefore, it prints 20 as output.
A function is compiled once and can be called from anywhere that has visibility to the funciton.
Example: #define PI 3.14
We can undefine PI macro by #undef PI
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.