#include<stdio.h> #define str(x) #x #define Xstr(x) str(x) #define oper multiply int main() { char *opername = Xstr(oper); printf("%s\n", opername); return 0; }
The macro #define Xstr(x) str(x) replaces the symbol 'Xstr(x)' with 'str(x)'.
The macro #define oper multiply replaces the symbol 'oper' with 'multiply'.
Step 1: char *opername = Xstr(oper); The varible *opername is declared as an pointer to a character type.
=> Xstr(oper); becomes,
=> Xstr(multiply);
=> str(multiply)
=> char *opername = multiply
Step 2: printf("%s\n", opername); It prints the value of variable opername.
Hence the output of the program is "multiply"
#include<stdio.h> #define FUN(arg) do\ {\ if(arg)\ printf("CuriousTab...", "\n");\ }while(--i) int main() { int i=2; FUN(i<3); return 0; }
Step 1: int i=2; The variable i is declared as an integer type and initialized to 2.
Step 2: FUN(i<3); becomes,
do
{
if(2 < 3)
printf("CuriousTab...", "\n");
}while(--2)
After the 2 while loops the value of i becomes '0'(zero). Hence the while loop breaks.
Hence the output of the program is "CuriousTab... CuriousTab..."
#include<stdio.h> #define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2); int main() { char *str1="India"; char *str2="CURIOUSTAB"; JOIN(str1, str2); return 0; }
#include<stdio.h> #define PRINT(int) printf("int=%d, ", int); int main() { int x=2, y=3, z=4; PRINT(x); PRINT(y); PRINT(z); return 0; }
Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.
Step 2: PRINT(x); becomes printf("int=%d,",x). Hence it prints 'int=2'.
Step 3: PRINT(y); becomes printf("int=%d,",y). Hence it prints 'int=3'.
Step 4: PRINT(z); becomes printf("int=%d,",z). Hence it prints 'int=4'.
Hence the output of the program is int=2, int=3, int=4.
#include<stdio.h> #define CUBE(x) (x*x*x) int main() { int a, b=3; a = CUBE(b++); printf("%d, %d\n", a, b); return 0; }
Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.
Step 2: a = CUBE(b++); becomes
=> a = b++ * b++ * b++;
=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.
=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)
Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b.
Hence the output of the program is 27, 6.
#include<stdio.h> #define SQUARE(x) x*x int main() { float s=10, u=30, t=2, a; a = 2*(s-u*t)/SQUARE(t); printf("Result = %f", a); return 0; }
Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.
Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,
=> a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .
=> a = 2 * (10 - 30 * 2) / 2 * 2;
=> a = 2 * (10 - 60) / 2 * 2;
=> a = 2 * (-50) / 2 * 2 ;
=> a = 2 * (-25) * 2 ;
=> a = (-50) * 2 ;
=> a = -100;
Step 3: printf("Result=%f", a); It prints the value of variable 'a'.
Hence the output of the program is -100
#include<stdio.h> #define MAX(a, b) (a > b? a : b) int main() { int x; x = MAX(3+2, 2+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); becomes,
=> x = (3+2 > 2+7 ? 3+2 : 2+7)
=> x = (5 > 9 ? 5 : 9)
=> x = 9
Step 3 : printf("%d\n", x); It prints the value of variable x.
Hence the output of the program is 9.
#include<stdio.h> #define SWAP(a, b) int t; t=a, a=b, b=t; int main() { int a=10, b=12; SWAP(a, b); printf("a = %d, b = %d\n", a, b); return 0; }
Step 1: int a=10, b=12; The variable a and b are declared as an integer type and initialized to 10, 12 respectively.
Step 2: SWAP(a, b);. Here the macro is substituted and it swaps the value to variable a and b.
Hence the output of the program is 12, 10.
#include<stdio.h> #define MIN(x, y) (x<y)? x : y; int main() { int x=3, y=4, z; z = MIN(x+y/2, y-1); if(z > 0) printf("%d\n", z); return 0; }
Step 1: int x=3, y=4, z; The variable x, y, z are declared as an integer type and the variable x, y are initialized to value 3, 4 respectively.
Step 2: z = MIN(x+y/2, y-1); becomes,
=> z = (x+y/2 < y-1)? x+y/2 : y - 1;
=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;
=> z = (3+2 < 4-1)? 3+2 : 4 - 1;
=> z = (5 < 3)? 5 : 3;
The macro return the number 3 and it is stored in the variable z.
Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.
Step 4: printf("%d\n", z);. It prints the value of variable z.
Hence the output of the program is 3
#include<stdio.h> #define PRINT(i) printf("%d,",i) int main() { int x=2, y=3, z=4; PRINT(x); PRINT(y); PRINT(z); return 0; }
Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.
Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'.
Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'.
Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'.
Hence the output of the program is 2, 3, 4.
#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.
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.