C++ Data Types and Variables – CS201 VU Guide
Understanding data types and variables is the foundation of learning any programming language, including C++. If you're a CS201 student at Virtual University (VU), this guide will break down the topic in simple terms, enriched with examples, memory insights, and best practices to help you master the basics.
🔹 What Are Variables in C++?
A variable is a named storage that holds data. Think of it like a labeled box that stores values.
int age = 21;
float weight = 65.5;
char grade = 'A';
🔹 Why Are Data Types Important?
- Tell the compiler how much memory to allocate
- Specify what kind of data can be stored
- Define what operations are allowed
🔹 Categories of Data Types in C++
- Basic (Primitive) Data Types
- Derived Data Types
- Enumeration Data Types
- User-defined Data Types
1. Basic Data Types
Data Type | Description | Size | Example |
---|---|---|---|
int | Integer | 4 bytes | int marks = 90; |
float | Decimal (single precision) | 4 bytes | float pi = 3.14; |
double | Decimal (double precision) | 8 bytes | double g = 9.8; |
char | Character | 1 byte | char grade = 'A'; |
bool | Boolean | 1 byte | bool isPassed = true; |
2. Derived Data Types
- Arrays:
int numbers[5];
- Pointers:
int *ptr = #
- Functions:
int sum(int a, int b);
- References:
int& ref = x;
3. Enumeration (enum) Data Types
enum Day { SUNDAY, MONDAY, TUESDAY };
Day today = MONDAY;
4. User-defined Data Types
struct Student {
int id;
char name[50];
};
🔹 Declaring and Initializing Variables
int age;
int score = 95;
int x = 5, y = 10, z = 15;
Naming Rules
- Must start with a letter or underscore
- Case-sensitive
- No spaces or special characters
- Cannot be a reserved keyword
🔹 Constants in C++
const float PI = 3.14159;
🔹 Memory Allocation and Size
cout << sizeof(int); // Outputs 4
Signed vs Unsigned
unsigned int u = 4294967295;
🔹 Type Conversion (Casting)
Implicit Conversion
int x = 5;
float y = x;
Explicit Conversion
float x = 3.14;
int y = (int)x;
🔹 Variable Scope
- Local: Inside functions
- Global: Outside all functions
- Static: Retains value between calls
void demo() {
static int counter = 0;
counter++;
cout << counter;
}
🔹 Best Practices
- Use meaningful names
- Initialize variables
- Use
const
for constants - Use the correct data type
- Comment your variables
🔹 Common Mistakes
Mistake | Description |
---|---|
Uninitialized variables | Contain garbage values |
Wrong type operations | e.g., adding int to string |
Overflows | Exceeding max range of data type |
Typos | Wrong variable names |
🔹 Sample Program
#include <iostream>
using namespace std;
int main() {
const float PI = 3.14159;
int radius = 5;
float area = PI * radius * radius;
cout << "Radius: " << radius << endl;
cout << "Area: " << area << endl;
return 0;
}
🔹 FAQ
- Q: Which type for age?
A: Useint
. - Q: Can I store numbers in
char
?
A: Yes, but they represent ASCII codes. - Q: Is
double
better thanfloat
?
A: More precise, but takes more memory. - Q: Are global variables bad?
A: Not always, but they can lead to poor structure.
🔹 Conclusion
Mastering data types and variables in C++ is crucial for CS201 and your programming journey. This guide covered primitive types, constants, memory allocation, scope, and common practices to help you write clean, reliable code.
0 Comments