C Variables & Constants
In this tutorial, you will learn about variables rules for naming a variable. You will also learn about different literals in C programming and how to create constants.
A variable is the name given to the memory allocation (a storage area where the data stored). Variables are the symbolic representation to indicate a memory allocation.
A variable is consist of three parts :
- Type : refers to the size and type of data is going to store in the variable.
- Variable : refers to the unique name given to the variable to represent it in program.
- Value : refers to the data store in the variable.
Note : Value is optional while declaring a variable in a program. We can assign data later, according to our needs.
int steps = 357;
In the above example, steps is a variable of
int type having an integer value
We can also replace/change the data of a variable.
int steps = 357;
steps = 500;
In the above example, the value of variable steps is changed from 357 to 500.
Rules for naming a variable
- A variable name can only contain letters (both uppercase and lowercase letters), digits (0 - 9) and underscore (_).
- A variable name must begin with letter (both uppercase and lowercase letters) or underscore(_).
- No special symbols are allowed other than underscore.
- We cannot use keywords like int, float, while, for as keywords .
- Variables are case-sensitive in C. Example - star and Star are two different variables in C.
Note : Always choose meaningful names for variables that make sense to make it easy to unnderstand for others.
We cannot change the type of variable type once it is declared.
int percentage = 80; // integer variable
number = 85.5; // error
float number; // error
In the above example, the type of percentage variable is
int. We cannot assign a floating-point (decimal) value 85.5 to variable percentage. Also, We cannot redefine the data type of the variable to
float. That's why we get an error in both the second and third lines of the example.
To know more about different types of data a variable can store .
Literals are the value/data assigned to each constant variable. We can use var literals directly in the code. For example: 78, 45.9, 'A' etc.
There are four types of literals in C programming :
- Integer literal
- Float literal
- Character literal
- String literal
Note : Literals are constant values assigned to the variable not constant variable.
Integer literals are used to represent and store the integer values. There are three types of integer literals in C programming:
- decimal number (base 10)
- octal number (base 8)
- hexadecimal number (base 16)
Decimal number : 0, -10, 12 etc
Octal number : 03274, 0374, 02990008 etc
Hexadecimal number : 0x7b0, 0x5f, 0x671 etc
In C programming, octal starts with a 0 followed by digits between 0-7, and hexadecimal starts with a 0x or 0X followed by heaxadecimal characters (i.e. digits from 0-9 and characters from a-f ).
Floating-point literals are used to represent and store real numbers. A real number is specified either using decimal or using exponential notation.
4.5, -2.3451, 0.12e-3
character literals are used to represent and store a character inside single quotes.
'x', 'M', '4', ','
A string literal are used to represent and store a sequence of character enclosed in double quotes.
"computer" //string constant
"" //null string constant
" " //string constant of six white space
"x" //string constant having a single character
"Break rules\n" //prints string with a newline (line-break)
Contants are those variables whose value cannot be changed.
There are two ways to define constants in C :
- #define preprocessor
- const keyword
We can declare constant in C using #define preprocessor in the header of the C program.
The const keyword is used to declare constant with a specific type.
type variable value;
double PI = 3.14;
PI = 11.39; //Error
We have to add keyword const before the type of variable to make it a constant.
In the above example, we have declare a variable PI, it's value cannot be changed.
We hope that this tutorial helped you develop better understanding of the concept of Variables & Literals in C.
Keep Learning : )
In the next tutorial, you'll learn about C