- 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 sizeof Operator
![]() Share with a Friend |
C Programming - C sizeof Operator
C sizeof Operator
The sizeof operator in C is used to determine the size, in bytes, of a data type, variable, or expression. It is an important operator in C programming as it helps in memory management and in writing portable code.
Syntax
C
sizeof(data_type)
sizeof(variable)
sizeof(expression)
- data_type: A data type (e.g., int, float, char).
- variable: A variable whose size is to be determined.
- expression: An expression whose size is to be determined.
Key Features of sizeof
- Compile-Time Evaluation: The sizeof operator is evaluated at compile time, meaning it does not consume any runtime resources. It returns a constant value representing the size.
- Return Type: It returns the result as size_t, which is an unsigned integer type defined in stddef.h.
- Parentheses: When using sizeof with a data type, parentheses are required, but when used with a variable or expression, parentheses are optional.
Examples
- Using sizeof with Data Types
C
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of char: %zu bytes\n", sizeof(char));
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
- Using sizeof with Variables
C
#include <stdio.h>
int main() {
int a = 5;
float b = 3.14;
char c = 'A';
printf("Size of a: %zu bytes\n", sizeof(a));
printf("Size of b: %zu bytes\n", sizeof(b));
printf("Size of c: %zu bytes\n", sizeof(c));
return 0;
}
Output:
Size of a: 4 bytes
Size of b: 4 bytes
Size of c: 1 byte
- Using sizeof with Arrays
C
#include <stdio.h>
int main() {
int arr[10];
// Size of the whole array
printf("Size of arr: %zu bytes\n", sizeof(arr));
// Size of one element in the array
printf("Size of one element in arr: %zu bytes\n", sizeof(arr[0]));
// Number of elements in the array
printf("Number of elements in arr: %zu\n", sizeof(arr) / sizeof(arr[0]));
return 0;
}
Output:
Size of arr: 40 bytes
Size of one element in arr: 4 bytes
Number of elements in arr: 10
sizeof and Pointers
sizeof can also be used with pointers, and it will return the size of the pointer itself, not the size of the memory it points to.
Example:
C
#include <stdio.h>
int main() {
int a = 5;
int *ptr = &a;
printf("Size of pointer: %zu bytes\n", sizeof(ptr));
printf("Size of data pointed to by pointer: %zu bytes\n", sizeof(*ptr));
return 0;
}
Output:
Size of pointer: 8 bytes // (On 64-bit system)
Size of data pointed to by pointer: 4 bytes // (For an int variable)
Use in Structures
The sizeof operator can be used to determine the size of structures, including padding introduced for alignment.
Example:
C
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p;
printf("Size of struct Person: %zu bytes\n", sizeof(p));
return 0;
}
Output:
Size of struct Person: 60 bytes // Padding may be included for alignment
Common Pitfalls
- sizeof on Expressions: The sizeof operator can be used on expressions, but it does not evaluate them.
C
int a = 5;
printf("Size of a + 3: %zu bytes\n", sizeof(a + 3)); // size of int, not the result
- Using sizeof on Dynamic Memory: When using malloc or similar functions, sizeof only gives the size of the pointer, not the memory allocated.
C
int *ptr = malloc(10 * sizeof(int));
printf("Size of ptr: %zu\n", sizeof(ptr)); // Only the size of the pointer, not the memory it points to
Applications of sizeof
- Memory Allocation: Ensure the correct amount of memory is allocated when using malloc or similar functions.
C
int *arr = malloc(10 * sizeof(int)); // Allocating memory for 10 integers
- Portability: Ensure your program works across different platforms by accounting for different data type sizes.
- Optimizing Data Structures: Use sizeof to check the size of structures and arrays to optimize memory usage.
Summary
- The sizeof operator is a compile-time operator that helps determine the memory size of data types, variables, or expressions.
- It is essential for memory management, data structure optimization, and ensuring portability across different systems.
- It is useful when working with arrays, structures, and dynamic memory allocation.
