#include<stdio.h>
#define clrscr() 10
int main()
{
printf("result=%d\n",clrscr());
return 0;
}
Solution
result=10
#defines are used for textual replacement
2.What is the output of the following program?#defines are used for textual replacement
#include<stdio.h>
#define fun(f1,f2) f1##f2
int main()
{
int some100=100;
printf("result=%d\n",fun(some,100));
return 0;
}
Solution
result=100
To know about the ## operator Click here
To know about the ## operator Click here
3.What is the output of the following program?
#include<stdio.h>
#define FALSE -1
#define TRUE 1
#define NULL 0
int main()
{
if(NULL)
puts("NULL\n");
else if(FALSE)
puts("FALSE\n");
else
puts("TRUE\n");
return 0;
}
Solution
FALSE
Preprocessor doesnt replace the vaues given inside the double quotes.The check by if condition is boolean value false. so it goes to else if part.-1 is boolean value true. hence it prints FALSE.
4.What is the output of the following program?Preprocessor doesnt replace the vaues given inside the double quotes.The check by if condition is boolean value false. so it goes to else if part.-1 is boolean value true. hence it prints FALSE.
#include<stdio.h>
#define max 5
#define int arr1[max]
main()
{
typedef char arr2[max];
arr1 list={0,1,2,3,4};
arr2 name="name";
printf("%d %s\n",list[0],name);
return 0;
}
Solution
Compier Error :arr1 undeclared
arr2 is decared of ype array of size 5 of characters.so it can be used to declare the variable name of the type arr2.But it is not the case of arr1.Hence an error.
#defines are used for textual replacement whereas typedefs are used for declaring new types.
arr2 is decared of ype array of size 5 of characters.so it can be used to declare the variable name of the type arr2.But it is not the case of arr1.Hence an error.
#defines are used for textual replacement whereas typedefs are used for declaring new types.
5.What is the output of the following program?
#include<stdio.h>
#define int char
main()
{
int i=65;
printf("sizeof(i)=%d \n",sizeof(i));
return 0;
}
Solution
sizeof(i)=1
#define replaces the string int by the macro char.The size of char is 1 byte
#define replaces the string int by the macro char.The size of char is 1 byte
6.What is the output of the following program?
#include<stdio.h>
#define assert(cond) if(!(cond))\
printf("assertion failed for condition %s\n",#cond)
main()
{
int x=100;
if(x==100)
assert(x<100);
else
printf("No assert call\n");
return 0;
}
Solution
assertion failed for condition x<100
7.What is the output of the following program?
#include<stdio.h>
#define assert(cond) if(!(cond))\
printf("assertion failed for condition %s\n",#cond)
main()
{
int x=100;
if(x==0)
assert(x<100);
else
printf("No assert call\n");
return 0;
}
Solution
No output
The else part in which the printf is there becomes the else for if in the assert macro.Hence nothing is printed.The solution is to use conditional operator instead of if statement.#define assert(cond) ((cond)?(0):printf(""))
The else part in which the printf is there becomes the else for if in the assert macro.Hence nothing is printed.The solution is to use conditional operator instead of if statement.#define assert(cond) ((cond)?(0):printf(""))
8.What is the output of the following Program?
#include<stdio.h>
#define string_def char*
main()
{
typedef char* string_type;
string_type s1="My",s2="name";
string_def s3="is",s4="Suni";
printf("%s %s %s %s\n",s1,s2,s3,s4);
return 0;
}
Solution
Segmentation Fault
The preprocessor output is
main()
{
typedef char* string_type;
string_type s1="My",s2="name";
char* s3="is",s4="Suni";
printf("%s %s %s %s\n",s1,s2,s3,s4);
return 0;
}
s4 is of type char not char* so printing s4 as a string produce segmentation fault.
The preprocessor output is
main()
{
typedef char* string_type;
string_type s1="My",s2="name";
char* s3="is",s4="Suni";
printf("%s %s %s %s\n",s1,s2,s3,s4);
return 0;
}
s4 is of type char not char* so printing s4 as a string produce segmentation fault.
9.What is the output of the following program?
#include<stdio.h>
#define DEF(array,type) sizeof(array)/sizeof(type)
main()
{
int arr[10];
printf("Total number of array elements=%d",DEF(arr,int));
return 0;
}
Solution
Total number of array elements=10
The size of integer array of 10 elements is 10*sizeof(int).The macro expands to sizeof(arr)/sizeof(int)=>10*sizeof(int)/sizeof(int).So the answer is 10
The size of integer array of 10 elements is 10*sizeof(int).The macro expands to sizeof(arr)/sizeof(int)=>10*sizeof(int)/sizeof(int).So the answer is 10
10.What is the output of the following Program?
#include<stdio.h>
#define max main()
main()
{
max;
printf("Hello Welcome\n");
return 0;
}
- Compilation error
- Preprocessing error
- runtime error
- executes until the stack overflows
4.executes until the stack overflows
Can you please write about
ReplyDelete1)Bufferring and Blocking
2)Assert
3)sizeof