Prerequisite: Operators in C
1. What will be the output of the following?
#include <stdio.h>
int main( void )
{
int a;
int b = 1;
int x[5] = { 1, 2, 3, 4, 5 };
a = 5 * 4 + x[--b] - (9 / b);
printf ( "%d" , a);
return 0;
}
|
Options:
1. 12
2. Abnormal Termination
3. 21
4. No Output
The answer is option(2).
Explanation : Here the square bracket has the highest priority that’s why it is evaluated first. After that parenthesis enjoys the priority but we have error because float point exception occurs when we divide 9/0.
2. What will be the output of the following?
#include <stdio.h>
int main( void )
{
int a;
int b = 1;
int x[5] = { 1, 2, 3, 4, 5 };
a = 5 * 4 + x[b++] - (9 / b);
printf ( "%d" , a);
return 0;
}
|
Options:
1. 12
2. 20
3. 18
4. No Output
The answer is option(3).
Explanation:Here the square bracket has the highest priority that’s why it is evaluated first. After that parenthesis enjoys the priority.Now a=5*4+x[b++]-(9/b)=18.
Refer: www.geeksforgeeks.org/c-operator-precedence-associativity/
3. what will be the output of the following?
#include <stdio.h>
int main( void )
{
int a;
int i = 1;
int b = 10 * i + sizeof (--i) + 4 - 10 / i;
printf ( "%d" , b);
return 0;
}
|
Options:
1. 4
2. 2
3. 6
4. 8
The answer is option(4).
Explanation:Here the sizeof() operator enjoys the highest priority, which result in 4. But evaluation is not possible inside sizeof() operator.
4. What will be the output of the following?
#include <stdio.h>
int main( void )
{
int a = 9;
float x;
x = a / 2;
printf ( "%f" , x);
return 0;
}
|
Options:
1. 4.000000
2. 0
3. 4
4. No output
The answer is option(1).
Explanation: Here 9/2 which is 4. Then 4 is assigned to float and it becomes 4.000000.
5. What will be the output of the following?
#include <stdio.h>
int main( void )
{
int a = 9;
float x;
x = ( float )a / 2;
printf ( "%f" , x);
return 0;
}
|
Options:
1. 4.000000
2. 4.50
3. 4
4. 4.500000
The answer is option(4).
Explanation: With the help of type-casting we are able to get the exact result.
Reference: https://www.geeksforgeeks.org/type-conversion-c/
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Sep, 2017
Like Article
Save Article