What is the output of the following program?void main(){int *ptr =55;clrscr();printf("%d", ++(ptr));}

Showing Answers 1 - 27 of 27 Answers

Supriya Ahire

  • Feb 15th, 2007
 

Hi, O/P of this program will be 57.due to integer it is adding 2 bytes(on dos env.)-Ms.Supriya Ahire

  Was this answer useful?  Yes

raja.s

  • Feb 17th, 2007
 

hi i think that the address value will be incremented with 2. If it's wromg correct me.

  Was this answer useful?  Yes

a

  • Feb 21st, 2007
 

the value can be 57 or 59 or 63, depending on the size of integer of the os, 2 bytes or 4 bytes or 8 bytes

  Was this answer useful?  Yes

ronakg

  • Mar 3rd, 2007
 

this will give an error saying can not convert int to int *.

if u redeclare

int *ptr = (int *)55;

then output will be 55 + sizeof(int)

  Was this answer useful?  Yes

Prajapati Ketan

  • Mar 7th, 2007
 

HI
The address value of 55 will be incremented by 1.

  Was this answer useful?  Yes

If we write
    int *x= 55;

It will give an error. During the declaration of pointers, they can only be  initialized with  addresses .

For example

  int a[10]={1,2,3};
  int *ptr=a;

Will work

  Was this answer useful?  Yes

abhimanipal

  • Jan 21st, 2010
 

int* ptr= 55 will not give you an error. It will give you a warning. Compile it and see. After that the output will be 55 + (size of int on your machine)

  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

First, the program won't even compile, as C has no clrscr function and you haven't provided one.

Second, if you fix that, the program still relies upon undefined behaviour - void main has never been a legal C construct - and thus the results are not defined.

Third, if you fix that, the program _still_ relies upon undefined behaviour, as ptr is a pointer, but you are trying to print an int; since there is no expectation whatsoever that the two types are remotely compatible, the results are not defined.

Fourth, you don't have the proper headers included.  Use of variadic functions (eg printf) without proper prototypes in scope leads to either undefined or implementation defined behaviour (don't recall which, offhand) but, again, the effect is that the results are not defined (at least, not unambiguously).

  Was this answer useful?  Yes

The result is undefined (meaning the output could be just about anything, including no output at all) for several reasons, the most pertinent one being that the "%d" conversion specifier expects the corresponding argument to be type int, not pointer to int (unless the original code dereferenced ptr inside the parentheses and got munged by the forum software (which is highly likely; I've never seen forum software that goes out of its way to mangle code this badly), in which case the result is well-defined and the output would be 56). 

Note: make sure that your compiler or IDE documentation explicitly states that "void main()" is a valid signature for main; I know that Visual Studio 2005 allows it (for C++, anyway), but some (such as gcc) do not, and using an unsupported signature for main also invokes undefined behavior.  For maximum portability, use "int main(void)". 

Also, remember that display control functions like clrscr() are not part of the standard C library, and will not be defined on all platforms.  That snippet won't even build on a Linux platform using gcc, for example. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions