DATA TYPES IN C

Maha



Data Types in c:

  • In the C programming language, a data type serves to define the kind of data a variable can store. 
  • These data types include integers, floating-point numbers, characters, and more. 
  • Each data type has specific characteristics and constraints that determine the range and nature of values a variable of that type can hold. 
  • Understanding and appropriately choosing data types are crucial aspects of programming in C, as they influence memory allocation, storage efficiency, and the overall functionality of the program.



Basic Data Type:

There are two main types of basic data:

Integers (for whole numbers) and floating-point numbers (for decimals). You can use both signed and unsigned numbers.
The amount of memory these data types use may differ on a 32 or 64-bit system. This matters because it affects how large or precise your numbers can be. Understanding this helps make your programs work well on different computers.

Basic Format Specifiers

There are different format specifiers for each data type. Here are some of them:


Let's see the basic data types. Its size is given according to 32-bit architecture.

1.char: 1 byte
  • Range: -128 to 127 (signed), 0 to 255 (unsigned)
2.int: 4 bytes
  • Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)
3.float: 4 bytes
  • Precision: 6 decimal places
4.double: 8 bytes
  • Precision: 15 decimal places
5.short: 2 bytes
  • Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)
6.long: 4 bytes
  • Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)
7.long long: 8 bytes
  • Range: Approximately -9.2 quintillion to 9.2 quintillion (signed), 0 to 18.4 quintillion (unsigned)
8.unsigned int: 4 bytes
  • Range: 0 to 4,294,967,295
9.unsigned char: 1 byte
  • Range: 0 to 255

Let's see the basic data types. Its size is given according to 64-bit architecture.

1.char: 1 byte
  • Range: -128 to 127 (signed), 0 to 255 (unsigned)
2.int: 4 bytes
  • Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)
3.float: 4 bytes
  • Precision: 6 decimal places
4.double: 8 bytes
  • Precision: 15 decimal places
5.short: 2 bytes
  • Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)
6.long: 8 bytes
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed), 0 to 18,446,744,073,709,551,615 (unsigned)
7.long long: 8 bytes
  • Range: Approximately -9.2 quintillion to 9.2 quintillion (signed), 0 to 18.4 quintillion (unsigned)
8.unsigned int: 4 bytes
  • Range: 0 to 4,294,967,295
9.unsigned char: 1 byte
  • Range: 0 to 255

1.Integers (int):

  • Integers are whole numbers with no decimal parts.
  • Used for values like counts or indices.
  • Signed by default, so it can represent positive and negative numbers
  • Takes up 4 bytes of memory, storing values between -2 billion and +2 billion.

Example:

int age = 25;

Program:

  
  #include<stdio.h>
  int main(){
        int age =25;
        printf("int data type \n");
        printf("Age = %d \n",age);
        printf("Size of int data type:%ld",sizeof(age));
  }


Output:


  int data type
  Age = 25
  Size of int data type:4


2.Characters (char):

Represents individual characters, like letters or symbols.
Uses a single byte of memory and can store 256 different characters.
Enclosed in single quotes.

Example:

char grade = 'A';

Program:


   #include<stdio.h>
   int main(){
char grade ='A';
printf("char data type \n");
printf("grade = %c \n",grade);
printf("Size of char data type:%ld \n",sizeof(grade));
   }



Output:

 
  char data type
  grade=A
  Size of char data type:1


3.Floating-Point (float):

Used for numbers with decimal parts or fractions.
Provides good precision, accurate to about 6 decimal places.
Takes up 4 bytes of memory.

Example:

float temperature = 98.6;

Program:


  #include<stdio.h>
  int main(){
float temperature =98.6;
printf("float data type \n");
printf("Temperature = %f \n",temperature);
printf("Size of float data type:%ld",sizeof(temperature));
  }



Output:

 
  float data type
  temperature =  98.599998
  Size of float data type:4


4.Double Precision (double):

Similar to float but offers greater precision, suitable for scientific or financial calculations.
Uses 8 bytes of memory, accurate to about 15 decimal places.
C treats decimal numbers as doubles by default if the type is not specified.

Example:

double pi = 3.14159265359;

