C Programming: Notes & Examples
Pointers, arrays, functions, structures — foundational C with detailed code examples.
Why learn C
C gives deep control over memory and performance. It is foundational for operating systems, embedded software, compilers, and performance-critical libraries.
Learning C builds strong low-level reasoning that helps with C++, Rust, systems programming, and interview problem solving.
Basic C program structure
#include <stdio.h>
int main(void) {
printf("Hello, Rishtaara!\n");
return 0;
}Primitive data types
- int for whole numbers.
- float and double for decimal values.
- char for single characters.
- Use sizeof(type) to inspect memory footprint.
Operators in C
#include <stdio.h>
int main(void) {
int a = 10, b = 3;
printf("Sum: %d\n", a + b);
printf("Remainder: %d\n", a % b);
printf("a > b: %d\n", a > b);
return 0;
}if-else and switch
Control statements drive branching logic. Use switch when comparing one variable against multiple discrete values.
int marks = 82;
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 75) {
printf("Grade B\n");
} else {
printf("Grade C\n");
}for, while, and do-while
- for is best when iteration count is known.
- while suits condition-driven loops.
- do-while runs at least once.
for (int i = 0; i < 3; i++) {
printf("for: %d\n", i);
}
int count = 0;
while (count < 3) {
printf("while: %d\n", count);
count++;
}Function declarations and definitions
C uses function prototypes to declare signatures before use. This helps the compiler validate argument and return types.
#include <stdio.h>
int add(int a, int b); // prototype
int main(void) {
printf("%d\n", add(4, 5));
return 0;
}
int add(int a, int b) {
return a + b;
}Pass by value understanding
- C passes arguments by value by default.
- Use pointers when a function must modify caller data.
- Keep function responsibilities narrow and testable.
Addresses and dereferencing
Pointers store memory addresses. The '&' operator gets an address and '*' dereferences it to access the value at that address.
#include <stdio.h>
int main(void) {
int value = 25;
int *ptr = &value;
printf("value=%d\n", value);
printf("address=%p\n", (void*)ptr);
printf("dereference=%d\n", *ptr);
return 0;
}Pointer safety tips
- Initialize pointers before dereferencing.
- Avoid returning pointers to local stack variables.
- Set freed pointers to NULL to reduce accidental reuse.
Array fundamentals
- Arrays store same-type elements contiguously.
- Indexes start at 0.
- Bounds checking is manual in C.
String handling with char arrays
#include <stdio.h>
#include <string.h>
int main(void) {
int scores[3] = {78, 85, 92};
char name[20] = "Rishtaara";
printf("First score: %d\n", scores[0]);
printf("Name length: %zu\n", strlen(name));
return 0;
}Using struct for domain modeling
Structures let you group related fields under one custom type. They are key for representing records like students, employees, and products.
#include <stdio.h>
struct Student {
char name[30];
int grade;
float marks;
};
int main(void) {
struct Student s = {"Ishita", 12, 91.5f};
printf("%s scored %.1f\n", s.name, s.marks);
return 0;
}typedef and nested structs
- typedef improves readability for custom types.
- Structs can contain other structs.
- Combine structs with pointers for dynamic data models.
Project outcome
- Capture student details with structs.
- Store multiple records in arrays.
- Search and print records by criteria.
- Practice modular function design.
Starter project code
#include <stdio.h>
typedef struct {
int id;
char name[40];
float marks;
} Student;
int main(void) {
Student students[2] = {
{1, "Asha", 88.5f},
{2, "Kabir", 91.0f}
};
for (int i = 0; i < 2; i++) {
printf("%d %s %.1f\n", students[i].id, students[i].name, students[i].marks);
}
return 0;
}Key Takeaways
- C teaches memory-level reasoning and performance-oriented coding.
- Strong basics in variables, operators, and control flow are essential.
- Functions and structs improve code organization and reuse.
- Pointers, arrays, and strings are central C interview topics.
- Mini projects help convert syntax knowledge into problem-solving skill.
Frequently Asked Questions
- Is C still used in modern software?
- Yes. C remains critical in embedded systems, operating systems, firmware, networking stacks, and high-performance libraries.
- Why are pointers important in C?
- Pointers provide direct memory access, which enables efficient data structures, dynamic memory management, and low-level system interactions.
- Should I learn C before C++?
- Learning C first gives strong fundamentals in memory and procedural programming, which makes many C++ concepts easier to understand later.