0% found this document useful (0 votes)
102 views

Data Types in C - GeeksforGeeks

Understand datatypes in C

Uploaded by

Anupam Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Data Types in C - GeeksforGeeks

Understand datatypes in C

Uploaded by

Anupam Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Job Fair 2023 Strings Arrays Pointers Memory Management File Handling C Programs C Interview Questions

Data Types in C

Read Discuss(20) Courses Practice Video

Each variable in C has an associated data type. Each data type requires different
amounts of memory and has some specific operations which can be performed over it.
It specifies the type of data that the variable can store like integer, character, floating,
double, etc. The data type is a collection of data with values having fixed values,
meaning as well as its characteristics.

The data types in C can be classified as follows:

Types Description

Primitive Arithmetic types can be further classified into integer and floating
Data Types data types.

The data type has no value or operator and it does not provide a
Void Types
result to its caller. But void comes under Primitive data types.

User
It is mainly used to assign names to integral constants, which make a
Defined
program easy to read and maintain
DataTypes

Derived The data types that are derived from the primitive or built-in
types datatypes are referred to as Derived Data Types.

Different data types also have different ranges up to which they can store numbers.
These ranges may vary from compiler to compiler. Below is a list of ranges along with
the memory requirement and format specifiers on the 32-bit GCC compiler.

Data Type Memory Range Format


(bytes) Specifier

short int 2 -32,768 to 32,767 %hd

unsigned short
2 0 to 65,535 %hu
int

unsigned int 4 0 to 4,294,967,295 %u

-2,147,483,648 to
int 4 %d
2,147,483,647

-2,147,483,648 to
long int 4 %ld
2,147,483,647

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long 0 to
8 %llu
long int 18,446,744,073,709,551,615

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 %f
1.2E-38 to 3.4E+38

double 8 %lf
1.7E-308 to 1.7E+308

long double 16 %Lf


3.4E-4932 to 1.1E+4932

Integer Types

The integer data type in C is used to store the whole numbers without decimal values.
Octal values, hexadecimal values, and decimal values can be stored in int data type in
C. We can determine the size of the int data type by using the sizeof operator in C.
Unsigned int data type in C is used to store the data values from zero to positive
numbers but it can’t store negative values like signed int. Unsigned int is larger in size
than signed int and it uses “%u” as a format specifier in C programming language.
Below is the programming implementation of the int data type in C.

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


Size: 2 bytes or 4 bytes
Format Specifier: %d

Note: The size of an integer data type is compiler-dependent, when processors are 16-
bit systems, then it shows the output of int as 2 bytes. And when processors are 32-
bit then it shows 2 bytes as well as 4 bytes.

// C program to print Integer data types.


#include <stdio.h>

int main()
{
// Integer value with positive data.
int a = 9;

// integer value with negative data.


int b = -9;

// U or u is Used for Unsigned int in C.


int c = 89U;

// L or l is used for long int in C.


long int d = 99998L;

printf("Integer value with positive data: %d\n", a);


printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n", c);
printf("Integer value with an long int data: %ld", d);

return 0;
}

Output

Integer value with positive data: 9


Integer value with negative data: -9
Integer value with an unsigned int data: 89
Integer value with an long int data: 99998

Character Types

Character data type allows its variable to store only a single character. The storage
size of the character is 1. It is the most basic data type in C. It stores a single character
and requires a single byte of memory in almost all compilers.

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


Size: 1 byte
Format Specifier: %c

// C program to print Integer data types.


#include <stdio.h>

int main()
{

char a = 'a';
char c;

printf("Value of a: %c\n", a);

a++;
printf("Value of a after increment is: %c\n", a);

// c is assigned ASCII values


// which corresponds to the
// character 'c'
// a-->97 b-->98 c-->99
// here c will be printed
c = 99;

printf("Value of c: %c", c);

return 0;
}

Output

Value of a: a
Value of a after increment is: b
Value of c: c

Floating-Point Types

In C programming float data type is used to store floating-point values. Float in C is


used to store decimal and exponential values. It is used to store decimal numbers
(numbers with floating point values) with single precision.

Range: 1.2E-38 to 3.4E+38


Size: 4 bytes
Format Specifier: %f

// C Program to demonstrate use


// of Floating types
#include <stdio.h>

int main()
{

float a = 9.0f;
float b = 2.5f;

// 2x10^-4
float c = 2E-4f;
printf("%f\n",a);
printf("%f\n",b);
printf("%f",c);

return 0;
}

Output

9.000000
2.500000
0.000200

Double Types

A Double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It is used to define numeric values which hold numbers
with decimal values in C. Double data type is basically a precision sort of data type
that is capable of holding 64 bits of decimal numbers or floating points. Since double
has more precision as compared to that float then it is much more obvious that it
occupies twice the memory as occupied by the floating-point type. It can easily
accommodate about 16 to 17 digits after or before a decimal point.

