C Programming/Types

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

In C, there is a small key set of types. The rest are variations, modifiers, arrays and other such low-level data structures. The keywords for these types are int, char and float.

Contents

[edit] Types in C

[edit] Integer types

When we wish to store an integer, we use an integer type, usually int. ints have at least 16 bits. Many platforms have 32-bit ints. There is also long int, which is guaranteed to have at least 32 bits. short ints have at most 16 bits, just like regular ints. However, often int is made to be longer, so short int is used when conserving memory is important.

All these integer types are signed, meaning that they can hold both positive and negative values. There is an corresponding unsigned type that only holds nonnegative values. However, they can hold larger values in the same number of bits. Using the keyword unsigned yields an unsigned type. For example, unsigned int is a unsigned integer type of at least 16 bits. The keyword signed can also be used, but is redundant.

When conserving memory is even more important, we can pack an assortment of 1-bit, 2-bit, and 3-bit values into a structure using bit_fields. (Or we can use the bit-manipulation instructions to manually pack and unpack bit fields from a standard "char" or "int".)

[edit] Character types

When we wish to store a single character or byte, we use a character type. The C name for this type is called char. chars always use one byte each. char can also be used to store integers small enough to fit in a byte.

Since char is an integer type, it is important to know whether it is signed or unsigned. However, compilers are allowed to make them either. If you want to guarantee that your character variable is signed or unsigned, use the type signed char or unsigned char. Unlike char, these types are guaranteed to be either signed or unsigned, according to their names.

[edit] Floating-point types

When we wish to store a floating-point (non-integer) value, we can use the types float, double, and long double. These differ only in the precision and range that they provide. A float generally has 32 bits and a double 64. long double might be bigger, but it could just be the same as double.

[edit] Void type

There is a special type called void. You are not allowed to have variables of type void. There are various uses of void, one of which is as a return type for functions that don't need to return an actual value. void did not exist before it was introduced in the 1989 ANSI C standard. However, there should be no need to worry about its not being available unless you use a really old compiler (in which case, upgrade).

There is no fundamental predefined string type in C. Strings in C are more complicated (they are arrays of chars), requiring manual handling, and we will deal with them later.

[edit] Declaring variables

All type declarations using the above simple types follow a standard format

type_name variable_name;

where type_name represents one of the C type names, and variable_name would be replaced by the programmer with a name for the variable. According to the C standards there are a few restrictions on variable names. A variable name:

  • must be at least one character long
  • must be less than some maximum character length. 32 on some systems, 256 on others, and probably everything in between on yet others.
  • must start with a letter
  • must be composed of letters, numbers and/or the underscore character
  • must not contain spaces
  • must not be equal to reserved words such as "int", "char", "float", et cetera. Any keyword used for the C language itself is off-limits.
  • is case sensitive. This means that "NUMBER", "number", "Number", "numbeR", and "NuMbEr" are all different variables.

For example, if we want to declare a variable called number and of type int, we write

int number;

In C, all declarations are terminated by semicolons just as if they were statements. If we wish to declare a number of variables of the same type, we can write a comma separated list of variable names after the type. For example:

int variable1, variable2, variable3;

which declares all variable1, variable2, and variable3 to be of type integer.

[edit] enum


Some C programmers use enum to declare an enumerated type.

These enumerated types are useful when we build switch case statements.

[edit] Related articles

Back to contents: C programming

[edit] Concepts

[edit] In this section