Data Types in C Programming Language

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

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

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

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

Represents single-precision floating-point numbers (numbers with a fractional part). It is typically 4 bytes in size.

  • float
  • double
  • long double

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.

  • signed
  • unsigned

Represents the absence of type. It is used for functions that do not return a value or to specify generic pointers.

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 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 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.

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.
Compilershortintlong
16-bit(Turbo C/C++)224
32-bit(Visual Studio, gcc)244
Based on the size, the range of values that they can store would vary. A 2 byte integer can take values from -32768 to +32767, whereas a 4-byte integer can take values from -2147483648 to +2147483647.

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:

In these declarations, signed and int can be dropped. So following declarations would have served the same purpose as the ones made above:

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:

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.

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,

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:

unsigned0 to 2^n – 1
signed-2^(n-1) to 2^(n-1) -1
This is the general formula to finding the range of signed or unsigned for interger or character datatypes.

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.

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.

Code1:

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.

CharacterASCII
A65
B66
Z90
a97
b98
z122
048
149
957
This table is of important ASCII Code like Alphabets uppercase, lowercase and digits.

What are the basic data types in C?

The basic data types in C are:
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?

Derived data types are constructed from the basic data types. Examples include:
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?

float represents single-precision floating-point numbers (typically 4 bytes), whereas double represents double-precision floating-point numbers (typically 8 bytes). double can represent a larger range of values with more precision compared to float.

What are short, long, signed, and unsigned qualifiers?

short and long are used to define the size of an integer data type. short usually takes 2 bytes, while long can take 4 or 8 bytes.
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?

The size of data types can vary depending on the system and compiler, but typically:
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?

struct allows the grouping of variables of different types under a single name. Each member in a struct has its own storage location.
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?

The void data type is used to specify that a function does not return a value or that a pointer has no specific type. For example, void is used in the declaration of functions that perform an operation but do not return any value.

Can we use bool data type in C?

The bool data type is not a standard type in C (before C99). However, C99 introduced _Bool as a standard boolean type. To use bool in C, include the header , which defines bool as an alias for _Bool.

What is a pointer data type in C?

A pointer is a variable that stores the memory address of another variable. Pointers are declared using the * operator. For example, int *ptr; declares a pointer to an integer.

What is the significance of sizeof() operator?

The sizeof() operator is used to determine the size, in bytes, of a data type or variable. It is useful for memory allocation and for understanding the architecture-dependent sizes of types.

What is the range of char data type in C?

The range of char depends on whether it is signed or unsigned:
Signed char: -128 to 127
Unsigned char: 0 to 255

What is a volatile keyword in C?

The volatile keyword is used to tell the compiler that a variable’s value may change at any time, without any action being taken by the code the compiler finds nearby. It is commonly used in embedded systems and hardware-level programming.

What is the difference between const and volatile?

const is used to make a variable’s value constant, meaning it cannot be modified after initialization. volatile, on the other hand, indicates that a variable’s value may change unexpectedly, preventing the compiler from optimizing code that accesses it.
Previous

Learn More

Subscribe “JNG ACADEMY” for more learning best content.

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.

Leave a Comment