(Learn C Programming for Beginners — Day 6)
Welcome to Day 6 of your 30-Day C Programming Adventure! 🚀
You can write code that works. That's great! But can you write code that others (and your future self) can understand?
Today isn't about learning new complex logic. It's about craftsmanship. We're going to learn the art of writing clean, readable, and professional C code.
By the end of this post, your code won't just run—it will shine.
🎯 Goals for Today
- Master Comments — How to leave notes in your code without breaking it.
- Escape Sequences — How to print special characters (like newlines and tabs).
- Code Style — Indentation, naming conventions, and best practices.
- Refactor a Program — Take messy code and make it beautiful.
📝 Comments: The "Sticky Notes" of Code
Comments are lines of text in your program that the compiler completely ignores. They are for humans only.
Why use them?
- Explanation: "This formula calculates the area of a circle."
- Debugging: Temporarily disable a line of code without deleting it.
- Documentation: "Author: Sad Adnan, Date: May 2025."
Two Types of Comments in C:
1. Single-line Comment (//)
Everything after // on that line is ignored.
int age = 25; // This is a variable for age
2. Multi-line Comment (/* ... */)
Everything between /* and */ is ignored, even across multiple lines.
/*
This program calculates the sum of two numbers.
Author: Sad Adnan
Date: May 2025
*/
🏃 Escape Sequences: The Special Characters
You've already used \n to create a new line. That's an escape sequence. It tells the compiler, "Hey, don't print the letter 'n', do something special instead!"
Here are the most useful ones:
| Sequence | Meaning | Example Output |
| :--- | :--- | :--- |
| \n | New Line | Moves cursor to the next line. |
| \t | Horizontal Tab | Adds a tab space (great for aligning columns). |
| \" | Double Quote | Prints a " inside a string. |
| \' | Single Quote | Prints a ' inside a char. |
| \\ | Backslash | Prints a single backslash \. |
Example:
printf("Hello\tWorld!\n"); // Hello World!
printf("She said, \"Hi!\"\n"); // She said, "Hi!"
printf("Path: C:\\Windows\n"); // Path: C:\Windows
🎨 Code Style: The Art of Indentation
C doesn't force you to indent your code (like Python does), but you absolutely should.
Bad Code (Hard to Read):
#include <stdio.h>
int main(){int x=10;if(x>5){printf("Big");}return 0;}
Good Code (Easy to Read):
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("Big");
}
return 0;
}
Best Practices:
- Indentation: Use 4 spaces (or 1 tab) for every block of code inside
{ }. - Spacing: Put spaces around operators (
a = b + cis better thana=b+c). - Naming: Use
camelCaseorsnake_casefor variables (studentAgeorstudent_age). - One Statement Per Line: Don't cram everything together.
💻 Let's Code: The "Receipt Printer"
Let's write a program that uses escape sequences to print a neatly formatted receipt.
Step 1: Create day6_style.c.
Step 2: Type this code:
/*
Project: Simple Receipt Printer
Description: Demonstrates escape sequences and clean code style.
*/
#include <stdio.h>
int main() {
// Declare variables
float item1Price = 12.50;
float item2Price = 4.99;
float total = item1Price + item2Price;
// Print the Receipt Header
printf("******************************\n");
printf("\tSTORE RECEIPT\n");
printf("******************************\n\n");
// Print Items with Tabs for alignment
printf("Item\t\tPrice\n");
printf("----\t\t-----\n");
printf("Coffee\t\t$%.2f\n", item1Price);
printf("Donut\t\t$%.2f\n", item2Price);
// Print Total
printf("\n------------------------------\n");
printf("TOTAL\t\t$%.2f\n", total);
printf("------------------------------\n");
printf("\nThank you for shopping!\n");
return 0;
}
Step 3: Compile and Run!
* gcc day6_style.c -o day6_style
* ./day6_style
Notice how \t helps align the prices perfectly? That's the power of escape sequences!
📝 Day 6 Summary
- Comments (
//,/* */) explain your code to humans. - Escape Sequences (
\n,\t,\") let you format output and print special characters. - Indentation & Spacing make your code readable and professional.
- Clean Code is just as important as working code.
🚀 Challenge for Tomorrow
Take your "Age Calculator" from Day 4 and:
1. Add comments explaining each step.
2. Use \t to align the output nicely.
3. Ensure your indentation is perfect.
Next Up: Day 7: Week 1 Review & Mini Project — We'll combine everything we've learned so far to build a fully functional Simple Calculator!
Found this helpful? Clap 👏 and follow to keep up with the adventure!
Comments
Post a Comment