edited by
1,153 views
3 votes
3 votes
#include <stdio.h>
int main()
{
    int i;
    printf("%d", &i)+1;
    scanf("%d", i)-1;
}

a. Runtime error.

b. Runtime error. Access violation.

c. Compile error. Illegal syntax

d. None of the above

edited by

2 Answers

Best answer
3 votes
3 votes
printf("%d", &i)+1;

This prints the address of "i" but using "%d" causes the address to be printed in signed format which can give negative value. Also, this assumes integer size and pointer size are the same which might not be true. For address "%p" must be used. Anyway there is no error here as printf function returns integer (the no. of characters successfully printed) and this returned value is added with 1. In C language there is no restriction that a value must always be assigned or used. Simply writing 

123;

is also valid in C. 

scanf("%d", i)-1;

Like printf, scanf also returns an int. But it returns the no. of items successfully read. So, here also there is no syntax error. But scanf moves the read data to the memory location given to it as argument(s). Here, i is used as a memory location which most likely won't be valid. Hence, this causes runtime error- segmentation fault in Linux. 

selected by
1 votes
1 votes
I think no compile time and run time error. I t will print some garbage value

Related questions

3 votes
3 votes
1 answer
1
5 votes
5 votes
3 answers
2
suvasish pal asked Aug 26, 2017
632 views
Will it result in to an error if a header file is included twice?[A].Yes[B].No[C].It is compiler dependent
1 votes
1 votes
1 answer
3
Vasu Srivastava asked Aug 18, 2017
1,062 views
#include <stdio.h int main(void){ int i=511; char *p = (char *)&i; printf("%d", *p); }OK so why take 2's complement and not simple binary number? Means, why is C giving -...
2 votes
2 votes
6 answers
4
Nitesh Choudhary asked Jun 21, 2017
1,181 views
what is the o/p #include<stdio.h void main() { int a=5; printf("%d %d %d",a++,++a, a); }also suggest me notes so i can clear my concept about printf function (For Gate)