3 - Variables, Constants
3 - 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,
• 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?
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.