Variables and Constants
Variables can be classified as global or local, each with distinct scopes and lifetimes.
Scope
- Local Variable: Accessible only within the block where it is declared (e.g., inside
{}). - Global Variable: Accessible throughout the entire program. To use across files, declare with
extern(e.g.,extern int value;).
Lifetime
- Local Variable: Created upon entering its scope and destroyed upon exiting.
- Global Variable: Exists for the duration of the program's execution.
Constants in C come in four forms:
- Literal Constants: Direct values like numbers (
42), characters ('x'), or strings ("text"). - Const Variables: Variables made read-only using the
constqualifier.
int main() {
const int fixed = 50;
// fixed = 60; // This line would cause a compilation error
printf("Value: %d", fixed);
return 0;
}
- Macro Constants: Defined using
#definepreprocessor directive.
#define LIMIT 100
int main() {
int counter = LIMIT;
printf("Counter: %d", counter); // Outputs 100
return 0;
}
- Enumeration Constants: Defined within an
enumto represent a set of named integer constants.
enum Direction { NORTH, EAST, SOUTH, WEST };
int main() {
enum Direction heading = EAST;
printf("Heading code: %d", heading); // Outputs 1
return 0;
}
Strings, Escape Sequences, and Comments
Strings
A string is a sequence of characters terminated by a null character \0. The strlen() function calculates the length excluding the null terminator.
#include <string.h>
int main() {
char str1[] = "Hello"; // Automatically includes '\0'
char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Length: %zu", strlen(str1)); // Outputs 5
return 0;
}
Escape Sequences
Special character combinations that represent non-printable characters or change interpretation.
int main() {
printf("Path: C:\\Users\\Doc\\file.txt\n"); // Prints backslashes
printf("Tab\tSeparated\n"); // Inserts a tab
printf("Octal: \101\n"); // Prints 'A' (ASCII 65 in octal)
printf("Hex: \x42\n"); // Prints 'B' (ASCII 66 in hexadecimal)
return 0;
}
Comments
- Multi-line:
/* comment text */ - Single-line:
// comment text
Control Flow: Selection and Loops
Selection with if-else
int main() {
int choice = 0;
printf("Enter 1 to continue, 0 to exit: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Proceeding...\n");
} else {
printf("Exiting.\n");
}
return 0;
}
Loops with while
int main() {
int attempts = 0;
while (attempts < 5) {
printf("Attempt #%d\n", attempts + 1);
attempts++;
}
printf("Loop finished.\n");
return 0;
}
Functions and Arrays
Functions
Functions encapsulate reusable code blocks.
int calculateSum(int x, int y) {
return x + y;
}
int main() {
int a = 10, b = 20;
int result = calculateSum(a, b);
printf("Sum: %d", result); // Outputs 30
return 0;
}
Arrays
Arrays store collections of elements of the same type, indexed from 0.
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int idx = 0; idx < 5; idx++) {
printf("Element %d: %d\n", idx, numbers[idx]);
}
return 0;
}
Basic Operators
C provides various operators for arithmetic, comparison, assignment, and logical operations, which are fundamental for building expressions and controlilng program logic.