#include<stdio.h> #define MESS junk int main() { printf("MESS\n"); return 0; }
#include<stdio.h> #define SQR(x)(x*x) int main() { int a, b=3; a = SQR(b+2); printf("%d\n", a); return 0; }
Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3.
Step 2: a = SQR(b+2); becomes,
=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .
=> a = 3+2 * 3+2;
=> a = 3 + 6 + 2;
=> a = 11;
Step 3: printf("%d\n", a); It prints the value of variable 'a'.
Hence the output of the program is 11
#include<stdio.h> int main() { float fval=7.29; printf("%d\n", (int)fval); return 0; }
#include<stdio.h> int main() { float f=43.20; printf("%e, ", f); printf("%f, ", f); printf("%g", f); return 0; }
printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20 as 43.200001.
printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.
#include<stdio.h> #include<math.h> int main() { float n=1.54; printf("%f, %f\n", ceil(n), floor(n)); return 0; }
printf("%f, %f\n", ceil(n), floor(n)); In this line ceil(1.54) round up the 1.54 to 2 and floor(1.54) round down the 1.54 to 1.
In the printf("%f, %f\n", ceil(n), floor(n)); statement, the format specifier "%f %f" tells output to be float value. Hence it prints 2.000000 and 1.000000.
#include<stdio.h> #include<math.h> int main() { printf("%d, %d, %d\n", sizeof(3.14f), sizeof(3.14), sizeof(3.14l)); return 0; }
sizeof(3.14) here '3.14' specifies the double data type. Hence size of float is 8 bytes.
sizeof(3.14l) here '3.14l' specifies the long double data type. Hence size of float is 10 bytes.
Note: If you run the above program in Linux platform (GCC Compiler) it will give 4, 8, 12 as output. If you run in Windows platform (TurboC Compiler) it will give 4, 8, 10 as output. Because, C is a machine dependent language.
#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 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 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 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 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..."
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.