C – Data Types

Data Types in C

Data type:

  • We need to specify the data type in variable declaration.
  • Data type specifies, the type of data is allowed to store.

Data types are classified into:

Primitive types: In the above diagram primitive types are generalized. Primitive types sub classified as follows

Following table describes the data type with size, format specifier and limits:

Character data type: Char type is used to store alphabets, symbols and digits. We represents character with single quotes.

#include <stdio.h>
int main(){
            char v1 = ‘a’;
            char v2 = ‘5’;
            char v3 = ‘$’;
            printf(“v1 value is : %c \n”, v1);
            printf(“v2 value is : %c \n”, v2);
            printf(“v3 value is : %c \n”, v3);
            return 0;
}

How character stores into memory?

  • Every character represented by an integer value called ASCII.
  • The range of ASCII from 0 to 255(1 byte limits)
  • Each character occupies 1 byte memory to store the corresponding integer value.
#include <stdio.h>
int main(){
            char v1 = ‘a’;
            char v2 = ‘5’;
            char v3 = ‘$’;
            printf(“v1 value is : %c \n”, v1);
            printf(“v2 value is : %c \n”, v2);
            printf(“v3 value is : %c \n”, v3);
            return 0;
}

Program to display short int information (size and limits):

#include <stdio.h>
int main(){
            char v1 = ‘a’;
            char v2 = ‘5’;
            char v3 = ‘$’;
            printf(“%c ascii value is : %d \n”, v1, v1);
            printf(“%c ascii value is : %d \n”, v2, v2);
            printf(“%c ascii value is : %d \n”, v3, v3);
            return 0;
}

Program to display Character type info:

#include <stdio.h>
#include <limits.h>
int main()
{
            printf(“Signed short min : %d \n”, SHRT_MIN);
            printf(“Signed short min : %d \n”, SHRT_MAX);
            printf(“Unsigned short max : %d \n”, USHRT_MAX);
            return 0;
}

What happens when we try to store the value beyond the limits of datatype?

  • Every variable can store the value within the limits only.
  • When limit exceeded, it counts the value in the specified limit and store some other value.
#include<stdio.h>
int main(){
            char ch = 130;
            printf(“ch val : %d \n”, ch);       // prints -126
            return 0;          
}
#include<stdio.h>
int main(){
            char c1=256, c2=1024;
            printf(“c1 : %d \n”, c1);
            printf(“c2 : %d \n”, c2);
            return 0;          
}
Output:
c1 : 0
c2 : 0
#include<stdio.h>
int main()
{
            short s = -32744;
            printf(“s val : %d \n”, s);          
            return 0;          
}

sizeof(): a pre-defined function used to find the size of Variable, value, Datatype etc.

Program to find the sizes of different variables:

#include <stdio.h>
int main(){
            char c;
            short s;
            float f; 
            printf(“char size : %d \n”, sizeof(c));  // prints-1
            printf(“short size : %d \n”, sizeof(s)); // prints-2
            printf(“float size : %d \n”, sizeof(f));  // prints-4
            return 0;
}

Program to find the sizes of data types directly:

#include <stdio.h>
int main()
{
            printf(“char size : %d \n”, sizeof(char));
            printf(“short size : %d \n”, sizeof(short));
            return 0;
}

Finding the size of expression: In expression, which type occupies highest memory will be the

#include <stdio.h>
int main(){
            char c;
            short s;
            printf(“char + short : %d \n”, sizeof(c+s));
            return 0;
}

ASCII(American Standards Code of Information Interchange): ASCII represents all the characters of language with constant integer values.

A-65
B-66
..
..
Z-90
a-97
b-98
..
..
z-122
0-48
1-49
..
..
9-57
#-35
$-36
..
..
..

scanf():

  • C library is a Collection of Header files.
  • Header file contains Variables and Functions.
  • printf() and scanf() functions belongs to stdio.h header file.
  • Printf() function is used to display the information on Monitor(standard output).
  • Scanf() function is used to read information from keyboard(standard input)

Address operator:

  • We need to specify the location (memory address) using variable to read the data. 
  • ‘&’ is called Address operator in C.
#include <stdio.h>
int main()
{
            int a;   
            printf(“Enter a value : “);
            scanf(“%d”, &a);
            printf(“a value is : %d \n”, a);
            return 0;
}

Program to display the address of variable:

#include <stdio.h>
int main()
{
            int val=10;
            char sym=’$’;
            printf(“Address of val : %d \n”, &val);
            printf(“Value of val : %d \n”, val);
            printf(“Address of sym : %d \n”, &sym);
            printf(“Value of sym : %c \n”, sym);
            return 0;
}
Scroll to Top