C Programming Tutorial
About Lesson

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.


Variables

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 :

  1. Type : refers to the size and type of data is going to store in the variable.
  2. Variable : refers to the unique name given to the variable to represent it in program.
  3. 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.

Example :

int steps = 357;

In the above example, steps is a variable of int type having an integer value 357.

We can also replace/change the data of a variable.

Example :

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

  1. A variable name can only contain letters (both uppercase and lowercase letters), digits (0 – 9) and underscore (_).
  2. A variable name must begin with letter (both uppercase and lowercase letters) or underscore(_).
  3. No special symbols are allowed other than underscore.
  4. We cannot use keywords like intfloatwhilefor as keywords .
  5. 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 understand for others.

We cannot change the type of variable type once it is declared.

Example :

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

Literals are the value/data assigned to each constant variable. We can use var literals directly in the code. For example: 7845.9‘A’ etc.

There are four types of literals in C programming :

  1. Integer literal
  2. Float literal
  3. Character literal
  4. String literal

Note : Literals are constant values assigned to the variable not constant variable.


Integer literals

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)

Example :

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

Floating-point literals are used to represent and store real numbers. A real number is specified either using decimal or using exponential notation.

Example :

4.5, -2.3451, 0.12e-3

Character literals

character literals are used to represent and store a character inside single quotes.

Example :

'x', 'M', '4', ','

String Literals

A string literal are used to represent and store a sequence of character enclosed in double quotes.

Example :

"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)

Constants

Constants are those variables whose value cannot be changed.
There are two ways to define constants in C :

  1. #define preprocessor
  2. const keyword

#define Preprocessor

We can declare constant in C using #define preprocessor in the header of the C program.

Syntax :

#define identifier value

Example :

#define Length 10

Const Keyword

The const keyword is used to declare constant with a specific type.

Syntax :

const type variable value;

Example :

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

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!