Lecture Notes: Basics of C Programming
Page 1: Introduction to C
What is C?
• C is a general-purpose, procedural programming language.
• Developed by Dennis Ritchie at Bell Labs in 1972.
• Known for its performance, low-level memory access, and wide usage in system
programming, such as operating systems (e.g., UNIX).
Features of C
• Simple and efficient
• Fast execution
• Supports structured programming
• Portable
• Rich library of built-in functions
Structure of a C Program
c
CopyEdit
#include <stdio.h> // preprocessor directive
int main() {
printf("Hello, World!"); // output
return 0; // exit status
}
Components Explained
• #include <stdio.h> – includes standard input/output library
• int main() – main function, entry point
• printf() – function to display output
• return 0 – ends program, 0 means success
Page 2: Variables, Data Types, and Operators
Variables
• Containers to store data values.
c
CopyEdit
int age = 25;
float pi = 3.14;
Data Types
Type Example Description
int 10 Integer numbers
float 3.14 Decimal numbers
char 'A' Single character
double 2.718281 Double precision float
Constants
• Use const keyword:
c
CopyEdit
const int MAX = 100;
Operators
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
Page 3: Control Structures
Conditional Statements
c
CopyEdit
if (condition) {
// code
} else {
// code
}
Switch Case
c
CopyEdit
switch(choice) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Invalid");
}
Loops
• For loop:
c
CopyEdit
for(int i=0; i<5; i++) {
printf("%d\n", i);
}
• While loop:
c
CopyEdit
int i = 0;
while(i < 5) {
printf("%d\n", i);
i++;
}
• Do-while loop:
c
CopyEdit
int i = 0;
do {
printf("%d\n", i);
i++;
} while(i < 5);
Page 4: Functions and Arrays
Functions
• Block of reusable code.
c
CopyEdit
int add(int a, int b) {
return a + b;
}
• Called using:
c
CopyEdit
int sum = add(5, 3);
Arrays
• Store multiple values of the same type.
c
CopyEdit
int numbers[5] = {1, 2, 3, 4, 5};
• Access elements using index:
c
CopyEdit
printf("%d", numbers[0]); // prints 1
Strings
• Arrays of characters ending with \0.
c
CopyEdit
char name[] = "Alice";
Page 5: Pointers and File I/O
Pointers
• Variables that store memory addresses.
c
CopyEdit
int x = 10;
int *ptr = &x;
printf("%d", *ptr); // Dereferencing
Why Use Pointers?
• Dynamic memory allocation
• Efficient array handling
• Functions with reference
File I/O
c
CopyEdit
FILE *fptr = fopen("[Link]", "w");
fprintf(fptr, "Hello File!");
fclose(fptr);
• Modes: "r" (read), "w" (write), "a" (append)
Conclusion
C is a foundational language that teaches core programming concepts, useful for embedded
systems, OS development, and more. Mastering its basics sets the stage for learning more advanced
languages and technologies.