C Programming Questions and Answers
What will be output of the following?
#include<stdio.h>int main() { float a=0.7; if(a < 0.7) printf("Cn"); else printf("C++n"); return0; }
Answer : Option A
Explanation :
if(a < 0.7) here a is a float variable and 0.7 is a double constant. The float variable a is less than double constant 0.7. Hence the if condition is satisfied and it prints 'C'
#include<stdio.h>int main()
{
float a=0.7;
printf("%.10f %.10fn",0.7, a);
return0;
}
Output:
0.7000000000 0.6999999881
A float occupies 4 bytes. if the hexadecimal equivalent of each of these bytes is A,B,C and D, then when this float is stored in memory, these bytes get stored in the order
Answer : Option B
We want to round off x, a float to an int value. The correct way to do so would be
Answer : Option A
What will be output of the program ?
#include<stdio.h>#include<math.h>int main()
{
printf("%d, %d, %dn", sizeof(3.14f), sizeof(3.14), sizeof(3.14l));
return0;
}
Answer : Option C
Explanation :
sizeof(3.14f) here '3.14f' specifies the float data type. Hence size of float is 4 bytes.
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.
By default any real number is treated as :
Answer : Option B
What will be output of the following ?
#include<stdio.h>#include<math.h>int main()
{
printf("%fn", sqrt(36.0));
return0;
}
Answer : Option C
Explanation :
printf("%fn", sqrt(36.0)); It prints the square root of 36 in the float format(i.e 6.000000).
Declaration Syntax: double sqrt(double x) calculates and return the positive square root of the given number.
Binary equivalent of 5.375 is
Answer : Option B
Which error you are likely to get when you run the following program?
#include<stdio.h>
main()
{
struct emp
{
char name[20];
float sal;
}
struct emp e[10];
int i;
for(i=0;i<=9;i++)
scanf("%s, %f",e[i].name,&e[i].sal);
return 0;
}
Answer : Option B
If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what would be the output of the following program?
#include<stdio.h>
int main()
{
float a =5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i++)
printf("%02x", (unsigned char)p[i]);
return 0;
}
Answer : Option C
What will be the output of the following program :
#include<stdio.h>
main()
{
int a=555,b=*ptr,*ptr=&a;
printf("%d %d %d",++a,--b,*ptr++);
}
Answer : Option A