Data Types in C Programming Language: In C programming, a data type defines the type of data a variable can hold. It determines the amount of memory allocated for that variable and the operations that can be performed on it.
C Programming Video Tutorial Lecture-02
Table of Content
Data Types in C
There are several data types in c programming languages have, a data types defines the type of data a variable can hold. It determines thae amount of memory allocated for that variable and the operations that can be performed on it. C Programming have mian three types of data types which is the following:
Primitive Data Types
Derived Data Types
User Define Data Types
Primitive Data Types
In C programming, primitive data types are the basic data types that are built into the language. They serve as the fundamental building blocks for data manipulation. The primitive data types in C include:
- int
- float
- char
- viod
int Data Type in C Programming Language
Represents integer numbers (whole numbers without a fractional part). The size of an int is typically 4 bytes, but this can vary depending on the system architecture.
Integer Data Types
- short int
- signed
- unsigned
- int
- signed
- unsigned
- long int
- singed
- unsigned
- long long int
- singed
- unsigned
float Data Type in C Programming Language
Represents single-precision floating-point numbers (numbers with a fractional part). It is typically 4 bytes in size.
Floating Point Number
- float
- double
- long double
char Data Type in C Programming Language
Represents single characters (such as ‘A’, ‘b’, ‘1’, etc.) and is typically 1 byte in size. It can also be used to store small integer values.
Character Data Types
- signed
- unsigned
void Data Type in C Programming Language
Represents the absence of type. It is used for functions that do not return a value or to specify generic pointers.
Derived Data Types
Derived data types in C programming are data types that are derived from the fundamental or primary data types (such as int, char, float, and double). Derived data types provide more complex data manipulation capabilities and allow for the storage of more complex data structures. They include arrays, pointers, structures, unions, functions, and enumerations. Here is a full detailed explanation of each:
Arrays
Arrays are a collection of elements of the same type stored in contiguous memory locations. They allow for the efficient storage and access of a large number of elements of the same data type.
Syntax:
data_type array_name[array_size];
Example:
int numbers[5]; // Declares an integer array of size 5
char name[10]; // Declares a character array (string) of size 10
Key Points:
The array index starts at 0.
Arrays can be single-dimensional (one row) or multi-dimensional (e.g., two-dimensional arrays for matrices).
Multi-dimensional arrays are declared with multiple sets of square brackets:
int matrix[3][4]; // Declares a 3×4 matrix of integers
Pointers
Pointers are variables that store the address of another variable. Pointers provide a way to directly access and manipulate memory locations, making them very powerful in C programming.
Syntax:
data_type *pointer_name;
Example:
int *ptr; // Declares a pointer to an integer
char *ch; // Declares a pointer to a character
Key Points:
The * operator is used to declare a pointer variable.
The & operator is used to get the address of a variable.
Pointers are used in dynamic memory allocation, function arguments, and array manipulation.
Structures
Structures (struct) are user-defined data types that group different types of variables under a single name. Structures are used to represent a record, for example, a student with attributes like name, roll number, and marks.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
//…
};
Example:
struct Student {
char name[50];
int roll_number;
float marks;
};
struct Student s1; // Declares a structure variable of type ‘Student’
Key Points:
Structures allow for the combination of different data types.
Structure members are accessed using the dot operator (.).
Structures can also contain arrays and pointers as members.
Unions
Unions are similar to structures but differ in how they store data. In a union, all members share the same memory location. The size of the union is equal to the size of its largest member.
Syntax:
union union_name {
data_type member1;
data_type member2;
//…
};
Example:
union Data {
int integer;
float floating_point;
char character;
};
union Data d1; // Declares a union variable of type ‘Data’
Key Points:
Unions are memory efficient, as they share memory among members.
Only one member can be accessed at a time, as modifying one member affects the others.
Functions
Functions in C are blocks of code designed to perform a specific task. Functions help in modularizing the code and promote code reusability.
Syntax:
return_type function_name(parameter_list) {
// function body
}
Example:
int add(int a, int b) {
return a + b;
}
int result = add(5, 3); // Calls the function ‘add’ with arguments 5 and 3
Key Points:
Functions can have zero or more parameters.
Functions can return a value or be void (no return value).
Functions can call other functions (including themselves, known as recursion).
Enumerations
Enumerations (enum) are user-defined data types consisting of integral constants. Each identifier in an enumeration is associated with a unique constant value.
Syntax:
enum enum_name {
constant1,
constant2,
//…
};
Example:
enum Weekday { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum Weekday today = Monday; // Declares a variable of enum type ‘Weekday’ and assigns it ‘Monday’
Key Points:
Enumerations make the code more readable by assigning names to integral constants.
By default, the enumeration constants start from 0 and increment by 1. This can be changed by explicitly assigning values.
User Defined Data Types
User-defined data types in C programming allow programmers to define their own data types based on existing ones, tailored to the needs of their programs. These types provide greater flexibility and help manage complex data. The primary user-defined data types in C are:
Structures (struct)
Unions (union)
Enumerations (enum)
Typedefs (typedef)
Structures (struct)
Structures in C allow you to group variables of different data types under a single name. This is particularly useful for representing a record or an entity that has multiple attributes, such as a student, an employee, or a book.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
//…
};
Example:
#include<stdio.h> //preprocessor directive
// Define a structure called Student
struct Student {
char name[50];
int roll_number;
float marks;
};
int main() {
// Declare a structure variable
struct Student student1;
// Assign values to the members of the structure
strcpy(student1.name, "John Doe");
student1.roll_number = 101;
student1.marks = 95.5;
// Access and print the members of the structure
printf("Name: %s\n", student1.name);
printf("Roll Number: %d\n", student1.roll_number);
printf("Marks: %.2f\n", student1.marks);
return 0;
}
Key Points:
Accessing Members: Use the dot operator (.) to access the members of a structure.
Memory Allocation: Each member has its own storage location; the total size of the structure is the sum of the sizes of its members (considering padding for alignment).
Nested Structures: Structures can contain other structures as members.
struct Address {
char city[30];
int zip;
};
struct Employee {
char name[50];
struct Address address; // Nested structure
};
Unions (union)
Unions in C are similar to structures but with one crucial difference: all members of a union share the same memory location. This means a union can only store one of its members’ values at a time.
Syntax:
union union_name {
data_type member1;
data_type member2;
//…
};
Example:
#include<stdio.h> //preprocessor directive
// Define a union called Data
union Data {
int integer;
float floating_point;
char character;
};
int main() {
// Declare a union variable
union Data data;
// Assign values to the union members
data.integer = 10;
printf("Integer: %d\n", data.integer);
data.floating_point = 22.5;
printf("Floating Point: %.2f\n", data.floating_point);
data.character = 'A';
printf("Character: %c\n", data.character);
return 0;
}
Key Points:
Shared Memory: All members share the same memory location. The size of the union is equal to the size of its largest member.
Accessing Members: Only one member can be accessed at a time, as modifying one member affects the others.
Use Cases: Unions are useful for memory-efficient storage when you know that only one of the members will be used at a time.
Enumerations (enum)
Enumerations (enum) provide a way to define a set of named integer constants. They are often used to assign names to integral constants, improving code readability.
Syntax:
enum enum_name {
constant1,
constant2,
//…
};
Example:
#include<stdio.h> //preprocessor directive
// Define an enumeration called Weekday
enum Weekday {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
int main() {
// Declare a variable of enum type
enum Weekday today;
today = Monday; // Assign a value to the enum variable
printf("Today is %d\n", today); // Outputs 1, since 'Monday' is at index 1
return 0;
}
Key Points:
Default Values: By default, the first constant has the value 0, and the rest are incremented by 1. This can be changed by explicitly assigning values.
enum Color {
Red = 1,
Green = 3,
Blue = 5
};
Use Cases: Enums are particularly useful for representing states, modes, options, and choices in a program.
Typedefs (typedef)
Typedef is a keyword used to create an alias or a new name for an existing data type. This is useful for simplifying complex declarations and making the code more readable.
Syntax:
typedef existing_data_type new_name;
Example:
#include<stdio.h> //preprocessor directive
typedef unsigned long ulong; // Create an alias ‘ulong’ for ‘unsigned long’
int main() {
ulong x = 1000; // Use the typedef alias
printf(“Value of x: %lu\n”, x);
return 0;
}
Key Points:
Simplifies Code: typedef can be used to simplify complex type declarations.
typedef struct {
int x;
int y;
} Point;
Point p1; // Now ‘Point’ can be used directly instead of ‘struct Point’
Readability: Improves readability and maintainability of code by providing meaningful names for data types.
Type Modifiers
C also provides type modifiers to alter the properties of the primitive data types:
- signed: Indicates that a variable can store both negative and positive values (this is the default for int and char).
- unsigned: Indicates that a variable can only store non-negative values (positive and zero).
- short and long: Used with int to define the range and storage size.
Size of Different Integers
Compiler | short | int | long |
16-bit(Turbo C/C++) | 2 | 2 | 4 |
32-bit(Visual Studio, gcc) | 2 | 4 | 4 |
Each of these integers has two further variations that is signed and unsigned. In the signed varety, the highest or leftmost bit stores the sign of the number 0, if the numer is positive, and 1, if the number is negative.
On the other hand, in unsigned al lthe bits are used to store the value of the number. These variables can be declared as shown below:
short signed int a;
short unsigned int i;
singed int j;
unsigned int k;
singed long int x;
unsinged long int y;
In these declarations, signed and int can be dropped. So following declarations would have served the same purpose as the ones made above:
short a;
short unsinged i;
int j;
unsinged k;
long int x;
unsinged long y;
Chars singed, unsigned
Similar to integers, chars also can be singed or unsigned. Both occupy one byte each, but have different ranges. To begin with, it might appear strange as to how a char can have a sign. Consider the statement:
signed char ch = ‘A’;
Here binary equivalent of the ASCII/Unicode value of ‘A’ is 65 gets stored in ch. And if 65’s binary can be stored, then -54’s binary can also be stored.
As with integers, singed is default. So signed char is same as char and has a range from -128 to +127. Likewise, an unsigned char has a rnge from 0 to 255.
Reals float, double, long double
A float occupies four bytes in memory and can range from -3.4e38 to +3.4e38. If this is insufficient, then C offers a double data type that occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308, A variable of type double can be declared as,
double population;
If the situation demands usage of real numbers that lie even beyond the range offered by double data type, then there exits a long double that can range from -1.7e4932 to +1.7e4932. A long double occupies 10 bytes in memory.
The essence of all the data types that we have learnt so far has been captured in given below:
Data Type | Range | Bytes | Format |
singed char | -128 to +127 | 1 | %c |
unsinged char | 0 to 255 | 1 | %c |
short singed int | -32768 to +32767 | 2 | %d |
short unsinged int | 0 to 65535 | 2 | %u |
singed int | -2147483648 to +2147483647 | 4 | %d |
unsinged int | 0 to 4294967295 | 4 | %u |
long singed int | -2147483648 to +2147483647 | 4 | %ld |
long unsigned int | 0 to 4294967295 | 4 | %lu |
float | -3.4e38 to +3.4e38 | 4 | %f |
double | -1.7e308 to +1.7e308 | 8 | %lf |
long double | -1.7e4932 to +1.7e4932 | 10 | %Lf |
In General Formula of Range of signed and unsigned
unsigned | 0 to 2^n – 1 |
signed | -2^(n-1) to 2^(n-1) -1 |
Cyclic Property of Data Types in C Programming
The cyclic property of data types in C programming refers to the behavior of certain data types when they exceed their maximum or minimum limits. When a variable of a specific data type (like int, char, etc.) exceeds its storage capacity, it wraps around to the opposite end of its range, effectively “cycling” through its range of possible values.
Explanation:
For integral data types in C (like char, int, short, long, etc.), each type has a specific range of values it can represent. When an arithmetic operation causes a variable to exceed this range:
For unsigned types, exceeding the maximum value causes the variable to wrap around to 0.
For signed types, exceeding the maximum or minimum value causes undefined behavior, but typically, it wraps around to the opposite extreme value.
Example of cyclic property for unsigned data tyeps:
Example of cyclic property for signed data tyeps:
Concepts of Cylic Property with help of below figure:
Examples:
Code1:
American Standard Code for Information Interchange (ASCII code)
ASCII (American Standard Code for Information Interchange) is a character encoding standard used in C programming (and many other languages and systems) to represent text. ASCII codes are used to represent characters such as letters, digits, punctuation marks, and control characters (e.g., newline, backspace) using numeric values.
Character | ASCII |
A | 65 |
B | 66 |
… | … |
Z | 90 |
a | 97 |
b | 98 |
… | … |
z | 122 |
0 | 48 |
1 | 49 |
… | … |
9 | 57 |
Frequently Asked Questions (FAQs)
What are the basic data types in C?
int (integer): Used to store integers (whole numbers).
char (character): Used to store single characters.
float (floating point): Used to store decimal numbers with single precision.
double (double floating point): Used to store decimal numbers with double precision.
void: Represents the absence of a type or no value.
What are derived data types in C?
Arrays: Collection of elements of the same type.
Pointers: Variables that store the memory address of another variable.
Structures (struct): User-defined data type that allows grouping variables of different types.
Unions (union): Similar to structures but shares the same memory location for all its members.
Enumerations (enum): User-defined data type consisting of named integer constants.
What is the difference between float and double?
What are short, long, signed, and unsigned qualifiers?
signed and unsigned qualifiers define whether a data type can store negative values or only positive values and zero. An unsigned int can store larger positive numbers than a signed int of the same size.
What is the size of int, char, float, and double in C?
int: 4 bytes
char: 1 byte
float: 4 bytes
double: 8 bytes
To get the exact size on your system, you can use the sizeof() operator in C.
How does a struct differ from a union?
union also allows the grouping of variables of different types, but all members share the same memory location, meaning the size of a union is equal to the size of its largest member.
What is the void data type used for?
Can we use bool data type in C?
What is a pointer data type in C?
What is the significance of sizeof() operator?
What is the range of char data type in C?
Signed char: -128 to 127
Unsigned char: 0 to 255
What is a volatile keyword in C?
What is the difference between const and volatile?
Learn More
Subscribe “JNG ACADEMY” for more learning best content.
Important Links
C Programming Language Lecture-01
C Programming Language Lecture-02
C Programming Language Lecture-03
C Programming Language Youtube Playlist
Thank You So Much, Guys… For Visiting our Website and Youtube.