90009
90009
1. Introduction
2. sizeof Operator in C++
- What is sizeof?
- Importance of sizeof in Memory Management
- Usage and Examples
3. Unicode in C++
- What is Unicode?
- Importance of Unicode in Modern Programming
- How to Use Unicode in C++
- Examples
4. Type Conversion in C++
- What is Type Conversion?
- Importance of Type Conversion
- Implicit Type Conversion
- Explicit Type Conversion (Type Casting)
- Type Conversion Between Numeric and Non-Numeric Types
- Examples
5. Conclusion
6. References
1
Sizeof Operator in C++
What is sizeof?
The sizeof operator in C++ is a compile-time operator that
returns the size (in bytes) of a data type or an object. It is used
to determine how much memory a variable, type, or structure
occupies in memory.
Importance of sizeof in Memory Management
• Efficient Memory Allocation: Helps in allocating the right
amount of memory dynamically using malloc, calloc, or
new.
• Avoiding Overflows: Ensures that buffer sizes are
correctly set to prevent overflows.
• Portability: The size of data types may vary across
different systems. Using sizeof ensures that programs work
consistently across platforms.
• Structure Alignment: Helps in understanding the memory
layout of structures and avoiding memory wastage due to
padding.
Usage and Examples
1. Finding the Size of Data Types
2
Output (depends on system):
3
Unicode in C++
What is Unicode?
Unicode is a universal character encoding standard that
represents almost all characters and symbols used in written
languages worldwide. It assigns a unique code to each
character, ensuring consistent representation across different
platforms and programming languages.
Unicode supports multiple encoding formats, including:
• UTF-8 (variable-length, commonly used)
• UTF-16 (used in Windows and Java)
• UTF-32 (fixed-length, less common)
Importance of Unicode in Modern Programming
• Supports Internationalization: Allows programs to handle
multiple languages and scripts.
• Cross-Platform Compatibility: Ensures consistency
across different operating systems.
• Standardized Encoding: Avoids issues related to ASCII
and other legacy encodings.
• Supports Special Characters & Emojis: Essential for
modern applications like social media, messaging apps,
and databases.
4
How to Use Unicode in C++
C++ supports Unicode through wide characters and string types:
• wchar_t (wide character type)
• char16_t (UTF-16 encoding)
• char32_t (UTF-32 encoding)
• std::wstring, std::u16string, std::u32string (wide strings)
• u8"..." (UTF-8 string literals in C++20)
Examples
1. Using Wide Characters (wchar_t)
5
3-Using UTF-8 Strings (C++20)
6
Types of Type Conversion
1. Implicit Type Conversion (Automatic Type Conversion)
Implicit type conversion (also known as type promotion) is
automatically performed by the compiler when a smaller data
type is assigned to a larger data type without loss of data.
Rules of Implicit Conversion
• Integer to Floating-Point Conversion
int → float → double
• Character to Integer Conversion
char → int
• Lower to Higher Precision Type Conversion
short → int → long → float → double
Example: Implicit Type Conversion
Output:
7
2. Explicit Type Conversion (Type Casting)
Explicit type conversion (also known as type casting) is
manually performed by the programmer to force a conversion
between incompatible data types.
Methods of Explicit Type Casting
1. C-Style Cast: (type)value
2. C++ Functional Cast: type(value)
3. Static Cast: static_cast<type>(value)
4. Reinterpret Cast, Dynamic Cast, and Const Cast
(Advanced cases)
Example: Explicit Type Casting
8
Output:
9
String to Numeric Conversion
Using stoi(), stof(), etc., from <string> library:
Output:
10
Conclusion
Understanding these concepts is important for writing optimized, secure,
and robust C++ programs. By applying best practices, programmers can
enhance the efficiency and reliability of their code.
References
1. Bjarne Stroustrup, "The C++ Programming Language."
2. C++ Documentation: https://cplusplus.com
3. C++ Standard Library Guide
11