Like HowStuffWorks on Facebook!

The Basics of C Programming

        Tech | Programming

More on Arrays

Variable Types

There are three standard variable types in C:

  • Integer: int
  • Floating point: float
  • Character: char

An int is a 4-byte integer value. A float is a 4-byte floating point value. A char is a 1-byte single character (like "a" or "3"). A string is declared as an array of characters.

There are a number of derivative types:

  • double (8-byte floating point value)
  • short (2-byte integer)
  • unsigned short or unsigned int (positive integers, no sign bit)

Operators and Operator Precedence

The operators in C are similar to the operators in most languages:

+ - addition
- - subtraction
/ - division
* - multiplication
% - mod

The / operator performs integer division if both operands are integers, and performs floating point division otherwise. For example:

void main()
    float a;

This code prints out a floating point value since a is declared as type float, but a will be 3.0 because the code performed an integer division.

Operator precedence in C is also similar to that in most other languages. Division and multiplication occur first, then addition and subtraction. The result of the calculation 5+3*4 is 17, not 32, because the * operator has higher precedence than + in C. You can use parentheses to change the normal precedence ordering: (5+3)*4 is 32. The 5+3 is evaluated first because it is in parentheses. We'll get into precedence later -- it becomes somewhat complicated in C once pointers are introduced.


C allows you to perform type conversions on the fly. You do this especially often when using pointers. Typecasting also occurs during the assignment operation for certain types. For example, in the code above, the integer value was automatically converted to a float.

You do typecasting in C by placing the type name in parentheses and putting it in front of the value you want to change. Thus, in the above code, replacing the line a=10/3; with a=(float)10/3; produces 3.33333 as the result because 10 is converted to a floating point value before the division.


You declare named, user-defined types in C with the typedef statement. The following example shows a type that appears often in C code:

#define TRUE  1
#define FALSE 0
typedef int boolean;

void main()
    boolean b;

    blah blah blah

This code allows you to declare Boolean types in C programs.

If you do not like the word "float'' for real numbers, you can say:

typedef float real;

and then later say:

real r1,r2,r3;

You can place typedef statements anywhere in a C program as long as they come prior to their first use in the code.


Structures in C allow you to group variable into a package. Here's an example:

struct rec
    int a,b,c;
    float d,e,f;

struct rec r;

As shown here, whenever you want to declare structures of the type rec, you have to say struct rec. This line is very easy to forget, and you get many compiler errors because you absent-mindedly leave out the struct. You can compress the code into the form:

struct rec
    int a,b,c;
    float d,e,f;
} r;

where the type declaration for rec and the variable r are declared in the same statement. Or you can create a typedef statement for the structure name. For example, if you do not like saying struct rec r every time you want to declare a record, you can say:

typedef struct rec rec_type;

and then declare records of type rec_type by saying:

rec_type r;

You access fields of structure using a period, for example, r.a=5;.


You declare arrays by inserting an array size after a normal declaration, as shown below:

int a[10];        /* array of integers */
char s[100];      /* array of characters
                    (a C string) */
float f[20];      /* array of reals */
struct rec r[50]; /* array of records */


Long Way     Short Way
i=i+1;       i++;
i=i-1;       i--;
i=i+3;       i += 3;
i=i*j;       i *= j;