c query

  • Thread starter Ramalingam Chandran
  • Start date
R

Ramalingam Chandran

case 1:

int i;

void main()
{
calli();
printf("%d",i)
}


calli()
{
i=10;
}


output of i=?


case 2:

static int i;

void main()
{
calli();
printf("%d",i)
}


calli()
{
i=10;
}


output of i=?



question:

in both cases what will be the output of i and what is the difference
between declaring i as a global variable and i as a global static
variable.
 
J

Joona I Palaste

Ramalingam Chandran said:
void main()
{
calli();
printf("%d",i)
}



output of i=?

None. Your code won't compile. If you correct the typo, then we have two
answers:
Strictly speaking, it might be anything at all, it could crash your
program, it could crash your computer, it could paint your hard drive
purple. void main() causes undefined behaviour.
Loosely speaking, on most systems it should be 10.
static int i;
void main()
{
calli();
printf("%d",i)
}



output of i=?

The same applies here. Why do you think the static makes any
difference?

question:
in both cases what will be the output of i and what is the difference
between declaring i as a global variable and i as a global static
variable.

Making it static restricts its visibility to this translation unit.
Nothing more.
 
B

Barry Schwarz

case 1:

int i;

void main()
{
calli();
printf("%d",i)
}


calli()
{
i=10;
}


output of i=?


case 2:

static int i;

void main()
{
calli();
printf("%d",i)
}


calli()
{
i=10;
}


output of i=?



question:

in both cases what will be the output of i and what is the difference
between declaring i as a global variable and i as a global static
variable.

There are one obvious difference: In case 1, i is visible outside of
the source file it is defined in while in case 2 it is not.



<<Remove the del for email>>
 
M

Minti

case 1:

int i;

void main()
{
calli();
printf("%d",i)
}


calli()
{
i=10;
}


output of i=?

Did you _first_ compile and execute this program?(*)
case 2:

static int i;

void main()
{
calli();
printf("%d",i)
}


calli()
{
i=10;
}


output of i=?

Read (*).
question:

in both cases what will be the output of i

Read (*).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top