Tuesday, December 17, 2019

Data Types in C Programming

There are three types of data Types in c programming:



1. Primary Data Types:
char
--> Character data types are used to define variables taking one character as its value.
short
--> The short integer can be used in places where small values and little storage space is required.
int
--> Integer data types are used to define the variables taking integer values with or without constant values given.
--> There are other data types like ‘short’ and ‘long’ used to define integer values but they have different ranges
long
--> long integer gives us a long range or a bigger size compared to ‘short’
--> It can cause our program to take more time for execution because of the storage size it offers.

float and double
--> Float and double data types are used to define variables that take up a decimal value or an exponential value.
--> Float has a range of –3.4e38 to +3.4e38 and its size is 4 bytes.
--> Double has a range of -1.7e308 to +1.7e308 and its size is 8 bytes.

2. Derived Data Types:
Array
--> array is a collection of data of the same data type.
--> we declare an integer array, all the values in the array have to be an integer.
--> These are declared under the same variable and are accessed using it.
Pointers
--> A pointer contains the address of a variable in the program.

3. User-defined Data Types

Structures :
--> It is a collection of variables of different data types represented by the same name.
Union :
--> Store data of different data type in the same memory location.
--> A union can have multiple members but it can store only one member at a particular time.
Enum
--> Enumeration is used to declare the variables and consists of integral constants.




EXAMPLE 1: Integer Variable in C
#include <stdio.h>
void main()
{
    int i = 15;
    printf("integer: %d \n", i);
}
OUTPUT:
The %d is to tell printf() function to format the integer i as a decimal number.

The output from this program would be integer: 15.


EXAMPLE 2: Float Variable in C
#include <stdio.h>
void main()
{
    float f = 3.145
    printf("My float: %f \n", f);
}

OUTPUT:
The %f is to tell printf() function to format the float variable f as a decimal floating number. My float: 3.14

EXAMPLE 3: Character Variable in C
#include <stdio.h>
void main()
{
    char c;
    c = 'b';
    printf("This is my character: %c \n", c);
OUTPUT:
The %c is to tell printf() function to format the variable “c” as a character.

 The output from this program would be This is my character: b.

No comments:

Post a Comment