Range: 1.7E-308 to 1.7E+308


Size: 8 bytes
Format Specifier: %lf

// C Program to demonstrate
// use of double data type
#include <stdio.h>

int main()
{

double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;

printf("%lf\n", a);

printf("%lf\n", b);

printf("%lf", c);

return 0;
}

Output

123123123.000000
12.293123
2312312312.123123

Void Data types

The void data type in C is used to specify that no value is present. It does not provide a
result value to its caller. It has no values and no operations. It is used to represent
nothing. Void is used in multiple ways as function return type, function arguments as
void, and pointers to void.

Syntax:

// function return type void

void exit(int check);

// Function without any parameter can accept void.

int print(void);

// memory allocation function which


// returns a pointer to void.
void *malloc( size_t size);

// C program to demonstrate
// use of void pointers
#include <stdio.h>

int main()
{
int val = 30;
void *ptr = &val;
printf("%d", *(int *)ptr);
return 0;
}

Output

30

We can use the sizeof() operator to check the size of a variable. See the following C
program for the usage of the various data types:

// C Program to print size of


// different data type in C
#include <stdio.h>

int main()
{
int size_of_int=sizeof(int);
int size_of_char= sizeof(char);
int size_of_float=sizeof(float);
int size_of_double=sizeof(double);

printf("The size of int data type : %d\n",size_of_int );


printf("The size of char data type : %d\n",size_of_char);
printf("The size of float data type : %d\n",size_of_float);
printf("The size of double data type : %d",size_of_double);

return 0;
}

Output

The size of int data type : 4


The size of char data type : 1
The size of float data type : 4
The size of double data type : 8

To check your knowledge of data types in C, go through the Quiz on Data Types.

Last Updated : 09 Sep, 2022 333

Similar Reads

1. Di!erence between fundamental data types and derived data types

2. How to Convert Data From SQL to C Data Types?

3. How to print range of basic data types without any library function and constant in C?

4. What are the data types for which it is not possible to create an array?

5. C | Data Types | Question 1

6. C | Data Types | Question 2

7. C | Data Types | Question 4

8. C | Data Types | Question 5

9. C | Data Types | Question 6

10. C | Data Types | Question 7

Next

bool in C

Article Contributed By :

GeeksforGeeks

Vote for difficulty


Current difficulty : Easy

Easy Normal Medium Hard Expert

Improved By : pantpawan, Srichandrahaas, kamleshjoshi18

Article Tags : C Basics, C-Data Types, C Language

Improve Article Report Issue

C o u rs e s

90k+ interested Geeks 107k+ interested Geeks 795k+ interested Geeks 18k+ interested Gee

Master C Programming with Master C++ Programming - Complete Interview DevOps Engineering
Data Structures Complete Beginner to… Preparation - Self Paced Planning to Product
Advanced
Beginner to Advance Beginner to Advance Beginner to Advance Beginner to Advanc

A-143, 9th Floor, Sovereign Corporate Tower,


Sector-136, Noida, Uttar Pradesh - 201305

[email protected]

Company Explore Languages Data Algorithms Web

About Us Job Fair For Python Structures Sorting Development

Careers Students Java Array Searching HTML

In Media POTD: Revamped C++ String Greedy CSS

Contact Us Python Backend GoLang Linked List Dynamic JavaScript


LIVE Programming
Terms and SQL Stack Bootstrap
Conditions Android App Pattern
R Language Queue ReactJS
Development Searching
Privacy Policy Android Tutorial Tree AngularJS
DevOps LIVE Recursion
Copyright Policy Graph NodeJS
DSA in Backtracking
Third-Party
JavaScript
Copyright
Notices

Advertise with us

Data Science Interview Python GfG School UPSC/SSC/BA Write & Earn
& ML Corner Python Tutorial CBSE Notes for NKING Write an Article

Data Science Company Python Class 8 SSC CGL Syllabus Improve an


With Python Preparation Programming CBSE Notes for SBI PO Syllabus Article

Data Science For Preparation for Examples Class 9 Pick Topics to


IBPS PO Syllabus
Beginner SDE Django Tutorial CBSE Notes for Write
UPSC Ethics
Machine Company Python Projects Class 10 Write Interview
Notes
Learning Tutorial Interview Corner Python Tkinter CBSE Notes for Experience
UPSC Economics
Maths For Experienced Class 11 Internships
OpenCV Python Notes
Machine Interview Tutorial CBSE Notes for Video Internship
UPSC History
Learning Internship Class 12
Notes
Pandas Tutorial Interview English Grammar

NumPy Tutorial Competitive

NLP Tutorial Programming

Aptitude

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Got It !
acknowledge that you have read and @geeksforgeeks , Some
understood our Cookie rights
▲Policy reserved
& Privacy Policy

You might also like