Skip to content

Datatypes and their properties

Datatype determines:
1. type of data
2. memory requirement
3. valid operations
4. range of values

Bytes is the measurement used to express the quantity of memory of computer.

1 char = 1 byte memory
1 byte = 8 bits
1 bit = 0 or 1
1024 bytes = 1 Kilo Byte
1024 KB = 1 Mega Byte
1024 MB = 1 Giga Byte
1024 GB=1 Tera Byte

int
used to create integer type variables.  These variable can store rounded numbers.  it requires 2 or 4 bytes of memory.
Eg: 45

2 bytes will be allocated in 16 bit systems
4 bytes will be allocated in 32 or 64 bit systems

float
used to create variable that can store numbers with decimal places. requires 4 bytes of memory
Eg: 23.4567

double
same as float but can store number with higher precision.  requires 8 bytes of memory.
Eg: 23.4567892345

char
used to create variable that can store any ASCII character.  requires 1 byte of memory.  ASCII Table is developed by ANSI

ASCII – American Standard Code for Information Intechange
ANSI – American National Standards Institute

ASCII is table that is defining 256 characters that can be represented in system’s memory.  they are: (0-255)
small alphabets: a-z
Upper Alphabets: A-Z
numbers: 0-9
symbols: $, #, *, @ etc
special characters: NULL, Enter, Tab, Space etc.

//Example
#include<stdio.h>

void main()
{
   int x;
   x=45;
   float y;
   y=2.34;
   double z;
   z=48.369;
   char ch;
   ch=’A’;
   printf(“x=%d\n”,x);
   printf(“y=%f\n”,y);
   printf(“z=%lf\n”,z);
   printf(“ch=%c\n”,ch);
}

sizeof()
The sizeof is a pre-defined operator of C Language that is used to return the memory size of a variable or datatype.

syntax:
sizeof(variable or datatype)

Example:
#include<stdio.h>

void main()
{
    printf(“Memory size of Integer: %d bytes”,sizeof(int));
    printf(“\nMemory size of Float: %d bytes”,sizeof(float));
    printf(“\nMemory size of Double: %d bytes”,sizeof(double));
    printf(“\nMemory size of Char: %d bytes”,sizeof(char));
}

>>>Datatype Modifiers:
There are 4 keywords that are considered as datatypes modifiers or qualifiers from C Language.
short
long
signed
unsigned

These are useful to extend or reduce the data holding capability of variable.

Signed vs Unsigned
signed (Default): It can store both positive and negative values.  Most integer types (int, char) are signed by default.
unsigned: Variable declared using this can store only positive values (and zero).  Doubles the upper limit of the corresponding signed type.

Example: int vs unsigned int
Type Range (Typical 4 bytes)
int (signed) –2,147,483,648 to 2,147,483,647
unsigned int 0 to 4,294,967,295

Short Vs Long
these are used to change default memory requirement of variable

short int 2 bytes –32,768 to 32,767
int 4 bytes –2,147,483,648 to 2,147,483,647

in 64 bit systems ‘int’ means ‘long signed int’
in 16 bit systems ‘int’ means ‘short signed int’

#include<stdio.h>

void main()
{
    short int x;
    printf(“Memory allocated for x =%d bytes”,sizeof(x));

}

>>>Important points to be remember regarding variables:
1. a variable can be declared anywhere in the program but before its first use.
//Example
#include <stdio.h>

void main()
{
    printf(“C Program\n”);
    int x;
    x=10;
    printf(“x=%d”,x);
}

2. a variable can be assigned with many values during program execution
3. first value assignment of variable is known as ‘initialisation’.  The first value is called as ‘initial value’.
//Example
#include <stdio.h>
void main()
{
    int x;
    x=10;  //initialisation or assignment
    printf(“x=%d\n”,x);
    x=20; //assignment
    printf(“x=%d\n”,x);
    x=30;
    printf(“x=%d\n”,x);
}

4. Variable can also be initailised along with declaration
//Example
#include <stdio.h>

void main()
{
    int x=10;  //declaration with initialisation
    printf(“x=%d\n”,x);
   
}

5. garbage or unpredictable value will be stored in the variable if you left it un-intialised after declaration.

//Example
#include <stdio.h>

void main()
{
    int x;
    printf(“x=%d\n”,x);

}

6. we can restrict re-assignment of variable with ‘const’ keyword

//Example
#include <stdio.h>

void main()
{
    const int x=4;
    //x=12; gives error
    printf(“x=%d\n”,x);
//    x=24; //error
    printf(“x=%d\n”,x);

}

7. variable cannot be re-declared in the same scope even with other datatype.

//Example
#include <stdio.h>

void main()
{
    int x;
    x=12;
    printf(“x=%d\n”,x);
    float x; //gives error
}

  • 8. you can declare a list of variables of same datatype with single declaration statement by separating their identifiers with comma.  You can assign the same value to multiple variables in a single line:

    //Example
    #include <stdio.h>
    int main() {
        int a, b, c;
        a = b = c = 10;  // All three variables are assigned 10
        printf(“a = %d, b = %d, c = %d\n”, a, b, c);
        return 0;
    }

    9. You can assign different values to multiple variables in one line:

    //Example
    #include <stdio.h>
    int main() {
        int x = 5, y = 10, z = 15; // Different values assigned
        printf(“x = %d, y = %d, z = %d\n”, x, y, z);
        return 0;
    }