Question:
can anybody explain me the below program pls?
pavitha
2009-03-07 02:56:31 UTC
main()
{
extern int i;
{ int i=20;
{
const volatile unsigned i=30; printf("%d",i);
}
printf("%d",i);
}
printf("%d",i);
}
int i;
answer is 30,20
Three answers:
Gaurav K
2009-03-07 03:38:14 UTC
here brackets i.e. "{" defines the scope of the variables



for ex in line 4

i=20 has scope up to the closing of that curly bracket which start before it.



further int line 5

a constant volatile unsigned is declared but i think there is some problem it must be declared as below



const volatile unsigned int i=30;



here const is for constant

volatile is that it can be removed at the run time within the scope

unsigned is for it range to be only in positive integers.

after declaration it is printing of i=30

then in second printf, 20 will be printed

then in next printf it will print garbage value as no value is assigned to



extern int i;



and thus it gives u that result u showed
koppe74
2009-03-07 03:43:52 UTC
Difficult to explain without the complete listing, but I'll try...



"extern" tells that the variable is really declared somewhere else, in another source-file. Often it will be refering to a variable or function belonging to a library.



"volatile" tells the compiler that the variable can change in ways not controlled by the program, and therefor not to use optimaztions that assumes that "if the program hasn't changed the variable, then it's unchanged". This would be the case for a variable (pointer) "connected" to something like the system-clock -- it should show current-time, not what the time was last time the program checked.



"const" declares a constant, a variable that should not be changed. As it is together with "volatile", the "const" may be there to protect us from changing something that should remain outside the control of the program -- like the system clock (we're allowed to read it, but should not be allowed to change it). This way, "const" will lock the content of a variable -- or something pointed to be a pointer -- and not allows us to change it.



For pointers, we can also use "const" to lock the address (the address can't be changed), but still allows us to change the content... it depends upon where the "const" is. E.g "const double *ptr" and "double const *ptr" -- one allows the address to change, but we can't change the *content* of what that address points to... and one locks the address (we can't change what it points to), but allows us to change the content (of what it points to).



Not a really good answer, as I would have to see the full source code for both this program and for where the "extern" is declared.
sarvan k
2009-03-07 08:13:52 UTC
This program is to show how a single variable i is differed with the use of various type of specifiers.



since the unsigned value is assigned 30 is printed first.

Then after the const scope is completed the external value 20 is printed.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...