0% found this document useful (0 votes)
221 views5 pages

Variable and Constant: Variables in C

Variables in C are used to store and retrieve data during a program's execution. They require memory allocation and their type determines the amount of memory used. Variables can be declared using data types like int, float, char, etc and can consist of letters, numbers, and underscores but must not start with a number. Constants are variables that cannot be modified once defined - they hold fixed values throughout the program's lifetime. Constants are declared using const or #define and can only be assigned values during declaration. The main differences between variables and constants are that variables can be modified during runtime while constants remain fixed, and variables are stored in memory segments while constants are stored in the text segment as read-only values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views5 pages

Variable and Constant: Variables in C

Variables in C are used to store and retrieve data during a program's execution. They require memory allocation and their type determines the amount of memory used. Variables can be declared using data types like int, float, char, etc and can consist of letters, numbers, and underscores but must not start with a number. Constants are variables that cannot be modified once defined - they hold fixed values throughout the program's lifetime. Constants are declared using const or #define and can only be assigned values during declaration. The main differences between variables and constants are that variables can be modified during runtime while constants remain fixed, and variables are stored in memory segments while constants are stored in the text segment as read-only values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VARIABLE AND CONSTANT

Variables in C
A variable in simple terms is a storage place that has some memory
allocated to it. It is used to store some form of data and retrieve it when
required. Different types of variables require different amounts of memory
and have some specific set of operations that can be applied to them.

C Variable Declaration
• type variable_name;
• type variable1_name, variable2_name, variable3_name;

• A variable name can consist of alphabets (both upper and lower case),
numbers, and the underscore ‘_’ character. However, the name must not
start with a number.
Example of C Variable
#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';
// This is also both declaration
// and definition as 'b' is allocated
// memory and assigned some garbage value.
float b;
// multiple declarations and definitions
int _c, _d45, e;
// Let us print a variable
printf("%c \n", a123);
return 0;
}

Output:
a
Constants in C
• The constants are those variables or values in the C which
cannot be modified once they are defined in the program.
• They have fixed values till the program’s life.
• We can only assign value to the constant in the declaration.
• There can be any type of constant like integer, float, octal,
hexadecimal, character constants, etc.
Example of C Constant
#include <stdio.h>
// Constants Macro
#define val 10
// Driver code
int main()
{
// constant variables
const float floatVal = 5.8;
const char charVal = 'a';
// printing constants
printf("Integer Constant: %d\n", val);
printf("Floating point Constant: %f\n", floatVal);
printf("Character Constant: %c\n", charVal);
return 0;
}
Output

Integer Constant: 10
Floating point Constant: 5.800000
Character Constant: a
Difference between Constant and Variables

Constant Variables

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

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

The constants are generally stored in the text segment as they are read- The variables are stored inside a data segment, heap, or stack depending
only 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 variable can only be defined using the standard variable definition


A constant can be defined by using #define or const keyword.
syntax.

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


const int pi = 3.14; var = 10;

You might also like