your resources to programming and free tutition


C++ soon here!
VB - soon here!






Next Back


This lesson will teach you how to:

  • Initialize variables
  • Use arithmetic operators

Q. What's the difference between a lawyer and a gigolo?
A. A gigolo only screws one person at a time.

Initializing data variables at declaration time

    Unlike PASCAL, in C variables may be initialized with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialized to 10:

int count = 10;

Simple assignment of values to variables

The = operator is used to assign values to data variables. Consider the following statement, which assigns the value 32 an integer variable count, and the letter A to the character variable letter :

count = 32;
letter = ‘A’;

The value of variables at declaration time

Lets examine what the default value a variable is assigned when its declared. To do this, lets consider the following program, which declares two variables, count which is an integer, and letter which is a character. Neither variable is pre-initialized. The value of each variable is printed out using a printf() statement.

#include <stdio.h>

int main(void)

    int count;
    char letter;

    printf(“Count = %d\n”, count);
    printf(“Letter = %c\n”, letter);

    return 0;


Sample program output

count = 26494
letter = f

It can be seen from the sample output that the values which each of the variables take on declaration time are non-zero. In C, this is common, and programmers must ensure that variables are assigned values before using them. If the program runs again, the output could well have different values for each of the variables. We can never assume that variables declare in the manner above will take on specific values. Some compilers may issue warnings related to the use of variables, and Turbo C from Borland issues the following warning:

    “possible use of ‘count’ before definition in function main

Radix changing

Data umbers may be expressed in any base by simply altering the modifier, e.g. decimal, octal, or hexadecimal. This is achieved by the letter which follows the % sign related to the printf() argument.

#include <stdio.h>

int main(void)           /* Prints the same value in decimal, hex, and octal */

     int number = 100;

    printf(“In decimal the number is %d\n”, number);
    printf(“In hex the number is %x\n”, number);
    printf(“In octal the number is %o\n”, number);
                        /* what about %X as an argument? */

    return 0;


Sample program output

In decimal the number is 100
In hex the number is 64
In octal the number is 144

Note how the variable number is initialized to one hundred at the time of its declaration.

Defining variables in octal and hexadecimal

Often, when writing systems programs, the programmer needs to use a different number base rather than the default decimal. Integer constants can be defined in octal or hex by using the associated prefix, e.g. to define an integer as an octal constant use %o

int sum = %o567;

To define an integer as a hex constant use %Ox

int sum = %Ox7ab4;
int flag = %Ox7AB4;
           /* Notice upper or lower case are not the same variables */

Arithmetic Operators:

The symbols of the arithmetic operators are:

C symbol Example Priority Function
() (a + b) / c 1 Parenthesized operation
* a * b 2 Multiplication
/ a / b 2 Division
% a % b 2 Modulus
+ a + b 3 Addition
- a - b 3 Subtraction
= a = b 4 Assignment

If a line has an expression which contains several operators of equal priority, C will evaluate if from left to right. Note that in C, the equal (=) sign is a replacement operator. This means that the expressions such as a = b = c are permissible, but order of evaluation is now from right to left, so that c would be assigned to b, which in turn would be assigned to a, giving all three variables the same value, namely that of c.

The following code fragment adds the variables loop and count together, leaving in the variable sum:

sum = loop + count;

Note: If modulus % sign is needed to be displayed as part of the text string, use two % signs, e.g. %%.

#include <stdio.h>

int main(void)

    int sum = 50;
    flat modulus;

    modulus = sum % 10;
    printf(“The %% of %d by 10 is %f\n”, sum, modulus);

    return 0;


Sample program output

The % of 50 by 10 is 0.000000

back to top          exercises         lesson 3          lesson 5