Variables, Data Types, and Operators
30 minC provides fundamental data types that represent different kinds of values in memory. Basic types include integers (int, short, long, long long), floating-point numbers (float, double, long double), and characters (char). Each type has a specific size and range of values it can represent. Understanding data types is essential for writing correct C programs and managing memory efficiently.
Variables in C must be explicitly declared with their type before use. Variable declarations specify the type and name, and optionally an initial value. C is statically typed, meaning types are checked at compile time. Variable names must follow C naming rules (letters, digits, underscores, starting with letter or underscore). Understanding variable declaration enables proper program structure.
C provides a rich set of operators for manipulating data. Arithmetic operators (+, -, *, /, %) perform mathematical operations. Relational operators (==, !=, <, >, <=, >=) compare values. Logical operators (&&, ||, !) perform boolean logic. Bitwise operators (&, |, ^, ~, <<, >>) manipulate individual bits. Understanding operators enables you to express complex logic efficiently.
Type casting allows explicit conversion between different data types. Implicit conversions occur automatically in some contexts (like int to float), while explicit casts use (type) syntax. Type casting is essential when you need to convert between incompatible types or ensure specific behavior. Understanding type casting enables proper type handling and prevents unexpected behavior.
Integer types vary in size: char (1 byte), short (typically 2 bytes), int (typically 4 bytes), long (4 or 8 bytes), and long long (8 bytes). Unsigned variants (unsigned int, etc.) don't store negative values but have larger positive ranges. Floating-point types include float (4 bytes, ~7 decimal digits), double (8 bytes, ~15 decimal digits), and long double (typically 10 or 16 bytes). Understanding type sizes enables appropriate type selection.
Operator precedence determines the order of operations in expressions. Understanding precedence is crucial for writing correct expressions. Parentheses can override precedence. Common precedence issues include arithmetic before comparison, and logical AND before OR. Understanding operator precedence prevents subtle bugs in expressions.
Key Concepts
- C has fundamental data types: int, float, double, char.
- Variables must be declared with explicit types before use.
- Operators include arithmetic, relational, logical, and bitwise.
- Type casting allows conversion between different data types.
- Operator precedence determines evaluation order.
Learning Objectives
Master
- Understanding C data types and their sizes
- Declaring and initializing variables
- Using arithmetic, relational, and logical operators
- Performing type casting and conversions
Develop
- Understanding type systems and memory representation
- Writing type-safe C programs
- Understanding operator behavior and precedence
Tips
- Use appropriate types: int for integers, float/double for decimals.
- Initialize variables: int x = 0; to avoid undefined behavior.
- Use explicit casts: (float)int_var for type conversions.
- Understand precedence: use parentheses to clarify expression order.
Common Pitfalls
- Using wrong data types, causing overflow or precision loss.
- Not initializing variables, causing undefined behavior.
- Not understanding operator precedence, writing incorrect expressions.
- Mixing signed and unsigned types, causing unexpected comparisons.
Summary
- C provides fundamental data types for different value kinds.
- Variables must be explicitly declared with types.
- Operators enable data manipulation and comparisons.
- Type casting enables conversion between types.
Exercise
Demonstrate different data types, operators, and type casting.
#include <stdio.h>
int main() {
// Integer types
int age = 25;
short smallNum = 100;
long bigNum = 1000000L;
// Floating point types
float height = 1.75f;
double pi = 3.14159265359;
// Character type
char grade = 'A';
// Arithmetic operations
int a = 10, b = 3;
printf("Addition: %d + %d = %d
", a, b, a + b);
printf("Subtraction: %d - %d = %d
", a, b, a - b);
printf("Multiplication: %d * %d = %d
", a, b, a * b);
printf("Division: %d / %d = %d
", a, b, a / b);
printf("Modulus: %d %% %d = %d
", a, b, a % b);
// Type casting
float result = (float)a / b;
printf("Float division: %d / %d = %.2f
", a, b, result);
// Relational operators
printf("Is %d > %d? %s
", a, b, a > b ? "Yes" : "No");
return 0;
}
Exercise Tips
- Use sizeof() to check type sizes: printf("%zu", sizeof(int));
- Use unsigned types for non-negative values: unsigned int count = 0;
- Be careful with integer division: use (float) for decimal results.
- Use % operator for remainders: useful for cycles and patterns.