Editor choice

Variables and Constants In C Programming Language

What is a Variable?

A variable is an identifier which is used to store some value. Constants can never change at the time of execution. Variables can change during the execution of a program and update the value stored inside it.

A single variable can be used at multiple locations in a program. A variable name must be meaningful. It should represent the purpose of the variable.

Example: Height, age, are the meaningful variables that represent the purpose it is being used for. Height variable can be used to store a height value. Age variable can be used to store the age of a person

A variable must be declared first before it is used somewhere inside the program. A variable name is formed using characters, digits and an underscore.

Following are the rules that must be followed while creating a variable:

  1. variable name should consist of only characters, digits and an underscore.
  2. In the variable name should not begin with a number.
  3. The variable name should not consist of whitespace.
  4. A variable name should not consist of a keyword.
  5. ‘C’ is a case sensitive language that means a variable named ‘age’ and ‘AGE’ are different.

Following are the examples of valid variable names in a ‘C’ program:

height or HEIGHT
_height
_height1
My_name

Following are the examples of invalid variable names in a ‘C’ program:

1height
Hei$ght
My name

For example, we declare an integer variable my_variable and assign it the value 48:

int my_variable;
my_variable = 48;

By the way, we can both declare and initialize (assign an initial value) a variable in a single statement:

int my_variable = 48;

Constants

Constants are the fixed values that never change during the execution of a program. Following are the various types of constants:

Integer Constants

An integer constant is nothing but a value consisting of digits or numbers. These values never change during the execution of a program. Integer constants can be octal, decimal and hexadecimal.

  1. Decimal constant contains digits from 0-9 .
  2. Octal constant contains digits from 0-7, and these types of constants are always preceded by 0.
  3. Hexadecimal constant contains a digit from 0-9 as well as characters from A-F. Hexadecimal constants are always preceded by 0X.

E.g of Constants are :

Example, 111, 1234
Example, 012, 065
Example, 0X2, 0Xbcd

Character Constant

A character constant contains only a single character enclosed within a single quote (”). We can also represent character constant by providing ASCII value of it.

Example, 'A', '9'

String Constant

A string constant contains a sequence of characters enclosed within double quotes (” “).

Example, "Hello", "Programming"

Real Constant

Like integer constants that always contains an integer value. ‘C’ also provides real constants that contain a decimal point or a fraction value. The real constants are also called as floating point constants. The real constant contains a decimal point and a fractional value.

Example, 202.15, 300.00
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!๐Ÿ‘‹
Can we help you?