Data Types & Variables

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.

  1. 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.
  2. 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:

TypeSize(Bytes)Format specifier
int 4%d, %i
char 1%c
float 4%f
double 8%lf
short int 2%hd
long int 8%ld, %li
unsigned int 4%u
unsigned long int 8%lu
unsigned char 1%c
DataTypes

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 range, typically between -2147483648 and 2147483647. we can use int for declaring an integer variables

int a; //declaring variable a of type int
int a,b; //Multiple declaration at once

float and double: Used to store floating-point numbers (numbers with a decimal point), with limited precision.

float salary; //declaring variable of type float
double price; //declaring variable of type float

price = 32.50

double is Similar to float, but with higher precision.

char: Used to store a single character, such as a letter or symbol or a range of numbers from -127 to 128.

char a = 127;//declaration and initialization with a number
char b = 'A' //declaration and initialization with a character

bool: Used to store boolean values (true or false).

boolean status = True;

The unsigned modifier before data type restrict the storage of number in negative side, and allow us to store more values in the positive side

    Leave a Reply

    Your email address will not be published.

    Need Help?