The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
90 views

https://gateoverflow.in/45555/c-programming-predict-the-output

main()
{
    {
        extern int i;
        int i=20;
        {
            const volatile unsigned i=30; printf("%d",i);
        }
        printf("%d",i);
    }
printf("%d",i);
}
int i;

rude explained how output is printed but its giving error when i'm trying to run the program here https://ideone.com/xteXgV

in Programming by Boss (35k points)
edited by | 90 views
0
It should give an error because you are saying the variable is constant and then you are confusing it by saying volatile
0

according to me code is wrong because 

extern int i;         // i is declared

Then again in next line

int i=20;  /* defined i but it is treated as local variable because there was no declaration of y globally with extern */
extern int i;
main()
{
    {
        int i=20;
        {
            const volatile unsigned i=30; printf("%d",i);
        }
        printf("%d",i);
    }
printf("%d",i);
}
int i;

https://ideone.com/TxkXNu

The above modified code will work fine and print 30200

0
I think this will cause multiple declaration error even if you run the code with "extern int i" inside main then it will work fine.
0

Shubhgupta which will cause multiple declaration error?

0
which you mentioned in question not your comment one.. :)
0

Shubhgupta yes so the selected answer on that question needs correction right? unfortunately not many people commented here for confirmation that's why i'm unable to deselect that answer 

0
yes correction is needed for that question.
+1

Mk Utkarsh When we declare a variable as extern, its a promise to the compiler that the definition of that variable will be at some other place in global scope and not in the same block in which the variable is declared as extern. So when u try to define the variable in the same block in which it is declared, it will result in compile time error as its a local variable and it wont fulfill the promise made to the compiler since local variables are invisible to linkers.

Am i correct?

0

Somoshree Datta 5 i also believe the same

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,092 questions
55,276 answers
190,804 comments
86,089 users