Data Types in C Language with example
xr:d:DAFs1Levafk:20,j:5522940580380554460,t:23090110

Data Types in C Language with example

๐Ÿ“Œ Definition:

A data type in C specifies the type of data a variable can hold, such as integers, floating-point numbers, characters, etc. It also defines the size, range, and operations allowed on that data.


๐Ÿ”ถ Types of Data Types in C

C language has the following main categories of data types:

1. Primary (Fundamental) Data Types

2. Derived Data Types

3. User-defined Data Types

4. Void Data Type


โœ… 1. Primary (Fundamental) Data Types

These are the basic built-in data types provided by the C language.

Data TypeSize (in bytes)Format SpecifierExampleDescription
int2 or 4%d or %iint a = 10;Stores integers
float4%ffloat x = 2.5;Stores decimal numbers (single precision)
double8%lfdouble y = 3.14159;Stores decimal numbers (double precision)
char1%cchar ch = 'A';Stores a single character

โš ๏ธ Size may vary depending on compiler and system (e.g., 32-bit vs 64-bit).


๐Ÿ”น Integer Types in Detail

TypeSize (Bytes)Range (Approx)Format Specifier
int4-2,147,483,648 to 2,147,483,647%d
short int2-32,768 to 32,767%hd
long int4 or 8-2^31 to 2^31-1 or more%ld
unsigned int40 to 4,294,967,295%u

๐Ÿ”น Floating Point Types

TypeSize (Bytes)PrecisionFormat Specifier
float46-7 digits%f
double815-16 digits%lf
long double10 or 12>16 digits%Lf

๐Ÿ”น Character Type

TypeSize (Bytes)RangeFormat Specifier
char1-128 to 127 (signed)%c
unsigned char10 to 255%c

โœ… 2. Derived Data Types

These are based on the fundamental data types.

TypeDescriptionExample
ArrayCollection of same data typesint arr[10];
PointerStores memory address of variableint *ptr = &a;
FunctionReturns data after executionint add(int x, int y);
StructureGroups different data typesstruct Student {int id; char name[20];};

โœ… 3. User-defined Data Types

These are defined by the programmer using built-in types.

TypeDescriptionSyntax Example
typedefCreates alias for data typetypedef int Number;
enumDefines a set of named integer constantsenum Color {RED, GREEN, BLUE};
structCombines variables of different typesstruct Person {char name[20]; int age;};
unionSimilar to struct but shares memoryunion Data {int i; float f;};

โœ… 4. Void Data Type

TypeDescriptionExample
voidRepresents absence of value (no return type)void display();
  • Used for:
  • Functions with no return (void main())
  • Void pointers (void *ptr)

๐Ÿงช Examples in C Code

#include <stdio.h>

int main() {
    int a = 10;
    float b = 5.75;
    char c = 'Z';
    double d = 3.14159265359;

    printf("Integer: %d\n", a);
    printf("Float: %f\n", b);
    printf("Character: %c\n", c);
    printf("Double: %lf\n", d);

    return 0;
}

๐Ÿ–ฅ Output:

Integer: 10
Float: 5.750000
Character: Z
Double: 3.141593

๐Ÿ“Œ Summary Chart

CategoryTypes
Fundamentalint, float, double, char
Derivedarray, pointer, function, structure
User-definedtypedef, enum, struct, union
Specialvoid

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *