- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Data Types
![]() Share with a Friend |
C Programming - C Data Types
C Data Types
In C programming, data types specify the type of data a variable can hold. They determine the size, range, and operations that can be performed on the variable.
Categories of Data Types
- Basic Data Types:
- int, float, char, double
- Derived Data Types:
- Arrays, Pointers, Structures, Unions
- Enumeration Data Type:
- enum
- Void Data Type:
- void
- Basic Data Types
| Type | Keyword | Size (bytes) | Range |
|---|---|---|---|
Integer |
int |
2 or 4 |
-32,768 to 32,767 (2 bytes) |
Float |
float |
4 |
~3.4E-38 to 3.4E+38 |
Character |
char |
1 |
-128 to 127 (signed) |
Double |
double |
8 |
~1.7E-308 to 1.7E+308 |
Examples
int age = 25;
float salary = 50000.50;
char grade = 'A';
double distance = 1234567.89;
- Derived Data Types
- a) Arrays
- Store multiple elements of the same type.
Example:
int numbers[5] = {1, 2, 3, 4, 5};
- b) Pointers
- Store the address of a variable.
Example:
int x = 10;
int *ptr = &x; // Pointer to x
- c) Structures
- Group different types of data.
Example:
struct Student {
int id;
char name[50];
float marks;
};
- d) Unions
- Share memory among variables.
Example:
union Data {
int i;
float f;
};
- Enumeration Data Type
- Used to define named integer constants.
Example:
enum Day { Sunday, Monday, Tuesday };
enum Day today = Monday;
- Void Data Type
- Represents "no value."
- Used in functions that do not return a value.
Example:
void displayMessage() {
printf("Hello, World!\n");
}
Modifiers in C
Data types can be modified to change their size and range using modifiers like:
- Signed
- Unsigned
- Short
- Long
Example
| Type | Size (bytes) | Range |
|---|---|---|
signed int |
4 |
-2,147,483,648 to 2,147,483,647 |
unsigned int |
4 |
0 to 4,294,967,295 |
short int |
2 |
-32,768 to 32,767 |
unsigned short int |
2 |
0 to 65,535 |
long int |
8 |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int |
8 |
0 to 18,446,744,073,709,551,615 |
Example Code: Data Types in Action
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
double pi = 3.14159265358979;
unsigned int score = 4294967295;
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
printf("Value of Pi: %.15lf\n", pi);
printf("Score: %u\n", score);
return 0;
}
Key Points
- Choose Data Types Wisely:
- Use int for general numbers, float/double for decimals, and char for characters.
- Memory Matters:
- Smaller data types save memory but have smaller ranges.
- Type Conversion:
- Implicit (automatic) or explicit (casting) conversions can occur between data types.
