← Back to C Course | Chapter 12: Advanced Topics | Lesson 18 of 20

C Code Style & Best Practices

Good code style means writing neat, readable code with clear names and sensible structure, like tidy handwriting. It makes programs easier for everyone to understand.

Clean Indentation

Consistent indentation has zero effect on how the compiler interprets your code, but it has a huge effect on how quickly a human — including your future self — can visually parse nested blocks, matching braces, and control flow at a glance.

Example: Clean Indentation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Check whether 1
	if (1) {
		// Print formatted text to the screen
		printf("Properly indented block");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Meaningful Variable Names

Choosing descriptive variable names like totalScore instead of x or ts costs almost nothing to type but saves real time later, since the name itself documents what the variable holds without requiring a comment.

Short single-letter names are still fine for tight, conventional contexts like loop counters (i, j, k).

Example: Meaningful Variable Names

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare totalScore and set it to 95
	int totalScore = 95;
	// Print formatted text to the screen
	printf("%d", totalScore);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Centralizing Constants

Scattering unexplained numeric literals like 86400 or 3.14159 directly inside your logic makes the code hard to understand and hard to change safely; giving each one a name via #define or a const variable turns a mystery number into self-documenting code and means you only have to update it in one place.

Example: Centralizing Constants

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SECONDS_PER_DAY (replaced by the preprocessor)
#define SECONDS_PER_DAY 86400
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", SECONDS_PER_DAY);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Modularity

Keeping each function focused on a single, well-defined task makes it easier to test, easier to reuse elsewhere, and easier to reason about in isolation, whereas one enormous function that does five unrelated things becomes progressively harder to modify safely as it grows.

Example: Function Modularity

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Avoiding Magic Numbers

A magic number is any unexplained literal value embedded directly in your code whose meaning isn't obvious from context, and replacing it with a named constant (like MAX_ATTEMPTS instead of a bare 3) makes the intent of that value clear to anyone reading the code later.

Example: Avoiding Magic Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_ATTEMPTS (replaced by the preprocessor)
#define MAX_ATTEMPTS 3
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", MAX_ATTEMPTS);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using inconsistent indentation so the block structure is hard to see.
  2. Using vague names like x and tmp for important values.
  3. Repeating magic numbers in many places instead of naming them with const or #define.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.