C Variables And Constants


In a C program, some values do not change and some changes with the execution of the program. A constant never changes throughout the program and the variable changes frequently, hence the name.

Types of Constants

Constants can be classified into different types such as numeric constants and character constants. The following figure depicts the types of constants available with C language.

Types of Constants in C language
Types of Constants in C language

Numeric Constants And Character Constants

The numeric constants involve numbers such as integers and real numbers with decimal part – floating point number and double numbers are most popular types.

The character types are single character constant enclosed using single quotes and string constants are enclosed in double quotes. You can call string constant as a character array. For example,

/* Character constants */
/* String constants */

Named Constants

Some constant can be named in C programs. This is achieved with the help of C preprocessor commands. The C preprocessor will turn these constants in to values before execution of a program.

#define MAX 100

The command is #define and the name of the constant is MAX with a value of 100.

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 key = 'B';

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

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.



  • 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.


Effective C: An Introduction to Professional C Programming

A detailed introduction to the C programming language for experienced programmers.

The world runs on code written in the C programming language, yet most schools begin the curriculum with Python or Java. Effective C bridges this gap and brings C into the modern era–covering the modern C17

You’ll learn:

  • How to identify and handle undefined behavior in a C program
  • The range and representations of integers and floating-point values
  • How dynamic memory allocation works and how to use nonstandard functions
  • How to use character encodings and types
  • How to perform I/O with terminals and filesystems using C Standard streams and POSIX file descriptors
  • How to understand the C compiler’s translation phases and the role of the preprocessor
  • How to test, debug, and analyze C programs

Effective C will teach you how to write professional, secure, and portable C code that will stand the test of time and help strengthen the foundation of the computing world.