Program:


  #include<stdio.h>
  int main(){
double pi=3.14159265359;
printf("double data type\n");
printf("pi=%lf\n",pi);
printf("Size of double data type:%ld",sizeof(pi));
  }



Output:


  double data type
  pi=3.141593
  Size of double data type:8


Derived Data Type:

C introduces derived data types such as arrays, pointers, structures, and unions. These advanced data types empower programmers to manage diverse data, directly manipulate memory, and construct intricate data structures.

1.Arrays:

An array is a collection of elements of the same data type. Elements are stored in contiguous memory locations, and each element is accessed by an index.

Example:

int numbers[6] = {10, 20, 30, 40, 50,\0};

Program:


  #include<stdio.h>
  int main(){
        int array_numbers[6]={10,20,30,40,50,60};
        for(int i=0;i<6;i++){
        printf("array_numbers[%d]= %d\n",i,array_numbers[i]);

        }
  }



Output:


  array_numbers[0]= 10
  array_numbers[1]= 20
  array_numbers[2]= 30
  array_numbers[3]= 40
  array_numbers[4]= 50
  array_numbers[5]= 60


2.Pointers:

A pointer is a variable that holds the memory address of another variable. It allows manipulation of memory directly, and it's often used for dynamic memory allocation.

Example:

int num;
int *ptr = &num;  // Pointer pointing to the address of 'num'

Program:


  #include<stdio.h>
  int main(){
        int num = 23;
        int *ptr =&num; // pointer pointing to the address of num
        printf("Value of num is: %d", *ptr);
  }



Output:

  
  Value of num is: 23


3.Structures:

A structure is a user-defined data type that allows combining variables of different data types under one name. Each variable within a structure is called a member.

Example:

struct Person {
    char empl_name[50];
    int empl_age;
    float empl_salary;
};

struct Person person1 = {"John", 30, 6000.00};

Program:

  
  #include<stdio.h>
  int main(){
        struct person{
                char empl_name[50];
                int empl_age;
                float empl_salary;
        };
        struct person employee ={"jshon",30,6000.00};
        printf("Name of employee: %s\n",employee.empl_name);
        printf("Age of employee: %d\n",employee.empl_age);
        printf("Salary of employee: %f\n",employee.empl_salary);
  }



Output:


  Name of employee: jshon
  Age of employee: 30
  Salary of employee: 6000.000000


4.Unions:

A union is similar to a structure, but all members share the same memory space. Only one member can hold a value at a time. Unions are useful when different types need to be represented interchangeably.

Example:

union studentdetails {
    char student_name[20];
    int student_mark;
    float student_percentage;
};

 union studentdetails data;

Program:


#include <stdio.h>
#include <string.h>

union studentdetails {
    char student_name[20];
    int student_mark;
    float student_percentage;
};

int main() {
    union studentdetails data;

    // Use strcpy to copy the string into the union member
    strcpy(data.student_name, "Ram");
    printf("student_name: %s\n", data.student_name);

    data.student_mark = 450;
    printf("student_mark: %d\n", data.student_mark);

    // Use casting to float for correct calculation
    data.student_percentage = (float)data.student_mark / 5;
    printf("student_percentage: %.2f\n", data.student_percentage);

    return 0;
}


Output:


student_name: Ram
student_mark: 450
student_percentage: 90.00


5. Enumerations:

An enumeration is a user-defined data type used to assign names to integral constants, improving code readability.

Example:

enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum Days today = Wednesday;

Program:


#include <stdio.h>

int main() {
    // Define an enumeration for days of the week
    enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

    // Declare a variable of the enum type and assign a value
    enum Days today = Wednesday;

    // Use a switch statement to display the corresponding day
    switch (today) {
        case Sunday:
            printf("Today is Sunday.\n");
            break;
        case Monday:
            printf("Today is Monday.\n");
            break;
        case Tuesday:
            printf("Today is Tuesday.\n");
            break;
        case Wednesday:
            printf("Today is Wednesday.\n");
            break;
        case Thursday:
            printf("Today is Thursday.\n");
            break;
        case Friday:
            printf("Today is Friday.\n");
            break;
        case Saturday:
            printf("Today is Saturday.\n");
            break;
        default:
            printf("Invalid day.\n");
    }

    return 0;
}


Output:


Today is Wednesday.




More topic in C

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send