C Programming Constants and Variables

In this tutorial, you will learn about variables, rules for naming a variable, constants and different type of constants in C programming.

Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:

int playerScore = 95;

Here, playerScore is a variable of integer type. Here, the variable is assigned an integer value 95.

The value of a variable can be changed, hence the name variable.

char ch = 'a';
// some code
ch = 'l';

Rules for naming a variable

  1. A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
  2. The first letter of a variable should be either a letter or an underscore.
  3. There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if variable name is longer than 31 characters.

Note: You should always try to give meaningful names to variables. For example: firstName is a better variable name than fn.

C is a strongly typed language. This means, variable type cannot be changed once it is declared. For example:

int number = 5;      // integer variable
number = 5.5;        // error
double number;       // error

Here, the type of number variable is int. You cannot assign floating-point (decimal) value 5.5 to this variable. Also, you cannot redefine the type of the variable to double. By the way, to store decimal values in C, you need to declare its type to either double or float.

Visit this page to learn more about different types of data a variable can store.


Constants/Literals

A constant is a value (or an identifier) whose value cannot be altered in a program. For example: 1, 2.5, 'c' etc.

Here, 1, 2.5 and 'c'are literal constants. Why? You cannot assign different values to these terms.

You can also create non-modifiable variables in C programming. For example:

const double PI = 3.14;

Notice, we have added keyword const.

Here, PI is a symbolic constant. It's actually a variable however, it's value cannot be changed.

const double PI = 3.14;
PI = 2.9;         //Error

Below are the different types of constants you can use in C.


1. Integers

An integer is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:

  • decimal constant(base 10)
  • octal constant(base 8)
  • hexadecimal constant(base 16)

For example:

Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

In C programming, octal starts with a 0, and hexadecimal starts with a 0x.


2. Floating-point constants

A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:

-2.0
0.0000234
-0.22E-5

Note: E-5 = 10-5


3. Character constants

A character constant is created by enclosing a single character inside single quotation marks. For example: 'a''m', 'F', '2', '}' etc;


4. Escape Sequences

Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used.

For example: \n is used for newline. The backslash \ causes escape from the normal way the characters are handled by the compiler.

Escape Sequences
Escape Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character

5. String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:

"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline

6. Enumerations

Keyword enum is used to define enumeration types. For example:

enum color {yellow, green, black, white};

Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively. For more information, visit page: C Enumeration.


You can also define symbolic constants using #define. To learn more, visit: C Macros