Variable and Data Types

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 TypeSize in BytesRangeFormat Specifier
signed char1-128 to 127%c
unsigned char10 to 255%c
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int4-2,147,483,648 to 2,147,483,647%ld
unsigned long int40 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long long int80 to 18,446,744,073,709,551,615%llu
float41.2E-38 to 3.4E+38 (6 decimal places)%f
double82.3E-308 to 1.7E+308 (15 decimal places)%lf
long double123.4E-4932 to 1.1E+4932 (19 decimal places)%Lf

Mini Quiz

0%

C Programming - Quiz 4

1 / 5

Q1. What is a variable in C language?

2 / 5

Q2. Which of the following is a valid variable name in C?

3 / 5

Q3. Which data type is used to store whole numbers in C?

4 / 5

Q4. Which data type is used to store decimal values with single precision?

5 / 5

Q5. Which format specifier is used with the int data type in C?

Your score is

The average score is 0%

0%

Mini Project

Mini Project: Book Information

Question:

Write a C program that declares variables to store the following information:

  • Book Code (Character)
  • Number of Pages
  • Book Price

Assign suitable values to the variables and display them using the correct format specifiers.

View Answer Code
#include<stdio.h>

int main()
{
    char code = 'B';
    int pages = 250;
    float price = 399.5;

    printf("Book Code : %c\n", code);
    printf("Pages : %d\n", pages);
    printf("Price : %f", price);

    return 0;
}

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top