0% found this document useful (0 votes)
5 views6 pages

Data Types

The document explains data types in C programming, defining them as specifications that determine the type of value a variable can hold and the memory required. It categorizes data types into basic, derived, and user-defined types, detailing basic types like char, int, float, and double, along with their sizes and ranges. Additionally, it covers the importance of using appropriate data types to save memory, improve speed, and avoid errors, and provides format specifiers and the use of the sizeof() operator.

Uploaded by

idhruvbajaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Data Types

The document explains data types in C programming, defining them as specifications that determine the type of value a variable can hold and the memory required. It categorizes data types into basic, derived, and user-defined types, detailing basic types like char, int, float, and double, along with their sizes and ranges. Additionally, it covers the importance of using appropriate data types to save memory, improve speed, and avoid errors, and provides format specifiers and the use of the sizeof() operator.

Uploaded by

idhruvbajaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA TYPES IN C

1. What is a Data Type?


A data type tells the computer what kind of value a variable will store and how much
memory it needs.
Just like in real life:
●​ If you want to keep tea, you use a cup
●​ If you want to keep food, you use a plate
●​ If you want to store clothes, you use an almirah
●​ If you want to store books, you use a bookshelf
So, data types are used to store different kinds of data in the proper form, just like
keeping things in the correct place in real life.

Data type = Type of data + Memory size + Value range

2. Why Data Types Are IMPORTANT?


●​ Age, salary, grade → all are different types of data
●​ Storing all data in same format would:
○​ Waste memory
○​ Give wrong results
So C uses different data types to:
●​ Save memory
●​ Improve speed
●​ Avoid errors

3. Classification of Data Types in C


C data types are classified as:
1.​ Basic (Primitive) Data Types
2.​ Derived Data Types
3.​ User-Defined Data Types

4. Basic Data Types in C


Basic data types store simple values.
Basic Data Types:
●​ char
●​ int
●​ float
●​ double
But int and double become more powerful using MODIFIERS.

5. Integer Data Types


All integer types are derived from int.
a) int
●​ Stores whole numbers
●​ Can store positive & negative values
int age = 20;
●​ Typical size: 4 bytes

#include <stdio.h>

int main() {
int age = 20; // int variable
printf("Age = %d\n", age);
return 0;
}

b) short int
●​ Uses less memory than int
●​ Used when values are small
short int marks = 85;
●​ Size: 2 bytes

#include <stdio.h>

int main() {
short int marks = 85; // short int variable
printf("Marks = %hd\n", marks);
return 0;
}

c) long int
●​ Used to store large integer values
●​ Size is greater than or equal to int
long int population = 5000000;
●​ Size: 4 or 8 bytes (system dependent)

#include <stdio.h>

int main() {
long int population = 5000000; // long int variable
printf("Population = %ld\n", population);
return 0;
}

d) long long int


●​ Used for very large integers
●​ Introduced to handle big values safely
long long int distance = 123456789012;
●​ Size: 8 bytes
Common in:
●​ Competitive programming
●​ Banking systems

#include <stdio.h>

int main() {
long long int distance = 123456789012; // very large integer
printf("Distance = %lld\n", distance);
return 0;
}

e) Signed & Unsigned Integers


signed int
●​ Stores positive + negative
●​ Default for int
signed int x = -10;

#include <stdio.h>

int main() {
signed int x = -10;
printf("Signed Value = %d\n", x);
return 0;
}

unsigned int
●​ Stores only positive values
●​ Range becomes double
unsigned int count = 100;

#include <stdio.h>

int main() {
unsigned int count = 100;
printf("Unsigned Value = %u\n", count);
return 0;
}

Use unsigned when value can never be negative

6. Character Data Type (char)


●​ Stores single character
●​ Stored internally as ASCII value​

char grade = 'A';


●​ Size: 1 byte
ASCII Example:
'A' → 65

#include <stdio.h>

int main() {
char grade = 'A'; // character variable
printf("Grade = %c\n", grade);
printf("ASCII Value = %d\n", grade); // ASCII representation
return 0;
}

Signed & Unsigned char


signed char c1 = -10;
unsigned char c2 = 255;

#include <stdio.h>

int main() {
signed char c1 = -10;
unsigned char c2 = 255;

printf("Signed char = %d\n", c1);


printf("Unsigned char = %d\n", c2);

return 0;
}

7. Floating-Point Data Types (Decimals)

a) float
●​ Stores decimal numbers
●​ Single precision
float price = 45.75;
●​ Size: 4 bytes
●​ Precision: ~6 digits

#include <stdio.h>

int main() {
float price = 45.75;
printf("Price = %f\n", price);
return 0;
}

b) double
●​ Stores decimal numbers with more accuracy
double salary = 45678.9876;
●​ Size: 8 bytes
●​ Precision: ~15 digits
double is more accurate than float

#include <stdio.h>

int main() {
double salary = 45678.9876;
printf("Salary = %lf\n", salary);
return 0;
}

c) long double
Used for very high precision calculations​

long double pi = 3.141592653589793;


●​ Size: 12 or 16 bytes (system dependent)
Used in:
●​ Scientific calculations
●​ Research programs

#include <stdio.h>

int main() {
long double pi = 3.141592653589793;
printf("Value of Pi = %Lf\n", pi);
return 0;
}

8. DATA TYPES TABLE (DATA TYPES – SIZE, RANGE & EXAMPLE)


Data Type Size Range (Typical) Example

char 1 byte -128 to 127 char c = 'A';

unsigned char 1 byte 0 to 255 unsigned char x = 200;

short int 2 bytes -32,768 to 32,767 short s = 100;

unsigned short int 2 bytes 0 to 65,535 unsigned short us = 500;

int 4 bytes -2.1B to +2.1B int a = 10;

unsigned int 4 bytes 0 to 4.2B unsigned int b = 20;

long int 4 / 8 bytes Large range long l = 500000;

unsigned long int 4 / 8 bytes Larger unsigned long ul =


900000;

long long int 8 bytes Very large long long ll =


123456789;

float 4 bytes ±3.4e38 float f = 3.14;

double 8 bytes ±1.7e308 double d = 45.678;

long double 12/16 bytes Very large long double ld = 3.14;


9. Format Specifiers
Data Type Format

int %d

unsigned int %u

short int %hd

long int %ld

long long int %lld

char %c

float %f

double %lf

long double %Lf

10. sizeof() Operator


Used to find memory size.
printf("%zu", sizeof(long int));

You might also like