Index
What is Variable
In programming, a variable is a container that holds a value or data. The value or data can be changed or modified throughout the program. Variables are an important concept in programming, as they allow us to store and manipulate data in a flexible way.
Here’s a an Analogy to understand it better.
- Box: A box is a container that can hold various items. Similarly, a variable is a container that can hold different types of data, such as numbers, strings, or Boolean values.
- Refrigerator: A refrigerator can hold different types of food items, and the items can be added or removed as needed. Similarly, a variable can hold different types of data, and the data can be modified or updated as needed throughout the program.
What is Data type
In C programming, a data type specifies the type of data that can be stored in a variable. It determines the range of values that the variable can hold and the operations that can be performed on the variable.
For example, the integer data type can hold whole numbers (positive, negative, or zero) and can perform arithmetic operations like addition, subtraction, multiplication, and division. The character data type can hold a single character, such as a letter or symbol, and can perform operations like comparison. However, it can also be used to hold integer values between -128 to 127 (or 0 to 255 if unsigned).
In C programming, there are several built-in data types, including:
Type | Size(Bytes) | Format specifier |
int | 2 | %d, %i |
char | 1 | %c |
float | 4 | %f |
double | 8 | %lf |
short int | 2 | %hd |
unsigned int | 2 | %u |
long int | 8 | %ld, %li |
unsigned long int | 4 | %lu |
unsigned char | 1 | %c |
The size of data types depend on the compiler, use sizeof(data_type) to know the size of data type
int
Used to store integers (whole numbers) with a limited range, typically between -32,768 and 32,767.
we can use int for declaring an integer variables
int a //declaring variable a of type int
float: Used to store floating-point numbers (numbers with a decimal point), with limited precision.
char: Used to store a single character, such as a letter or symbol.
double: Similar to float, but with higher precision.
bool: Used to store boolean values (true or false).
When declaring a variable in C programming, you need to specify the data type of the variable. For example, if you want to declare an integer variable named “age”, you would write: