Data types of Variables:

Data types specify the type of data getting stored in the variable. They restrict other types of data to be stored in the variable. In this tutorial we will discuss about some basic data types and their implementation.

For Numbers:

The basic data types which we commonly used to store numbers are given below.
  • int - stores integer value.
  • float - it stores decimal values.
  • double - it also stores decimal values but its precision is double than float.


datatype.c
#include <stdio.h>

int main() {
  int age = 42;
  float pi = 3.14;
  double per = 90.345;
      
  
  printf("Integer: %d\n", age);
  printf("Float: %f\n", pi);
  printf("Double: %lf\n", per);
  
  return 0;
}

Char Data Type:

Char data type is used to store any single character in a variable.

datatype.c
#include <stdio.h>

int main() {
  char grade;
  
  printf("Enter your grade : ");
  scanf(" %c", &grade); 
  
  printf("Grade = %c", grade);
  
  return 0;
}

Boolean Data Type:

Boolean data type stores only true / False. For using booleans in C, you have to import <stdbool.h> header file. It return 1 if true and 0 if false.

Booleans can store:
  • true / false
  • 1 / 0
  • Yes / No
datatype.c
#include <stdio.h>
#include <stdbool.h>  
  
int main() {
  bool isTrue = true;
  bool isFalse = false;
  printf("%d ", isTrue);  
  printf("%d", isFalse);         
    
  return 0;
}

String Data Type:

String data type is use to store sequence of characters/text/anything inside double quotation marks.

string.c
#include <stdio.h>

int main() {
  char greetings[] = "Hello, World!";
      
  printf("String: %s\n", greetings);
  
  return 0;
}

We will learn about string data type in detail later in this tutorial.

Format Specifiers:

Format specifiers in C are used with functions like printf and scanf to specify the expected format of the data to be printed or read.
Data TypeFormat specifier
int%d
float%f
double%lf
char%c
string%s

Size of Data Types:

Size of variable depends upon the data type of variable.
Data TypeSize
char1 byte
bool1 byte
int2 or 4 bytes
float4 bytes
double8 bytes

Note: It is not necessary to remember the size of all data types.


sizeof() operator:

We can find the size of any data type or variable using sizeof() operator. Let's see an example.

size.c
#include <stdio.h>

int main() {
  print("Size of data types in bytes:\n");
  printf("Int: %zu\n", sizeof(int));
  printf("Float: %zu\n", sizeof(float));
  printf("Double: %zu\n", sizeof(double));
  return 0;
}