In this tutorial we will learn about variable and data types in c language. First we will learn why we need variables in c, and after that about different data types in c language. Now look at the below code,
#include<stdio.h>
int main()
{
int n,m,s;
n=10;
m=30;
s=n+m;
printf("\nSum : %d",s);
return 0;
}
In the above C code, you need to focus on first line of main() function,that is “int n,m,s”.
- int – This is called data type, Integer data type.
- n,m,s – These are variables of integer type.
What is Variable in C Language
- Variable is a memory location.
- It can store some values.
- Value of a variable can changed, during programming.
Naming Rules for Variable
- Name of a variable can be combination of letters, numbers and underscore.
- First character of variable name should not be a number, it must start with character or underscore.
- A variable name can have both uppercase and lowercase or combination of both case.
- Variable name should not be a reserve keyword(predefined word in C, ex. void, break etc.).
- Name of variable should be meaningful.
Data Types in C Language
Here we will learn about different data types in C language and format of data type in C programming. Whenever you declare a variable in c, you need to specify a type of data to be store in variable as data type. Each data type in C Language has specific format for input or print a variable. We will discuss it in detail about why and how to use this with example.
List of Data Types
- char – To store single character value.
- int – Used to store whole numbers.
- float – float is used to store decimal number with single precision.
- double – To store decimal number with double precision.
Signed and Unsigned Modifier in C
In C signed and unsigned modifier can be used with char and int data types. It is related to positive and negative values. By default char and int are signed type.
- signed – default modifier, can store both negative and positive values.
- unsigned – It can store zero or positive values.
Data Type with Format Specifier in C
Data Type | Size in Bytes | Range | Format Specifier |
---|---|---|---|
signed char | 1 | -128 to 127 | %c |
unsigned char | 1 | 0 to 255 | %c |
short int | 2 | -32,768 to 32,767 | %hd |
unsigned short int | 2 | 0 to 65,535 | %hu |
unsigned int | 4 | 0 to 4,294,967,295 | %u |
int | 4 | -2,147,483,648 to 2,147,483,647 | %d |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
long long int | 8 | -(2^63) to (2^63)-1 | %lld |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 | %llu |
float | 4 | 1.2E-38 to 3.4E+38 (6 decimal places) | %f |
double | 8 | 2.3E-308 to 1.7E+308 (15 decimal places) | %lf |
long double | 12 | 3.4E-4932 to 1.1E+4932 (19 decimal places) | %Lf |