Data Types in C

Data Types in C

Introduction

In C programming, data types are crucial for defining the nature of variables and specifying the operations that can be performed on them. This chapter explores various data types in C, including basic types, character types, and derived types. Understanding these types is fundamental for writing efficient and error-free code.

Read Also – Loops in C

1. Basic Data Types

Data Types in C

*****************************************************************************************

1. Basic Data Types in c

a. Integer Types

- int

- short int

- long int

- long long int

b. Floating-Point Types

- float

- double

- long double

2. Character Type

- char

3. Derived Data Types

a. Array

- Array declaration and initialization

b. Pointer

- Pointer declaration and initialization

c. Structure

- Structure definition and usage

d. Union

- Union definition and usage

e. Enumeration

- Enumeration definition and usage

4. Void

- void data type

5. Examples

- Practical examples illustrating the usage of each data type.

*****************************************************************************************

Integer Types

1. int:

- Size: 4 bytes

- Range: -2,147,483,648 to 2,147,483,647

Example:

#include <stdio.h>int main() {int number = 42;printf("The value of 'number' is %d\n", number);return 0;}

Here, `number` is an integer variable initialized with the value 42.

*****************************************************************************************

2. short int:

- Size: 2 bytes

- Range: -32,768 to 32,767

Example:

short int smallNumber = -1234;

*****************************************************************************************

3. long int:

- Size: 4 bytes or 8 bytes (system-dependent)

- Range: -2,147,483,648 to 2,147,483,647 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example:

long int bigNumber = 1234567890L;

*****************************************************************************************

4. long long int:

- Size: 8 bytes

- Range: -(2^63) to (2^63)-1

Example:

long long int veryBigNumber = 987654321012345LL;

*****************************************************************************************

5. float:

- Size: 4 bytes

- Precision: 6 decimal places

Example:

float pi = 3.141592;

*****************************************************************************************

6. double:

- Size: 8 bytes

- Precision: 15 decimal places

Example:

double precisionValue = 12345.678901234567;

*****************************************************************************************

7. long double:

- Size: 10 bytes or 12 bytes (system-dependent)

- Precision: 18 decimal places

Example:

long double extendedPrecision = 9876543210.123456789012345L;

*****************************************************************************************

Character Type

8. char:

- Size: 1 byte

- Range: -128 to 127 or 0 to 255 (signed or unsigned)

Example:

char grade = ‘A’;

3. Derived Data Types

*****************************************************************************************

9. Array:

– A collection of elements of the same data type.

Example:

int numbers[5] = {1, 2, 3, 4, 5};

*****************************************************************************************

10. Pointer:

– A variable that stores the memory address of another variable.

Example:

int value = 10;

int *ptr = &value;

*****************************************************************************************

11. Structure:

– A user-defined data type that groups different variables under one name.

Example:

struct Person {

char name[50];

int age;

};

struct Person person1;

person1.age = 25;

*****************************************************************************************

12. Union:

– Similar to a structure, but it uses the same memory location for all its members.

Example:

union Data {

int intValue;

float floatValue;

char stringValue[20];

};

union Data data;

data.intValue = 42;

*****************************************************************************************

13. Enumeration:

– A user-defined data type that consists of named integer constants.

Example:

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

enum Days today = Wednesday;

*****************************************************************************************

14. Void:

– Represents the absence of a data type.

void printMessage() {

printf("Hello, world!\n");

}

int main() {

printMessage();

return 0;

}

*****************************************************************************************

Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment