Overview
This lecture introduces C programming, covering environment setup, core concepts, variables, user input/output, control structures, arrays, functions, pointers, file operations, memory management, and practical projects.
Setting Up the C Development Environment
- Install an IDE (e.g., Visual Studio Code) and a C compiler (like GCC).
- Download and configure necessary extensions: C/C++ and CodeRunner.
- Check compiler installation: gcc --versionfor Windows/Linux,clang --versionfor Mac.
- On Windows, set the compiler path in environment variables.
Writing and Running Your First C Program
- Start with #include <stdio.h>for input/output functions.
- Every C program requires a mainfunction as the entry point.
- Use return 0;inmainto indicate successful execution.
- Use printf()to display output, ending statements with;.
Comments in C
- Single-line comments use //.
- Multi-line comments use /* ... */.
- Comments are ignored by the compiler.
Variables and Data Types
- Variables are named containers for values.
- Common data types: int(whole numbers),float(decimals, 6-7 digits),double(decimals, ~15 digits),char(single character), string (array ofchar),bool(true/false, needs#include <stdbool.h>).
- Format specifiers in printf:%d(int),%f(float),%lf(double),%c(char),%s(string),%d(bool as int).
Format Specifiers and Output Formatting
- Control output width, justification, leading zeros, and precision using modifiers within format specifiers (e.g., %7.2f).
Arithmetic and Assignment Operators
- Use +,-,*,/,%(modulus) for math operations.
- ++/- --increment/decrement.
- +=,- -=,- *=,- /=are augmented assignments.
User Input
- Use scanf()orfgets()for input; clear input buffer as needed.
- Strings with spaces need fgets()and buffer size calculation.
- Always initialize variables to avoid undefined behavior.
Control Structures
- if,- else if,- elsefor decision making; use logical operators (- &&,- ||,- !).
- switchis preferred over multiple- if elsefor fixed integer or char values.
- Nested ifstatements enable multi-level decisions.
Loops
- whileand- do-whileloops repeat code based on condition.
- forloops are ideal for known number of iterations.
- Use breakto exit loops,continueto skip current iteration.
- Nested loops can handle matrices or generate tables.
Arrays and Strings
- Array declaration: type name[size];.
- Strings are arrays of char; array of strings is a 2D array or array of pointers.
- Always initialize arrays to zero or default values.
Functions
- Functions are reusable code blocks, defined with a return type, name, and parameters.
- Arguments are values passed to functions; parameters are received inside the function.
- Use returnto send values back;voidmeans no return.
- Function prototypes declare functions before use for type checking.
Pointers and Memory Management
- A pointer stores the address of a variable; declare with *.
- The &operator gets a variable’s address;*dereferences a pointer to get the value.
- Use malloc(),calloc(),realloc()for dynamic memory allocation;free()to release memory.
Structs, Typedefs, and Enums
- structgroups related variables; dot- .or arrow- ->accesses members.
- typedefcreates type aliases for readability.
- enumcreates named integer constants for improved clarity.
File Input/Output
- Use FILE *pointers andfopen(),fprintf(),fscanf(),fclose()for file operations.
- Always check if the file opened successfully and close files after use.
Practical Projects Covered
- Shopping cart, mad libs, weight/temperature converter, calculator, quiz game, number guessing, rock-paper-scissors, digital clock, and banking program.
Key Terms & Definitions
- IDE — Integrated Development Environment; workspace for coding.
- Compiler — Translates C code into machine code.
- Variable — Named data storage.
- Format Specifier — Symbol in printf/scanffor data type formatting.
- Pointer — Variable storing a memory address.
- Struct — Custom data type grouping related variables.
- Enum — User-defined type of named integer constants.
- Dynamic Memory — Runtime-allocated memory via malloc,calloc,realloc.
Action Items / Next Steps
- Practice: Write at least one C program using variables, input/output, control structures, loops, functions, arrays, and pointers.
- Review: Complete sample projects such as calculator, quiz game, and digital clock.
- Reading: Review C standard library documentation for string, math, and memory functions.