C Programming Tutorial – Variables and Constants

The variables and constants are the building blocks of a C program. They are used in expressions, read as user input values, help to compare values in conditional statements etc. We can build complex C programs using variables and constants.

You will learn about constants and variables, types of constants available in C, rules to construct constants and variables, declaration and definition of variables in c and an example program to practice. The Example program is written using Dev-C++ compiler version installed on a Windows 7 64-bit system.


Constants are those program elements that do not change during the execution of a program. That’s why they are called constants.

A constant can be any one of the following.


This can be A - Z or a - z.


Any digit or digits between 0 - 9.

Special Character

All kinds of special character symbols on keyboard - @, #, $, %, ^, &, *, (, ), !, etc.

Types of Constants

There are three types of constants used in C programming.

  1. Integer constants
  2. Real constants
  3. Character constants

Numeric constants like integers and real constants can be positive or negative. When no symbol attached to it, then it is a positive number.

The real constant contains an integer part and a decimal part.

For example, 4.1, -3.5, 334.44, -443.4434 

The character constants are enclosed in single quotes. Even digits that are enclosed in single quotes are considered as a character.

for example,  ‘b’, ‘H’, ‘5’, ’33’ etc.


Variables are those program elements that change during the execution of a C program. They are the variable elements, not fixed elements like constants. All values in a program are stored in memory locations. Each memory location is like a small container that holds some value. A variable is a name for this container, whose value changes frequently during the execution of a C program.

The following figure shows memory locations and a value of 10 stored at location 0x5000. This memory location has a name – Number.

Variables in Memory

Variables in Memory

The Number, in this case, s called a variable. You can always refer to the variable by its name, not by its value because the value changes continuously.

Rules to Construct Constants and Variables

There are rules to construct constants and variables.

Rules for Constants

You can use a constant directly into an expression in C programs. C programming language will allow only specific operation of a type constant.

For example, you can perform arithmetic operations on numbers, but not on characters.

Rules for Variables

There are important rules to remember before you create a variable. These rules are listed below.

Rule 1: Variable names always start with an underscore or a character.

Rule 2: No other special character allowed in variable names except an underscore.

Rule 3: No white space or a comma allowed in a variable name.

Rule 4: Variable name length depends on the compiler.

This is not a rule, but a best practice – always use meaningful variable names in your program.

Syntax for Constants and Variables

There is no specific syntax for a constant, you can use them directly in your programs.

For example,

A = 10 + 30;

printf ( "%d\n", 100 + 30);

Every variable must be declared and initialized before you use it in a C program. The syntax to declare a variable requires

the type of data for the variable.

a user-defined name for the variable.

For example,

int number;
char grade;

For example,

number = 100;
grade = ‘A’;

You can initialize a variable at the time of declaration itself.

For example,

int number = 200;
char grade = 'B';

Note:- Every statement in C programming language must end with a semi-colon;

An Example Program

#include <stdio.h>

int main() {

    int number;
    float sum;

/* Variable Declaration with data type */

    sum = 0.0;
    number = 1000;

/* Variable initialization for sum and number */

    printf(“ %d\n”, number);

/* Printing output to console */

    printf(“ %d\n”, 10.23 + 3.7);

    return 0;



The output of the above example program is given below.

Output- Variables and Constants

Output- Variables and Constants


Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.