C Programming Tutorial 54 of 206
Introduction & Setup
- Introduction to C
- History of C
- Features of C
- Applications of C
- Advantages and Limitations of C
- C Standards and Implementations
- How C Programs Run
- Setting Up C Development Environment
- Your First C Program
- Basic Syntax of C
- Structure of a C Program
- Compilation Process in C
Tokens & Basics
- Tokens in C
- Keywords in C
- Identifiers in C
- Constants in C
- Variables in C
- Scope Rules in C
- Storage Classes in C
- Comments in C
Data Types & I/O
- Data Types in C
- Primitive Data Types
- Derived Data Types
- Type Modifiers
- Type Conversion in C
- Type Casting in C
- sizeof Operator
- Input and Output in C
- printf() in C
- scanf() in C
- Character I/O: getchar and putchar
- String I/O: gets/fgets and puts
- Format Specifiers in C
Operators & Expressions
- Operators in C
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment and Decrement Operators
- Conditional Operator
- Comma Operator
- sizeof and Address Operators
- Operator Precedence and Associativity
- Expression Evaluation in C
- Short-Circuit Evaluation
Control Flow
- Decision Making in C
- if Statement
- if-else Statement
- else-if Ladder
- Nested if
- switch Statement
- Loops in C
- for Loop
- while Loop
- do-while Loop
- Nested Loops
- break Statement
- continue Statement
- goto Statement
Functions
- Functions in C
- Function Declaration and Definition
- Function Arguments
- Return Values
- Call by Value
- Call by Reference Using Pointers
- Parameter Passing Techniques
- Main Function in C
- Recursion in C
- Types of Recursion
- Inline Functions
- Nested Functions
- Function Pointers
- Variadic Functions
Arrays
- Arrays in C
- Declaration and Initialization of Arrays
- Accessing Array Elements
- Traversing an Array
- Passing Arrays to Functions
- Array Parameters and Pointers
- One-Dimensional Arrays in C
- Two Dimensional Arrays
- Multidimensional Arrays in C
- Passing 2D Arrays to Functions
- Array of Pointers
- Variable Length Arrays
Strings
- Strings in C
- String Declaration and Initialization
- String Length
- String Traversal
- String Comparison
- String Copy
- String Concatenation
- Substring Search
- String Tokenization
- String Reverse
- Palindrome String in C
- String Pattern Searching
- String Literals
Pointers
- Pointers in C
- Address and Dereference Operators
- Pointer Arithmetic
- Pointers and Arrays
- Pointer to Pointer
- Void Pointers
- Null Pointers
- Dangling Pointers
- Wild Pointers
- Constant Pointers and Pointer to Constant
- Function Pointers
- Array of Pointers
- Pointers to Functions
- Pointers to Structures
- Pointer Type Casting
Structures & Unions
- Structures in C
- Structure Members
- Array of Structures
- Nested Structures
- Pointers to Structures
- Passing Structures to Functions
- Structure Alignment and Padding
- Self-Referential Structures
- Bit Fields in Structures
- Unions in C
- Structure vs Union
- Enumeration in C
- typedef in C
Dynamic Memory & Memory
- Program Memory Layout in C
- Stack and Heap in C
- malloc()
- calloc()
- realloc()
- free()
- Dynamic Arrays
- Memory Leaks
- Dangling Memory
- Double Free
- Use After Free
- Memory Safety Best Practices
Preprocessor & Headers
- C Preprocessor
- Header Files in C
- #include Directive
- #define and Macros
- Function-like Macros
- Conditional Compilation
- #ifdef, #ifndef and #endif
- Predefined Macros
- Pragma Directive
- Include Guards
File Handling
- File Handling in C
- File Pointers
- fopen() and fclose()
- Reading a File
- Writing to a File
- Appending to a File
- fgetc() and fputc()
- fgets() and fputs()
- fprintf() and fscanf()
- Binary File Handling
- EOF, getc() and feof()
- Read/Write Structures to Files
- Delete a File
- File Error Handling
Error Handling & Debugging
- Error Handling in C
- perror()
- strerror()
- errno
- Assertions in C
- Debugging C Programs
- Common C Runtime Errors
- Divide by Zero Handling
- goto for Error Handling
Date, Time & Misc
- Date and Time in C
- time() Function
- clock() Function
- Random Numbers in C
- Linkage in C
- External and Static Linkage
- Storage Duration
- Integer Overflow and Undefined Behavior
- Implementation-Defined vs Undefined Behavior
Advanced C
- Generic Selection (_Generic)
- Variadic Functions
- Input/Output System Calls
- Signals in C
- Signal Handling
- Socket Programming in C
- Multithreading in C
- Threads and Synchronization
- Atomic Operations
- Memory-Mapped Files
- Low-Level Systems Programming
Practice & Projects
- C Programming Examples
- C Exercises
- C Fundamental Practice Problems
- C Functions Practice Problems
- C Compound Data Type Practice Problems
- C Pattern Programs
- C Array Programs
- C String Programs
- C Pointer Programs
- C Structure and Union Programs
- C File Handling Projects
- C Mini Projects
- C Interview Questions
- Data Structures and Algorithms in C
For Loop
for Loop — part of the C Programming Tutorial.
What Is a for Loop in C?
A for loop is used to repeatedly execute a block of code while a condition is true.
It is especially useful when you know, or can define, how many times you want to repeat an operation.
Basic Syntax for (initialization; condition; update) { // code to execute }
The three parts are:
Initialization — runs once before the loop starts. Condition — checked before every iteration. Update — runs after each iteration. Simple Example #include <stdio.h>
int main() { for (int i = 1; i <= 5; i++) { printf("%d\n", i); }
return 0;
} Output 1 2 3 4 5 How It Works i = 1 ↓ Check i <= 5 ↓ Execute loop body ↓ i++ ↓ Check condition again ↓ ...
When i becomes 6, the condition i <= 5 is false, so the loop stops.
Printing Even Numbers
A for loop can be combined with an increment of 2.
#include <stdio.h>
int main() { for (int i = 2; i <= 10; i += 2) { printf("%d\n", i); }
return 0;
}
Output:
2 4 6 8 10 Counting Backward
The update expression can also decrease the counter.
for (int i = 5; i >= 1; i--) { printf("%d\n", i); }
Output:
5 4 3 2 1 Nested for Loops
A for loop can be placed inside another for loop. This is called a nested loop.
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { printf("%d %d\n", i, j); } }
The inner loop completes all its iterations for each iteration of the outer loop.
Nested loops are commonly used for patterns, matrices, and two-dimensional arrays.
Infinite for Loop
If the condition is omitted, the loop can run indefinitely.
for (;;) { printf("Running...\n"); }
This is called an infinite loop.
It should generally be used only when the program has another mechanism to stop the loop.
break and continue break
break immediately exits the loop.
for (int i = 1; i <= 10; i++) { if (i == 5) break;
printf("%d\n", i);
}
Output:
1 2 3 4 continue
continue skips the remaining code in the current iteration and moves to the next iteration.
for (int i = 1; i <= 5; i++) { if (i == 3) continue;
printf("%d\n", i);
}
Output:
1 2 4 5 Common Mistakes Forgetting the update for (int i = 1; i <= 5;) { printf("%d\n", i); }
i never changes, so the loop does not terminate normally.
Using the wrong condition for (int i = 1; i < 5; i++)
prints 1 through 4, not 5.
Use:
i <= 5
when 5 should also be included.
Key Takeaway
A C for loop is useful when you need to repeat code based on a counter or condition.
Remember the structure:
for (initialization; condition; update)
For example:
for (int i = 1; i <= 5; i++) { printf("%d\n", i); }
Next: Learn about the while loop and when to use it instead of a for loop.
Related
-
While Loop
Learn how the while loop in C repeatedly executes a block of code while a condition remains true.
-
For Loop
Learn how for loops help DSA algorithms repeat operations, traverse data, control iterations, and solve problems with predictable time complexity.
-
Floyd's Triangle
Learn how to generate Floyd's Triangle using nested loops and understand the row-by-row numbering pattern.
-
Palindrome String in C
Learn how to check whether a string is a palindrome in C using character comparison, loops, and simple examples.