IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Const - Discussion

@ : Home > C Programming > Const > Find Output of Program - Discussion

Read more:

"I never think of the future. It comes soon enough."
- Albert Einstein
4. 

What will be the output of the program?

#include<stdio.h>

int main()
{
    const int x=5;
    const int *ptrx;
    ptrx = &x;
    *ptrx = 10;
    printf("%d\n", x);
    return 0;
}

[A]. 5[B]. 10
[C]. Error[D]. Garbage value

Answer: Option E

Explanation:

Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'.

Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.

Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx.

Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error.

To change the value of const variable x we have to use *(int *)&x = 10;


Prudhvi said: (Thu, Jul 14, 2011 12:23:18 PM)    
 
Any one give me a clear explanation to step 4.

Hari said: (Thu, Aug 25, 2011 10:43:46 AM)    
 
I also don't think step 4 willl work how can it will try to change the value of x answer is 5.

Martyf said: (Tue, Sep 13, 2011 10:36:57 AM)    
 
It is very difficult paper.

Prakash said: (Sun, Oct 30, 2011 12:16:54 PM)    
 
int main()
{
int x=5;
int *ptrx;
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
}
To change the value of const variable x we have to use *(int *)&x = 10;

Bijay said: (Thu, Dec 1, 2011 01:09:52 PM)    
 
In gcc compiler it will work o/p:10.

Voski said: (Mon, Jan 16, 2012 11:23:57 AM)    
 
A small correction in the Explanation Step 2 is needed: ptrx is not a constant, but a pointer to an integer constant.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!