C Programming - Const - Discussion

Discussion :: Const - Find Output of Program (Q.No.5)


What will be the output of the program in TurboC?

int fun(int **ptr);

int main()
    int i=10, j=20;
    const int *ptr = &i;
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    return 0;

[A]. i= FFE2 ptr=12 j=FFE4 ptr=24
[B]. i= FFE4 ptr=10 j=FFE2 ptr=20
[C]. i= FFE0 ptr=20 j=FFE1 ptr=30
[D]. Garbage value

Answer: Option B


No answer description available for this question.

Prashant Dixit said: (Jan 30, 2011)  
There must be an error in this program because ptr is declared as of constant type and it can not be modified so value of j can not be stored in ptr.

Himanshu said: (Feb 21, 2011)  
Can't get it. Please explain it in step by step procedure.

Sundar said: (Mar 18, 2011)  

In Turbo C (under DOS - 16 bit platform)

i = FFF4 ptr = 10 j = FFF2 ptr = 20

In GCC (under Linux - 32 bit platform)

i = BF93368C ptr = 10 j = BF933688 ptr = 20

I hope this may be help you a bit. Have a nice day guys!

Ajay said: (Jul 9, 2011)  
const int *ptr = &i;

It declares a pointer which points to integer of constant type.
Pointer is not constant.So we can change it.

Sharath said: (Jul 14, 2011)  
We can change the value of constant pointer

Sharath said: (Jul 14, 2011)  
int i=10;
const int *ptr=10; //correct
*ptr=&i; //correct
*ptr=10; //wrong

Siva Pavan said: (Sep 14, 2011)  
Coming to 2nd printf statement, since in pointers &p represents address,*p represents value then clearly p is declared as pointer preccedding by const keyword so its value is always constant since it is addressed to x(p=&x) the only possible value for p is x value i.e 10.

Karun Das said: (Feb 16, 2012)  
Yes, here an object is a constant, we can't change pointer here,

Totally confusion, please correct me if I am thinking in incorrect way.

Rupinderjit said: (May 21, 2012)  
Here object is constant not pointer pointed to by it. So ptr can point to any other address unless we declare int const*ptr=&i;, here pointer points to particular location is constant not an object.

Ashok said: (Oct 5, 2012)  
"const int *ptr" means ptr is a pointer to const integer so we can modify ptr value.

Where as "int const *ptr" is different one it means ptr is const pointer to int whose value could not be changed. We have to define it at the declaration of that pointer itself.

Vineet Yadav said: (Mar 18, 2013)  
const int *ptr;
int const* ptr;

It means pointer points to const integer value. that means we cant change the value of that integer but pointer can change its pointing location.

int * const ptr;
const * int ptr;

It means that pointer is constant here, it can point to an integer and then it cant change its pointing location.

Chaz said: (Apr 13, 2013)  
Simplifying the above explanation,

const int *ptr //cannot modify *ptr

int* const ptr //cannot modify ptr

Difference in ptr and *ptr is self-explanatory.

Ritesh Lenka said: (Jul 25, 2013)  
Here the int value that pointed by ptr is const. Not the ptr. When we declare int const *ptr, then ptr is consider as a constant.

Akshay11 said: (Oct 3, 2013)  
What does %5x means?

Abhishek said: (May 29, 2015)  
Can't understand? please explain?

Varinder said: (Jul 20, 2015)  
How constant may change?

Debasisd16061 said: (Jul 29, 2015)  
What is %5x?

Surbhi said: (Sep 6, 2015)  
% 5x here x is for hexadecimal value of ptr that has assigned to I and 5 specifies the width of ptr at least 5 digits wide (space also included).

Suyog said: (Dec 3, 2016)  
Please, can anybody explain it detail?

Bhavani said: (May 9, 2017)  
How FFE4 and FFE2 came in output? Please explain me.

Shalini said: (Aug 18, 2017)  
Here FFE4 and FFE2 are address of I and j variable respectively.

We can't predict address of variables every time.

I think some peoples doubt is same, why address of i is higher than address of j answer is because stack grows downwards in memory.

I hope you get it.

Selvakumar said: (Apr 26, 2018)  
I m not getting the given code. Please anyone explain this.

Appu said: (Jun 25, 2018)  
Please explain the given code.

Aditya said: (Jul 7, 2018)  
What will be the output of the following statement with an explanation?


Dhivya.S said: (Jul 12, 2019)  
Please explain how FFF4 from ptr?

Pooja said: (May 4, 2020)  
I can't understand the output. Please explain me.

Rishabh Singh said: (Aug 29, 2020)  
const int* ptr;

It declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified.

const int a = 10;
const int* ptr = &a;
*ptr = 5; // wrong
ptr++; // right

int * const ptr;
declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr can be modified.

int a = 10;
int *const ptr = &a;
*ptr = 5; // right
ptr++; // wrong
int const *ptr; // ptr is a pointer to constant int
int *const ptr; // ptr is a constant pointer to int

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.