Skip to main content

C Programming Day 6: Comments, Escape Sequences & Code Style - Writing Clean Code

(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

  1. Master Comments — How to leave notes in your code without breaking it.
  2. Escape Sequences — How to print special characters (like newlines and tabs).
  3. Code StyleIndentation, naming conventions, and best practices.
  4. 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:

  1. Indentation: Use 4 spaces (or 1 tab) for every block of code inside { }.
  2. Spacing: Put spaces around operators (a = b + c is better than a=b+c).
  3. Naming: Use camelCase or snake_case for variables (studentAge or student_age).
  4. 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!

CProgramming #CodingForBeginners #LearnToCode #Programming #Developer

Comments

Popular posts from this blog

Unlock C Programming in 30 Days

Your 30‑Day C Programming Series Click any day below to jump straight to that lesson (opens in a new tab): Day 0: Unlock C Programming in 30 Days Your ultimate self‑study guide introduction—your roadmap for the entire 30‑day journey. Read Day 0 Day 1: What is Programming & Setting Up Your C Lab Understand programming basics and install GCC + VS Code so you’re ready to code. Read Day 1 Day 2: Write Your First “Hello, World!” Learn the anatomy of a C program and compile & run your very first C code . Read Day 2 Day 3: Variables & Basic Data Types Discover how to store and print integers, floats, and characters in C . Read Day 3 Day 4: Interactive Input with scanf Make your programs listen—read numbers, floats, and chars from the user. Read Day 4

C Programming Day 3: Variables, Data Types & Constants - The "Boxes" of Programming

(Learn C Programming for Beginners — Day 3) Welcome back to your 30-Day C Programming Adventure ! 🚀 Yesterday, you wrote your very first "Hello, World!" program. You felt the power of making the computer speak. Today, we’re going to give your programs a memory. Imagine trying to cook a complex meal without any bowls or containers. You’d have ingredients everywhere! In programming, variables are those containers. They hold your data so you can use, change, and display it. Today, we unlock the power of Variables , Data Types , and Constants . By the end of this post, you'll be able to write programs that can store and manipulate information, not just print static text. 🎯 Goals for Today Understand Variables — What they are and how to create them. Master Data Types — Integers, floats, characters, and more. Learn Constants — Values that never change. Write Code — Create a program that stores and prints different types of data. 📦 What is a Varia...

C Programming Day 4: Input/Output - Talking to Your Computer

(Learn C Programming for Beginners — Day 4) Welcome to Day 4 of your 30-Day C Programming Adventure ! 🚀 So far, our programs have been a bit... quiet. We've told the computer what to store (variables) and what to show us ( printf ), but we haven't let the user say anything back. It's been a one-way conversation. Today, that changes. We are going to make your programs interactive . By the end of this post, you'll be able to write programs that ask questions, listen for answers, and respond accordingly. We're diving deep into printf (Output) and its best friend, scanf (Input). 🎯 Goals for Today Master printf — Learn how to format output beautifully. Unlock scanf — Learn how to take input from the user. Handle Common Pitfalls — Avoid the dreaded "skipped input" bug. Build an Interactive Program — Create a " Age Calculator " that talks to you. 📢 The Art of Speaking: printf Revisited You've used printf to print...