📖 1. Introduction to C Programming
Work through the lesson in this order: What is C? → Why was it created? → How does a C program become a running program? → How do we read our first program? → How do we avoid common beginner mistakes?
C is a general-purpose, procedural programming language used to write programs as a series of steps and functions. It also lets programmers work directly with memory and bits.
Your .c file is source code. It contains the instructions you write and edit. Development tools translate these instructions and connect the required program parts to create an executable. The operating system can then load and run that executable.
C source file (.c)
↓
Preprocessing
↓
C source after directives are handled
↓
Compilation
↓
Assembly / object code
↓
Linking with required libraries and object files
↓
Executable program
↓
Operating system loads it
↓
Running process
- It explains why a program can have a compile error before it ever runs.
- It explains why a missing function definition can produce a linker error.
- It explains why
#include, compilation and linking are different jobs. - It prepares you for later topics such as pointers, memory, files and system programming.
C was developed at Bell Labs in the early 1970s, primarily by Dennis Ritchie, during the development of the Unix operating system.
The name “Dennis Ritchie” is useful to remember, but also understand why C was developed. Programmers needed functions and other features to organise large programs, along with access to memory and hardware for system software.
Higher-level programming ideas Machine-oriented control
│ │
│ functions, loops, structures │
└─────────────── C ────────────────────┘
│
pointers, bits,
memory, addresses
That balance is why C is sometimes called a “middle-level language” in textbooks. This is an informal description, not an official classification in the C standard.
C helps you understand how programs use data and memory. For example, arrays store related values, while pointers let you work with addresses. These ideas are useful when you later learn data structures and system software.
| Area | Why C is useful | What You Learn |
|---|---|---|
| 🖥️ Operating systems | Efficient native code and low-level control | Memory, processes, system interfaces |
| 🔌 Embedded systems | Small runtime requirements and hardware access | Registers, bits, memory and timing |
| ⚙️ Compilers & runtimes | Fast native implementation | Translation and execution concepts |
| 📚 Native libraries | Portable interfaces and performance | Functions, pointers and data representation |
| 🎓 Programming education | Makes hidden concepts visible | Memory, types, addresses and execution |
Many interview questions about pointers, arrays, memory allocation, structures, recursion, bitwise operations and data structures become easier when your C foundation is strong.
| Characteristic | What it means | Simple example |
|---|---|---|
| Procedural | Logic is commonly divided into functions and ordered steps. | main() calls other functions. |
| Compiled | Source code is translated before normal execution. | gcc program.c -o program |
| Statically typed | Variables and expressions have types. These types determine which operations are valid and how values can be converted. | int age; |
| Portable | Programs written using standard C rules can often be compiled on another system. Machine-specific details still need checking. | The same standard C program can be compiled on different platforms. |
| Efficient | C allows close control over how data is stored and processed. Program design and compiler choices also affect performance. | Direct integer and pointer operations. |
| Manual resource management | Programmers must release resources when required, such as memory obtained through dynamic allocation. | malloc() followed by free(). |
The language rules are standardized, but some details can differ between systems. For example, an int is not required to occupy four bytes everywhere. Separate standard guarantees, which the language requires, from implementation details, which depend on the compiler or system.
#include <stdio.h>
int main(void)
{
printf("Hello, CodeBhavya!\n");
return 0;
}
| Part | What it does | Beginner meaning |
|---|---|---|
#include <stdio.h> | Provides declarations from the standard input/output header. | “I need the declarations for standard I/O facilities such as printf.” |
int main(void) | Defines the starting function for a hosted program, the usual C program run with operating-system and library support. | “This is where the program starts.” |
{ ... } | Marks the body of the function. | “These statements belong to main.” |
printf(...) | Calls a standard library function that writes formatted output to standard output, usually the terminal here. | “Display this message.” |
\n | Represents a newline escape sequence in the string. | “Move the cursor to the next line.” |
; | Terminates the expression statement. | “This statement is finished.” |
return 0; | Ends main and reports successful completion. This does not print a zero. | “The program finished normally.” |
#include <stdio.h> → make I/O declarations available
↓
int main(void) → define the starting function
↓
{ → begin function body
↓
printf(...) → perform output
↓
return 0; → finish successfully
↓
} → end function body
A compiler, linker, library, and header have different roles. The table below shows how each one helps build a program.
| Component | Main responsibility | Think of it as... |
|---|---|---|
| 🧪 Compiler | Checks C source and translates it, commonly into assembly code. | A translator + checker |
| 🔗 Linker | Combines compiled program parts and connects function calls with the definitions they need. | A connector |
| 📦 Library | Provides reusable functions and other facilities that a program can use. | A toolbox |
| 📄 Header | Provides declarations and related information needed by source files. | Information about functions and other facilities |
#include <stdio.h> does not itself perform linking.
The header tells the compiler how facilities such as printf are declared. The library supplies their compiled implementation. If a required definition is missing during the build, you may see linker errors.
A common C build process has four stages. One command may perform all four, so you do not always see the intermediate files. Running the finished program is a separate step.
#includeprogram.c
│
├── Preprocessor ──→ expanded source
│
├── Compiler ──────→ assembly/object representation
│
├── Assembler ─────→ object file
│
└── Linker ────────→ executable
│
↓
Operating system
│
↓
Running process
If someone asks, “Does the compiler directly execute your C program?”, the answer is no. Compilation translates source; execution happens later when the resulting program is loaded and run.
When a program fails, first ask “At what stage did it fail?” This is much better than changing random lines.
| Problem | Example | Usually discovered when? |
|---|---|---|
| 🔴 Syntax / translation error | A statement is missing a semicolon or another required part. | During translation |
| 🟠 Type / constraint diagnostic | A value has an incompatible type where the language requires the compiler to report a problem. | During translation |
| 🟣 Link error | A function is called, but its required definition is missing from the build. | During linking |
| 🔵 Runtime problem | A problem occurs while the program runs, such as using an invalid memory address. | During execution |
| 🟢 Logical error | The program runs but gives the wrong answer, for example by adding length and width instead of multiplying them to find area. | After execution / testing |
Does it compile?
│
├── No → inspect syntax/types/translation diagnostics
│
└── Yes
↓
Does it link?
│
├── No → inspect missing definitions/libraries
│
└── Yes
↓
Does it run correctly?
│
├── No → inspect runtime behavior
│
└── Yes, but wrong answer → inspect logic
The C standard defines the language and standard library. Compilers may also provide extra features that are not part of standard C.
Use standard C for the examples in this course unless a lesson identifies a compiler-specific feature. This makes it easier to use the same source with different compilers.
Read a short program one part at a time. Identify its data, input, calculations, decisions, and output using the cards below.
INPUT ↓ PROCESSING ↓ DECISION / LOOP ↓ OUTPUT ↓ CLEANUP (when resources require it)
If you see int a, b;, first identify the data. If you see scanf, identify the input. If you see a + b, identify the processing. If you see if, identify the decision. If you see printf, identify the output.
| Misconception | Correct understanding |
|---|---|
| “The compiler runs my program.” | The compiler translates the source. The resulting program is executed later. |
“#include links the library.” | #include is handled by preprocessing. Linking happens later. |
| “Every computer has the same C type sizes.” | Some sizes and representations are implementation-dependent. |
| “If it compiles, it must be correct.” | A program can compile successfully and still contain logical errors or undefined behavior. |
| “C automatically cleans every resource.” | Dynamic memory and many resources require explicit management. |
“main() is just another random function.” | In a hosted C program, main is the designated program entry function. |
| “C is only for old programs.” | C remains relevant in systems, embedded software, libraries, runtimes and performance-sensitive code. |
1.12 C Standards and Compiler Modes
C has several editions, including C90, C99, C11, C17 and C23. The selected compiler mode affects which features are accepted. For example, declaring a counter inside a for statement is allowed from C99 onward. Use C17 mode for the example below so the selected rules are clear. C23 features need support from your compiler.
Complete C program:
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 3; ++i)
printf("%d ", i);
putchar('\n');
return 0;
}
Expected output:
1 2 3
Step-by-step explanation:
- Save the source as demo.c.
- On GCC or Clang, compile with: gcc -std=c17 -Wall -Wextra -pedantic demo.c -o demo
- Read any compiler warnings before running the program. If the build succeeds, run it and check that it prints 1 2 3 followed by a newline.
1.13 Quick Revision
- C is a general-purpose procedural language with strong support for low-level programming.
- A C source file is translated before it runs.
- The basic toolchain story is preprocess → compile → assemble → link → run.
main()is the entry function of a hosted C program.printf()is a standard library function used for formatted output.- Do not confuse header inclusion with library linking.
- Always distinguish syntax/translation errors, link errors, runtime problems and logical errors.
- C is especially valuable because it makes types, memory, addresses, representation and execution easier to see.
1.14 Practice Questions
1.15 🎯 5 Beginner Programming Problems
Try each problem yourself first. Use 💻 Solve It Yourself to write and test your C program. Use Hint only when needed, and Show Program if you want to study the complete solution.
Write a C program to print your name.
Input: No input.
Output: Display your name.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
printf("Venu");
return 0;
}
Write a C program to add two numbers.
Input: Two integers.
Output: Print their sum.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a + b);
return 0;
}
Write a C program to calculate the area of a circle.
Input: Radius of the circle.
Output: Print the area rounded to two decimal places.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
const double PI = 3.14159;
double radius;
double area;
scanf("%lf", &radius);
area = PI * radius * radius;
printf("Area = %.2f", area);
return 0;
}
Write a C program to check whether a number is positive or negative.
Input: One integer.
Output: Print Positive, Negative, or Zero.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
if (n > 0)
printf("Positive");
else if (n < 0)
printf("Negative");
else
printf("Zero");
return 0;
}
Write a C program to find the largest of two numbers.
Input: Two integers.
Output: Print the larger value.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
if (a > b)
printf("%d", a);
else
printf("%d", b);
return 0;
}
1.16 Key Takeaway
Don't try to memorize C programs.
Instead, understand:
Input → Processing → Output
This thinking pattern will help you solve programming problems in C, Data Structures and eventually coding interviews.
🎤 Introduction to C — Interview Questions
Use these questions to revise the definition, history, features, uses, and importance of C.
💡 Introduction to C — Extra Tips
- Be able to explain What is C? in simple words before writing code.
- Practice writing and explaining a small C program without copying the solution.
- While debugging, check syntax first and then trace values step by step for Introduction to C.
- For interviews, connect syntax with behavior: know what the program does, not only how the statement looks.
✍️ Introduction to C — Extra Practice Questions
- Change the first program to print your name, then build and run it.
- Predict the output of the C standards example before running it. Explain why the loop prints three numbers.
- Remove the semicolon after printf in the first program. Read the compiler message, restore the semicolon, and build again.
- Change the greeting to display two lines using a newline escape sequence. Explain the role of each statement.
- Explain how a compiler and linker have different jobs when building the first program.