3

I read somewhere that default floating point values like 1.2 are double not float.
So what are default integer values like 6 , are they short , int or long?

Lundin
  • 195,001
  • 40
  • 254
  • 396
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
  • I heard that range of `short` and `int` is same. so a value bigger than `int` may come under `long` but what about small values, as they can be `short` and `int` both. – kevin gomes Jan 27 '14 at 14:11
  • By the way, these are normally called "constant expressions" or "integer constants". The phrase "default values" normally implies a standard value like `0` or `0.0` which happens in certain cases (such as defining a `static` variable without an initializer) – Brandin Jan 27 '14 at 14:33
  • 1
    After reading the answers below, test yourself and see if you can explain why `sizeof(4294967295u)` < `sizeof(2147483648)` :) – Lundin Jan 27 '14 at 14:59

3 Answers3

7

The type of integer literals given in base 10 is the first type in the following list in which their value can fit:

  • int
  • long int
  • long long int

For octal and hexadecimal literals, unsigned types will be considered as well, in the following order:

  • int
  • unsigned int
  • long int
  • unsigned long int
  • long long int
  • unsigned long long int

You can specify a u suffix to force unsigned types, an l suffix to force long or long long, or an ll suffix to force long long.

Reference: C99, 6.4.4.1p5

interjay
  • 107,303
  • 21
  • 270
  • 254
4

Just if someone is interested:

C11 §6.4.4.1/5:

The type of an integer constant is the first of the corresponding list in which its value can be represented.

---------------------------------------------------------------------------
Suffix        Decimal Constant              Octal/Hexadecimal Constant
---------------------------------------------------------------------------
none          int                           int
              long int                      unsigned int
              long long int                 unsigned long int
                                            long long int
                                            unsigned long long int
---------------------------------------------------------------------------
u or U        unsigned int                  unsigned int
              unsigned long int             unsigned long int
              unsigned long long int        unsigned long long int
---------------------------------------------------------------------------
l or L        long int                      long int
              long long int                 unsigned long int
                                            long long int
                                            unsigned long long int
---------------------------------------------------------------------------
Both u or U   unsigned long int             unsigned long int
and l or L    unsigned long long int        unsigned long long int
---------------------------------------------------------------------------
ll or LL      long long int                 long long int
                                            unsigend long long int
---------------------------------------------------------------------------
Both u or U   unsigned long long int        unsigned long long int
and ll or LL
---------------------------------------------------------------------------

As for the prefix §6.4.4.1/3:

A decimal constant begins with a nonzero digit and consists of a sequence of decimal digits. An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only. A hexadecimal constant consists of the prefix 0x or 0X followed by a sequence of the decimal digits and the letters a (or A) through f (or F) with values 10 through 15 respectively.

legends2k
  • 31,634
  • 25
  • 118
  • 222
2

There are three types of integer literals(or integer constants in the standards terminology): decimal, octal or hex and the rule are slightly different for your specific example 6 would be int but in general for decimal constants without a suffix(u, U, l, L, ll, LL) it will be based on which type can represent the value which is covered in the draft C99 standard section 6.4.4.1 Integer constants paragraph 5 which says:

The type of an integer literal is the first of the corresponding list in which its value can be represented.

so for a decimal literal without a suffix the types would be the first of:

  • int
  • long int
  • long long int

and for octal and hex the types would be the first of:

  • int
  • unsigned int
  • long int
  • unsigned long int
  • long long int
  • unsigned long long int
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740