0% found this document useful (0 votes)
13 views7 pages

3 - Variables, Constants

Uploaded by

saisudhiraicte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

3 - Variables, Constants

Uploaded by

saisudhiraicte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Variables, constants

Variables
• A variable in C is a memory location with some name that helps store some
form of data and retrieves it when required. We can store different types of
data in the variable and reuse the same variable for storing some other data
any number of times.

• There are many types of variables in C depending on the scope, storage class,
lifetime, type of data they store, etc. A variable is the basic building block of a
C program that can be used in expressions as a substitute in place of the value
it stores.
• They can be viewed as the names given to the memory location so that we can
refer to it without having to memorize the memory address. The size of the
variable depends upon the data type it stores.
C Variable Syntax
• The syntax to declare a variable in C specifies the name and the type of the
variable.
• data_type variable_name = value; // defining single variable
or
• data_type variable_name1, variable_name2; // defining multiple variable

• Here,

• data_type: Type of data that a variable can store.


• variable_name: Name of the variable given by the user.
• value: value assigned to the variable by the user.
• There are 3 aspects of defining a variable:

• Variable Declaration
• Variable declaration in C tells the compiler about the existence of the variable with the
given name and data type.When the variable is declared, an entry in symbol table is
created and memory will be allocated at the time of initialization of the variable.
• Variable Definition
• In the definition of a C variable, the compiler allocates some memory and some value to
it. A defined variable will contain some random garbage value till it is not initialized.
• Variable Initialization
• Initialization of a variable is the process where the user assigns some meaningful value to
the variable when creating the variable.
constants
• Constant in C is a variable that cannot be modified once it is declared
in the program. We can not make any change in the value of the
constant variables after they are defined
How to Define Constant in C?

• We define a constant in C language using the const keyword. Also


known as a const type qualifier, the const keyword is placed at the
start of the variable declaration to declare that variable as a constant.

• const data_type var_name = value;


Constant Variables
A constant is a variable or value that cannot be altered once A variable is a name associated with some memory location.
defined.

A constant is used to hold the fixed values which we can A variable is used to hold some value that can be changed
retrieve later but cannot change. according to the requirement.

The constants are generally stored in the text segment as they The variables are stored inside a data segment, heap, or stack
are read-only depending on the environment it is declared in.

We can only assign a value to the constant while defining it. We can assign value to the variable anytime.

A constant can be defined by using #define or const keyword. A variable can only be defined using the standard variable
definition syntax.

Example: #define pi 3.14 Example: int var = 25;


const int pi = 3.14; var = 10;

You might also like