Skip to main content
Engineering LibreTexts

3.2: Variable Naming, Types and Declaration

  • Page ID
    35804
  • Variable naming is fairly simple. Variable names are a combination of letters, numerals, and the underscore. Upper and lower case can be mixed and the length is typically 31 characters max, but the actual limit depends on the C compiler in use. Further, the variable name cannot be a reserved (key) word nor can it contain special characters such as . ; , * - and so on. So, legal names include things like x, volts, resistor7, or even I_Wanna_Go_Home_Now.

    C supports a handful of variable types. These include floating point or real numbers in two basic flavors: float, which is a 32 bit number, and double, which is a higher precision version using 64 bits. There are also a few integer types including char, which is 8 bits, short int, which is 16 bits, and long int, which is 32 bits. As char is 8 bits, it can hold 2 to the 8th combinations, or 256 different values. This is sufficient for a single ASCII character, hence the name. Similarly, a short int (or short, for short!) can hold 2 to the 16th combinations, or 65,536 values. chars and ints may be signed or unsigned (signed, allowing negative values, is the default). There is also a plain old int, which might be either 16 or 32 bits, depending on which is most efficient for the compiler (to be on the safe side, never use plain old int if the value might require more than 16 bits).

    Sometimes you might also come across special double long integers (also called long longs) that take up 8 bytes as well as 80 bit extended precision floats (as defined by the IEEE).

    Here is a table to summarize the sizes and ranges of variables:

    Table \(\PageIndex{1}\): Numeric types and ranges.
    Variable Type Bytes Used Minimum Maximum

    char

    1

    −128

    127

    unsigned char

    1 0

    255

    short int

    2

    −32768

    32767

    unsigned short int

    2 0

    65535

    long int

    4

    ≈ −2 billion

    ≈ 2 billion

    unsigned long int

    4 0

    ≈ 4 billion

    float
    (6 significant digits)

    4

    \(\pm\) 1.2 E −38

    \(\pm\) 3.4 E +38

    double
    (15 significant digits)

    8

    \(\pm\) 2.3 E −308

    \(\pm\) 1.7 E +308

    C also supports arrays and compound data types. We shall examine these in a later segment.

    Variables must be declared before they are used. They cannot be created on a whim, so to speak, as they are in Python. A declaration consists of the variable type followed by the variable name, and optionally, an initial value. Multiple declarations are allowed. Here are some examples:

    char x;              // declares a signed 8 bit integer called x
    unsigned char y;     // declares an unsigned 8 bit integer called y
    short z, a;          // declares two signed 16 bit integers named z and a
    float b =1.0;        // declares a real number named b and sets its initial value to 1.0

    Note that each of these declarations is followed with a semi-colon. The semi-colon is the C language way of saying “This statement ends here”. This means that you can be a little sloppy (or unique) in your way of dealing with spaces. The following are all equivalent and legal:

    float b = 1.0;
    float b=1.0;
    float b = 1.0 ;
    • Was this article helpful?