Variables in C programming



Definition of Variables

A variable is a named memory location in which data of a certain type can be stored. The contents of a variable can change , thus the name. User defined variables must be declared before they can be used in a program. It is during the declaration phase that the actual memory for the variable is reserved. .

Variables Rule

All variables in C must be declared before use.Get into the habit of declaring variables using lowercase characters.

Remember that C is case sensitive, so even though the two variables listed below have the same name, they are considered different variables in C.


 sum is different than Sum

The declaration of variables is done after the opening brace of main().


    main() {
    int sum;

It is possible to declare variables elsewhere in a program, but lets start simply and then get into variations later on.

The basic format for declaring variables is :


    data_type var, var ;

Where data_type is one of the four basic types, an integer, character, float, or double type .


    int i,j,k;
    float length,height;
    char midinit;

Constant qualifiers

Constant qualifiers can be declared with keyword const. An object declared by const cannot be modified. The value of p cannot be changed in the program.


    const int p=20;

Data Types

Data types are the keywords, which are used for assigning a type to a variable.


    Fundamental Data Types
    a. Integer types
    b. Floating Type
    c. Character types


    Derived Data Types
    a. Arrays
    b. Pointers
    c. Structures
    d. Enumeration

a. Integer Variables

These are whole numbers, both positive and negative. Unsigned integers(positive values only) are also supported. In addition, there are short and long integers.


    int age;
    short int age;
    long int age;

b. Float Variables

These are numbers which contain fractional parts, both positive and negative, and can be written in scientific notation.


    float x;

c. Double Variables

These are floating point numbers, both positive and negative, which have a higher precision than float variables.


    double a, _b;

d. Character Variable

These are single characters.Typical character values might be the letter A, the character 5, the symbol “, etc.


    char letter;
    char 5;

Initializing Variables

The following example illustrates the two methods for variable initialization for integer, double , float and char variables.


    #include <stdio.h>
    main () {
    int sum=33;
    float money=44.12;
    char letter;
    double pressure;
    letter='E';         // assign character value 
    pressure=2.01e-10;  // assign double value 
    printf("value of sum is %d\n",sum);
    printf("value of money is %f\n",money);
    printf("value of letter is %c\n",letter);
    printf("value of pressure is %e\n",pressure);
    }

    Output: 
    value of sum is 33
    value of money is 44.119999
    value of letter is E
    value of pressure is 2.010000e-10

PROMOTIONS