2

see once in hurry i have written

#include <stdio.h>

static flag;            
int main()
{
printf("flag is %d",flag);
return 0;
} 

it does not not give any warning or error and works fine. I dont understand why this is going work?

Jeegar Patel
  • 23,639
  • 42
  • 138
  • 202

5 Answers5

7

C assumes int when type is missing. It is also true for function definitions and their parameters:

// same as int add(int x, int y)
add(x,y) { return x + y; }

// same as int main()
main()
{
}
hamstergene
  • 22,806
  • 4
  • 50
  • 70
3

Staic is always default as a int and static int is 0 ..

sam_k
  • 5,757
  • 13
  • 70
  • 106
1

When int is used in conjunction with another keyword, int is optional

static int  <--> static
volatile int  <--> volatile
short int <--> short
long int <--> long

While short and long are accepted short forms, you should avoid leaving it out in conjunction with storage keywords (static, volatile).

Klas Lindbäck
  • 32,158
  • 4
  • 51
  • 77
1

Primary datatypes are

char , int , float , double

the Storage classes like

auto,static,extern etc

data type modifiers like

long,signed,unsigned etc,

if the storage classes or datatype modifiers miss the primary datatype of the variable then the compiler treats them as integer by default and even if function return types are not defined they are treated as int by default

example

long i is similar to long int i
static i is similar to static int i

But you cant simply say i and ask why compiler doesnt treat it as int

niko
  • 8,737
  • 25
  • 73
  • 128
-1

flag is initialized to 0, as a global static variable. other languages like Java will give an error if you try to use an uninitialized variable. i'm assuming this is C, in which case flag defaults to type int. please don't ever do this. it's really bad programming practice.

darkphoenix
  • 2,007
  • 3
  • 13
  • 12