Predict the Output:
int main()
{
int *p=(int *)2000;
scanf("%d",2000);
printf("%d",*p);
return 0;
}

if input is 20 ,what will be print

Answers were Sorted based on User's Feedback



Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%..

Answer / vadivelt

Result:
Ans1.It prints the input given
Ans2.Program Crashes

Why Ans1?
--
1.If the memory 2000 is not a system or read only location
and if it is not a address of other constant varible which
is assigned by the compiler, then the input is stored in
the location. And it will be fetched in the prinf()
statement using *p and ll be printed.

Why Ans2?
--
In this program the pointer *p, does not holds the address
of a variable for which memory is allocated(may be static
or dynamic).Instead blindly it holds the address 2000.

The address may contain.
1.System files(OS) or
2.It may be a location from read only memory.

So, when we are trying to get input value and store it in
the location 2000, using scanf(), it may try to overwrite
the data in system file location or read only memory.

So the program Ultimately has to crash.

Is This Answer Correct ?    7 Yes 0 No

Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%..

Answer / srsabariselvan

20

20 will stored in address 2000

Is This Answer Correct ?    3 Yes 2 No

Post New Answer

More C Code Interview Questions

How to reverse a String without using C functions ?

33 Answers   Matrix, TCS, Wipro,


Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.

2 Answers  


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }

2 Answers  


main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

5 Answers   HCL,






plz send me all data structure related programs

2 Answers  


WAP to display 1,2,3,4,5........N

2 Answers  


plz send me all data structure related programs

2 Answers  


What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }

1 Answers  


Code for 1>"ascii to string" 2>"string to ascii"

1 Answers   Aricent, Global Logic,


how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!

8 Answers   GATE,


write a program for area of circumference of shapes

0 Answers  


Categories