C Programming
Storage Classes; Command Line Arguments; Error Handling in C
C-CAT
Storage Classes
Storage Classes in C
| Storage Class | Scope | Lifetime | Default Value |
|---|---|---|---|
auto | Block/local | Till end of block | Garbage |
register | Block/local | Till end of block | Garbage |
static | Block or file | Entire program | 0 |
extern | File (global) | Entire program | 0 |
void count() {
static int call_count = 0; // retains value between calls!
call_count++;
printf("Called %d times\n", call_count);
}
int main() {
count(); // Called 1 times
count(); // Called 2 times
count(); // Called 3 times
return 0;
}
register
// HINT to compiler: store in CPU register for fast access
// Compiler may ignore this hint
register int i;
for (register int i = 0; i < 1000000; i++) {
// tight loop — register hint may make slightly faster
}
extern
// file1.c
int global_counter = 0;
// file2.c
extern int global_counter; // declare that it exists elsewhere
global_counter++; // use it
Command Line Arguments
What are Command Line Arguments?
Arguments passed to main() when running the program from terminal.
int main(int argc, char *argv[]) {
// argc = argument count (includes program name)
// argv = argument vector (array of strings)
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
// Run: ./program hello world 42
// Output:
// Number of arguments: 4
// argv[0] = ./program
// argv[1] = hello
// argv[2] = world
// argv[3] = 42
Using Command Line Arguments
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <number>\n", argv[0]);
return 1;
}
int num = atoi(argv[1]); // convert string to int
printf("Binary representation of %d:\n", num);
print_binary(num);
return 0;
}
// Run: ./binary 42
Error Handling in C
errno and perror
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
// method 1: perror prints error message
perror("fopen"); // fopen: No such file or directory
// method 2: errno and strerror
printf("Error code: %d\n", errno);
printf("Error: %s\n", strerror(errno));
return 1;
}
fclose(fp);
return 0;
}
Return Value Checking (Good Practice)
// Always check return values from library functions
int result = scanf("%d", &n);
if (result != 1) {
printf("Invalid input!\n");
return 1;
}
int *p = malloc(100 * sizeof(int));
if (p == NULL) {
printf("Memory allocation
failed!\n");
exit(EXIT_FAILURE);
}
int r = fclose(fp);
if (r != 0) {
perror("fclose");
}
Storage Duration and Linkage
An automatic local object is created when execution enters its block and ceases to exist when the block ends. A static local retains its value for the entire program while remaining visible only in its block. A file-scope object has static storage duration. extern declares an object or function defined elsewhere. register is a historical request for fast storage and does not guarantee a processor register.
Scope controls where a name can be used. Linkage controls whether declarations in different scopes or source files denote the same entity. Internal linkage, commonly produced by file-scope static, keeps a name inside one translation unit. External linkage allows definitions to be shared through declarations in headers.
Arguments and Error Reporting
A hosted C program may receive argc and argv. argc is the number of argument strings and argv[0] normally identifies the program. The array ends with a null pointer. Numeric arguments require checked conversion; strtol reports range and parsing position more reliably than atoi.
Library functions often signal failure through a return value and may set errno. The value of errno is meaningful only after a function reports failure. perror combines a prefix with the corresponding message. Programs should release acquired resources along every failure path and return a nonzero status to the environment when the requested operation could not be completed.
Continue learning
Related notes
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.