-4
#include<stdio.h>
int a;
int fun(int b)
{
   static int c=3;
   return ((++a)+(++b)+(++c));
}
Main()
{
   int i,a=2;
   for(i=0;i<2;i++)
      printf("%5d",fun(a));
}

I think answer is 1012 but don't have comp

I wanna know progress

svick
  • 214,528
  • 47
  • 357
  • 477
Frank Hao
  • 1
  • 2
  • 2
    `Main()` won't even compile. "*I wanna know progress*" Progress? of what? – Spikatrix May 13 '15 at 07:42
  • 3
    use some online compiler like http://ideone.com/ and check it yourself – Sourav Ghosh May 13 '15 at 07:42
  • you are wrong. A small hint: `a` in main is not the same as the global `a`. – mch May 13 '15 at 07:45
  • 1
    @LPs Perhaps you are doing the same mistake what OP did. – Dayal rai May 13 '15 at 07:47
  • @Dayal and Frank No, I have to press the correct number on my f***ing keyboard :). Anyway your code cannot compile. At least change `Main` to `void main (void)` – LPs May 13 '15 at 07:51
  • The output is ` 8 10` formatted in spaces. – Weather Vane May 13 '15 at 07:54
  • in main a=2 How's the gobal a and a in fun?I wanna know why is 8.sorry for my dumb.I am Chinese with not really good English :) – Frank Hao May 13 '15 at 07:59
  • If you change the first `int a;` to `int d;` and change `(++a)` to `(++d)` the result should be a bit more obvious, since `fun()` knows nothing about the `a` in `main()`. – Weather Vane May 13 '15 at 08:00
  • One more question, those int float double number,what would happen if they are not given values? Int goes 0?I think compiler will give me "error" for that – Frank Hao May 13 '15 at 08:03
  • Previous question about initialiazation: http://stackoverflow.com/questions/1831290/static-variable-initialization – Weather Vane May 13 '15 at 08:05
  • "*those int float double number,what would happen if they are not given values*" Some random "garbage" value is in them. Reading it leads to undefined behavior. You should not do this. 0 will be stored if the variable is global or `static`. In this case, you can read/write into these variables. – Spikatrix May 13 '15 at 08:09
  • If you don't have a compiler, why are you programming? This isn't a "I'm too lazy to even use an online compiler" service. And what's "I wanna know progress" supposed to mean? – Lundin May 13 '15 at 08:58
  • @Lundin Sorry.in this case,I just wanna pass my C programming test.I love programming. I mean really,I was a active python programmer, and I love that awesome language. For now,I had c p programming language at my university, and I don't have enough time to catch every detail of this language.but I must pass the exam – Frank Hao May 13 '15 at 09:29
  • Your problem. Why are you trying to export it here? – Martin James May 13 '15 at 14:26

1 Answers1

2

in response to Frank Hao comment

Global int a is initialized as 0.

for i = 0;
first call fun(2)
{
return ((1)+(3)+(4));
}

for i = 1;
second call fun(2)
{
return ((2)+(3)+(5));
}
Dayal rai
  • 6,115
  • 19
  • 28