Data Types in C-1
Data Types in C-1
Data Types in C are the data values a C program can process. They are the basic building blocks of a C program. It is
something we already saw in the variables in C and keywords in C section. Here, you will get to know about the data types
in detail. Explore this concept further in our comprehensive C Tutorial for Beginners. We are going to look at the wide
variety of data types available in C in this article, as well as their importance and applications. You can also check our C
Certification Course
Data types in C refer to the various types of data, such as integer and float, that a C program can process. Each type of data
is represented by a specific keyword and is used within a program to perform calculations, represent objects, or store
strings.
• A data type defines how a certain operation will be performed on the data item specified as its arguments such as
an arithmetic operation or a comparison test.
• Data types also determine the amount of memory allocated for storing a particular type of value.
• Data types are essential to C programming since they are necessary to define the items used within any program.
The fundamental data types in C, such as integers, float, characters, etc., are used to represent simple values and are
known as the "basic data types."
1. int
The int data type is used to store integers (whole numbers) like -1, 0, 42, etc.
Syntax
int variable_name;
#include <stdio.h>
int main()
int a = 9;
int b = -9;
int c = 89U;
c);
return 0;
Output
Integer value with positive data: 20
• Counting: Integers are commonly used for counting discrete objects or events. For example, counting the number
of students in a class, the number of days in a week, etc.
• Indexing: Integers are often used as indices to access elements in arrays, lists, or other data structures.
• Mathematical Operations: Integers are used in mathematical calculations such as addition, subtraction,
multiplication, and division.
• Loop Iterations: Integers are frequently used as loop counters in iteration constructs like for loops and while loops.
• Memory Allocation: Integers are used to represent memory addresses, offsets, and sizes.
• Error Codes and Flags: Integers are often used to represent error codes, status flags, and boolean values.
• User Input and Output: Integers are commonly used to represent user input from keyboards, mouse clicks, or
other input devices.
2. char
The char data type is used to store single characters like 'A', '1', or '$'. The character must be surrounded by single quotes
and we must use the %c format specifier to print it.
Syntax
char variable_name;
#include <stdio.h>
int main() {
printf("%c", myLetter);
return 0;
Output
You can also use ASCII values to display certain characters. Here, these values are not surrounded by quotes (' '), as they
are numbers
Example
#include <stdio.h>
int main() {
printf("%c\n", a);
printf("%c\n", b);
printf("%c", c);
return 0;
Output
3. float
The float data type is used to store single-precision floating-point numbers, which can represent decimal values with
limited precision. This data type size is 4 bytes, i.e., it occupies 4 bytes of memory. It ranges between +/- 3.4E-38F up until
+/- 1 .7E+308F depending on the system’s architecture.
Syntax
float variable_name;
#include <stdio.h>
int main() {
printf("%f", myNum);
return 0;
Output
6.850000
4. double
The double data type is used to store double-precision floating-point numbers i.e. 64 bits of decimal numbers or floating
points, which offer higher precision than float for decimal values. This data type size is 8 bytes, i.e., it occupies 8 bytes of
memory. It ranges between 1.7E-308 to 1.7E+308.
Syntax
double variable_name;
#include <stdio.h>
int main() {
printf("%lf", num);
return 0;
Output
20.630000
• The precision of a floating point value indicates how many digits the value can have after the decimal point.
• The precision of float is six or seven decimal digits, while double variables have a precision of about 15 digits.
• Therefore, it is recommended to use double for most calculations. However, double takes up twice as much
memory as float (8 bytes vs. 4 bytes).
If you want to remove the extra zeros from a floating point number or in other words, set decimal precision, you can use a
dot (.) followed by a number that specifies how many digits should be shown after the decimal point.
Example
#include <stdio.h>
int main() {
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
Output
7.500000
7.5
7.50
7.5000
5. void
When a function doesn't return a value or a pointer doesn't have a particular data type attached to it, C uses the void data
type to express this. It is commonly used in function declarations as a return type indicating that the function does not
return any values and that it simply performs some task without producing any results.
Syntax
void functionName(parameters) {
// Function code
#include <stdio.h>
int main()
printf("%d", *(int*)ptr);
return 0;
Output
50
In the C programming language, a derived data type is a data type that is created by combining one or more basic or other
derived data types.
1. Arrays
An array is a group of identically typed elements kept in consecutive memory regions. Multiple values of the same type can
be stored and accessed using a single variable name.
Syntax
data_type array_name[array_size];
Initializing Array in C
Though we haven't specified the size, the compiler understands the size as 5 due to the initialization of 5 elements.
• int marks[5];
• marks[1]=60;
• marks[2]=70;
• marks[3]=85;
• marks[4]=75;
1. Single-dimensional array: It is a collection of elements of the same data type that are stored in a contiguous block
of memory.
2. Multi-dimensional array: It is an array that contains one or more arrays as its elements. We will see this in the
next section multidimensional arrays in C.
2. Pointers
A pointer is a variable that keeps track of another variable's memory address. Pointers are used to indirectly access data by
referencing its location in memory and for dynamic memory allocation.
Syntax
data_type *pointer_name;
Example of Pointers in C
#include <stdio.h>
int main(){
int number=120;
int *p;
printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of
number.
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we
will get the value stored at the address contained by p.
return 0;
Output
Pointer Arithmetics in C
1. Increment/Decrement of a Pointer
#include <stdio.h>
int main()
int a = 22;
int *p = &a;
p++;
p--;
return 0;
Output
p = 1357824204
p++ = 1357824208
p-- = 1357824204
#include <stdio.h>
int main()
int a = 10;
ptr1 = &a;
ptr2 = &a;
ptr2 = ptr2 + 3;
return 0;
Output
#include <stdio.h>
int main()
// Integer variable
int N = 10;
ptr1 = &N;
ptr2 = &N;
ptr2 = ptr2 - 3;
return 0;
Output
1. Structure
A user-defined composite data type called a "structure" brings together variables of several data kinds under one name.
Complex data structures are made using it to represent real-world entities. You can create a structure by using the struct
keyword and declare each of its members inside curly braces
Syntax
struct structure_name {
data_type member1;
data_type member2;
};
To access the structure, create a variable of it. Use the struct keyword inside the main() method, followed by the name of
the structure and then the name of the structure variable. To access members of a structure, use the dot syntax (.) after
the structure variable.
#include <stdio.h>
struct scholarHat {
int myNum;
char myLetter;
};
int main() {
struct scholarHat s;
s.myNum = 15;
s.myLetter = 'S';
// Print values
return 0;
Output
My number: 15
My letter: S
2. Union
The union data type in C is a user-defined datatype that allows users to store different data types in the exact memory
location. It is similar to a structure but with one important difference, i.e., only one of its members can be active and
contain valid information at any given point in time.
Syntax
union union_name {
data_type member1;
data_type member2;
};
#include <stdio.h>
union Data
{ int x;
float y;
data;
int main()
data.x = 35;
data.y = 6.5f ;
return 0;
Output
data.x: 35
data.y: 6.500000
3. Enumeration
A user-defined data type called enumeration in C consists of a finite collection of named integer constants that are often
used to represent a group of related values with unique names. To create an enum, use the enum keyword, followed by
the name of the enum, and separate the enum items with a comma. To access the enum, you must create a variable of it.
Syntax
enum enum_name {
constant1,
constant2,
constant3,
};
#include <stdio.h>
enum ScholarHat {
LOW,
MEDIUM,
HIGH
};
int main() {
printf("%d", var);
return 0;
Output
• Memory Allocation: Data types determine the amount of memory allocated to a variable. Different data types
occupy different amounts of memory, allowing efficient use of resources.
• Data Representation: Data types define how data is represented in binary form, ensuring that the computer
understands how to manipulate and store values.
• Operations and Functions: Data types determine which operations and functions are valid for a variable. For
example, arithmetic operations vary depending on data types (e.g., integer vs. floating-point).
• Compatibility: Data types ensure compatibility when sharing data between different parts of a program or when
interfacing with external libraries or hardware.
• Optimization: Properly choosing data types can lead to optimized code. For example, using smaller data types for
variables that don't require large ranges can save memory and improve performance.
• Code Readability: Data types make code more readable and self-documenting. They convey the intended use of a
variable to other developers.
• Portability: Data types help ensure code portability across different platforms and compilers by defining the size
and behavior of variables consistently.
• Memory Management: Data types influence how memory is managed and released, which is crucial for
preventing memory leaks and efficiently using system resources.