Variables and Data Types
## Learning Objectives
- Declare and initialize variables
- Understand primitive types
- Master type conversion
- Learn about constants
## Variables
### Declaration
```c
int age; // Declaration
age = 25; // Assignment
int score = 100; // Declaration + Initialization
```
### Naming Rules
- Start with letter or underscore
- Can contain numbers (after first character)
- Cannot use reserved words
- Case-sensitive
```c
int age; // Valid
int _count; // Valid
int $total; // Valid (but avoid)
int 2ndPlace; // Invalid
int class; // Invalid (reserved word)
```
## Primitive Data Types
### Integer Types
| Type | Size | Range |
|------|------|-------|
| `char` | 1 byte | -128 to 127 |
| `short` | 2 bytes | -32,768 to 32,767 |
| `int` | 2-4 bytes | System-dependent |
| `long` | 4-8 bytes | System-dependent |
| `long long` | 8 bytes | Very large |
```c
char c = 'A';
short s = 32000;
int i = 2000000;
long l = 9000000000L;
long long ll = 9000000000000000000LL;
```
### Unsigned Types
```c
unsigned int u = 100;
unsigned char uc = 255;
unsigned long ul = 4000000000UL;
```
### Floating-Point Types
| Type | Size | Typical Range |
|------|------|---------------|
| `float` | 4 bytes | 1.2e-38 to 3.4e38 |
| `double` | 8 bytes | 2.2e-308 to 1.8e308 |
| `long double` | 16 bytes | Even larger |
```c
float pi = 3.14f;
double precise = 3.14159265359;
long double ld = 3.14159265358979323846L;
```
### Character Type
```c
char grade = 'A';
char newline = '\n';
char ascii = 65; // ASCII value
```
### Boolean Type (C99)
```c
#include
bool isActive = true;
bool hasPermission = false;
```
## Type Sizes
```c
#include
int main(void) {
printf("char: %zu bytes\n", sizeof(char));
printf("int: %zu bytes\n", sizeof(int));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
return 0;
}
```
## Initialization
### Default Values
**Warning:** Local variables are not automatically initialized!
```c
int x; // Garbage value (undefined)
int y = 0; // Explicitly initialized
```
### Initialization Styles
```c
int a = 10; // Direct initialization
int b = (int)10; // Cast initialization
int c = {10}; // Brace initialization (C99)
int d = 0; // Zero initialization
```
## Type Conversion
### Implicit (Automatic)
```c
int i = 100;
long l = i; // int to long (automatic)
double d = i; // int to double (automatic)
char c = 'A';
int x = c; // char to int (automatic)
```
### Explicit (Cast)
```c
double d = 3.99;
int i = (int)d; // Truncates to 3
float f = 3.14f;
int x = (int)f; // 3
```
### Integer Promotion
```c
char c1 = 10;
char c2 = 20;
int sum = c1 + c2; // Promoted to int
```
## Constants
### const Qualifier
```c
const double PI = 3.14159;
const int MAX_SIZE = 100;
// PI = 3.14; // Error! Cannot modify const
```
### #define Macro
```c
#define PI 3.14159
#define MAX_SIZE 100
#define TRUE 1
#define FALSE 0
```
### enum
```c
enum Color { RED, GREEN, BLUE };
enum Color myColor = RED; // myColor = 0
enum Day { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
```
## Limits
```c
#include
#include
#include
int main(void) {
printf("INT_MAX: %d\n", INT_MAX);
printf("INT_MIN: %d\n", INT_MIN);
printf("UINT_MAX: %u\n", UINT_MAX);
printf("FLT_MAX: %e\n", FLT_MAX);
return 0;
}
```
## Variables Scope
### Block Scope
```c
int global = 10; // File scope
int main(void) {
int local = 20; // Block scope
for (int i = 0; i < 5; i++) { // Loop scope
// i only exists here
}
return 0;
}
```
### Global vs Local
```c
int counter = 0; // Global
void increment(void) {
counter++; // Accesses global
}
int main(void) {
int counter = 10; // Local (shadows global)
printf("%d\n", counter); // 10 (local)
printf("%d\n", ::counter); // Error in C (use different names)
return 0;
}
```
## Static Variables
```c
void counter(void) {
static int count = 0; // Retains value between calls
count++;
printf("Count: %d\n", count);
}
int main(void) {
counter(); // Count: 1
counter(); // Count: 2
counter(); // Count: 3
return 0;
}
```
## Summary
- Variables store data values
- Integer types: char, short, int, long, long long
- Floating types: float, double, long double
- Use `const` or `#define` for constants
- Cast for explicit type conversion
- Local variables are not auto-initialized
- `static` variables retain value between calls
- Use `` and `` for type limits
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →