Sunday, January 12, 2025
HomeComputer ScienceData Types in C

Data Types in C

Data types in C define the type of data that a variable can hold. They determine the size and type of memory that a variable occupies and the operations that can be performed on it.

1. Basic Data Types

These are the fundamental data types provided by the C language.

Data Type Size (in bytes) Description
int 2 or 4 Stores integers (whole numbers).
float 4 Stores single-precision floating-point numbers.
double 8 Stores double-precision floating-point numbers.
char 1 Stores a single character.
_Bool 1 Stores Boolean values (true or false).

2. Derived Data Types

These are constructed from basic data types.

Type Description
Array Collection of elements of the same data type.
Pointer Stores the address of another variable.
Structure Combines variables of different data types.
Union Combines variables of different types sharing memory.
Function A block of code that performs a specific task.
See also  Wallpaper (Computer Desktops/Backgrounds)

3. Enumerated Data Type

  • Defined using the enum keyword.
  • Used to assign names to integral constants for better readability.

Example:

c
enum Color { RED, GREEN, BLUE };

4. Void Data Type

  • Represents the absence of any value.
  • Commonly used for functions that do not return a value.

Example:

c
void printMessage() {
printf("Hello, World!");
}

Modifiers for Basic Data Types

Modifiers alter the size and range of basic data types.

See also  Create JAVA PDF
Modifier Used With Description
signed int, char Allows storage of both positive and negative values.
unsigned int, char Stores only positive values.
short int Reduces the size of an integer.
long int, double Increases the size of the data type.

Ranges of Common Data Types

Data Type Range
char -128 to 127 (signed)
unsigned char 0 to 255
int -32,768 to 32,767 (16-bit)
unsigned int 0 to 65,535 (16-bit)
float 3.4E-38 to 3.4E+38
double 1.7E-308 to 1.7E+308

Examples of Variable Declarations

c
int age = 25; // Integer variable
float pi = 3.14; // Floating-point variable
char grade = 'A'; // Character variable
unsigned int count = 500; // Unsigned integer

Why Data Types Are Important

  1. Memory Efficiency: Helps allocate the right amount of memory.
  2. Type Safety: Ensures correct operations on variables.
  3. Performance: Optimizes code execution by using appropriate types.
See also  List of Input Devices

Understanding data types in C is fundamental to writing efficient and error-free programs.

Previous article
Next article
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x