π€ Unit 2: Data Types, Variables & Constants
Welcome back, coder-in-training! π
In this unit, weβll dive into the building blocks of C β data types, variables, and constants.
Think of these as the alphabet of programming: once you master them, you can form meaningful βsentencesβ (programs). β¨
π¦ Data Types in Cβ
Data types tell the computer what kind of data you want to store.
π Primary Data Typesβ
| Data Type | Keyword | Size (bytes) | Range | Format Specifier |
|---|---|---|---|---|
| Character | char | 1 | -128 to 127 | %c |
| Integer | int | 4 | -2,147,483,648 to 2,147,483,647 | %d or %i |
| Float | float | 4 | 3.4E-38 to 3.4E+38 | %f |
| Double | double | 8 | 1.7E-308 to 1.7E+308 | %lf |
| Void | void | 0 | No value | - |
π Data Type Modifiersβ
Modifiers tweak the size and range of basic data types:
| Modifier | Used With | Purpose |
|---|---|---|
short | int | Reduces size (2 bytes) |
long | int, double | Increases size |
signed | int, char | Allows negatives (default) |
unsigned | int, char | Only positive values |
short int a; // 2 bytes: -32,768 to 32,767
long int b; // 8 bytes: larger range
unsigned int c; // 0 to 4,294,967,295
unsigned char d; // 0 to 255
long long int e; // Very large numbers
π· Variablesβ
A variable is like a box π¦ with a label β you can store a value and change it later.
Declaration Syntaxβ
datatype variable_name;
// Examples
int age;
float salary;
char grade;
Initialization Stylesβ
- Step-by-step
- Direct
- Multiple
int age;
age = 25;
int age = 25;
int x, y, z;
int a = 10, b = 20, c = 30;
Naming Rules β ββ
β
Must start with a letter or underscore
β
Case sensitive (age, Age, AGE are different)
β No spaces, no special symbols, no keywords
// Good
int studentAge;
float total_salary;
char GRADE;
int _privateVar;
// Bad
int a;
int x1234;
int myVariableForStoringTheAgeOfStudent;
πΏ Constantsβ
Constants are values that cannot change once set. Like your date of birth π.
Types of Constantsβ
π’ Literalsβ
10, -50, 0x1A, 077 // Integers
3.14, -0.5, 2.5e3 // Floating
'A', '5', '\n' // Characters
"Hello" // Strings
π Symbolic Constantsβ
- #define
- const keyword
#define PI 3.14159
#define MAX_SIZE 100
#define NEWLINE '\n'
const int DAYS_IN_WEEK = 7;
const float GRAVITY = 9.81;
const char GRADE = 'A';
πΉ Escape Sequencesβ
| Sequence | Meaning |
|---|---|
\n | New line |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\' | Single quote |
\0 | Null terminator |
\b | Backspace |
\r | Carriage return |
β Operatorsβ
Operators are the tools π§ that let you work with values.
Arithmetic Operatorsβ
| Operator | Example | Meaning |
|---|---|---|
+ | a + b | Add |
- | a - b | Subtract |
* | a * b | Multiply |
/ | a / b | Divide |
% | a % b | Remainder |
- Integer Division
- Float Division
int result1 = 10 / 3; // Result = 3
float result2 = 10.0 / 3; // Result = 3.333...
Relational Operatorsβ
| Operator | Example | Meaning |
|---|---|---|
== | a == b | Equal |
!= | a != b | Not equal |
> | a > b | Greater |
< | a < b | Less |
>= | a >= b | Greater/Equal |
<= | a <= b | Less/Equal |
Logical Operatorsβ
| Operator | Example | True If... |
| -------- | --------------------- | --------------- | -------- | --- | --------- | ----------------- |
| && | (a > 5) && (b < 10) | Both true |
| | | | (a > 5) | | (b < 10) | At least one true |
| ! | !(a > 5) | Reverses result |
Assignment Operatorsβ
| Operator | Example | Equivalent |
|---|---|---|
= | a = 5 | Assign |
+= | a += 3 | a = a + 3 |
-= | a -= 3 | a = a - 3 |
*= | a *= 3 | a = a * 3 |
/= | a /= 3 | a = a / 3 |
%= | a %= 3 | a = a % 3 |
Increment & Decrementβ
- Pre-increment
- Post-increment
int a = 5, b;
b = ++a; // a = 6, b = 6
int a = 5, b;
b = a++; // b = 5, then a = 6
Conditional (Ternary)β
status = (age >= 18) ? "Adult" : "Minor";
int max = (a > b) ? a : b;
π Type Conversionβ
- Implicit
- Explicit (Casting)
int a = 10;
float b = 5.5;
float result = a + b; // a converted to float
int a = 10, b = 3;
float result = (float)a / b; // 3.333...
π Complete Exampleβ
#include <stdio.h>
#define PI 3.14159
int main() {
const int MAX_MARKS = 100;
int marks = 85;
float percentage;
char grade;
percentage = (float)marks / MAX_MARKS * 100;
grade = (percentage >= 90) ? 'A' :
(percentage >= 80) ? 'B' :
(percentage >= 70) ? 'C' :
(percentage >= 60) ? 'D' : 'F';
printf("=== Student Report Card ===\n");
printf("Marks: %d/%d\n", marks, MAX_MARKS);
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);
if (marks >= 40 && marks <= MAX_MARKS) {
printf("Status: PASS\n");
} else {
printf("Status: FAIL\n");
}
int bonus = 5;
marks += bonus;
printf("After bonus: %d\n", marks);
return 0;
}
π Quick Quizβ
What is the difference between pre-increment and post-increment?
- No difference
- Pre-increment increases before using, post-increment after β