Introduction
In our last chapter, you wrote your very first "Hello World" program. You communicated directly with the computer, and it responded exactly as you instructed. However, as a software engineer, you are rarely coding just for the machine—you are coding for other humans, too.
When you build large applications, your code can grow to thousands of lines. If another developer (or even you, six months later) looks at that code, they need to understand what it does. This is where Comments come in. Furthermore, to truly master C, you must understand the microscopic building blocks that make up your code, known as Tokens.
In this guide, we will explore how to write effective comments, break down the six types of C tokens, and look at the industry best practices that separate amateur coders from professionals.
What are Comments in C?
Comments are notes written in the code for humans to read. When the C compiler runs your program, it completely ignores anything marked as a comment. Comments do not affect the speed or size of your final program.
There are two ways to write comments in C:
1. Single-Line Comments ( // )
If you only need to write a brief note, use two forward slashes. Everything on that specific line after the // will be ignored by the compiler.
// This is a single-line comment
printf("Hello\n"); // This prints Hello to the screen
2. Multi-Line Comments ( /* ... */ )
If you need to write a detailed explanation that spans several paragraphs, you use a forward slash and an asterisk /* to start the comment, and an asterisk and a forward slash */ to end it.
/*
This program was created by NeoGyan.
It calculates the total marks of a student.
Do not modify this section without permission.
*/
Understanding C Tokens
If you look at an English sentence, you know it is made of words, punctuation marks, and spaces. In C programming, the smallest individual unit of code is called a Token. A C program is simply a collection of tokens put together following specific grammar rules (syntax).
There are exactly 6 types of tokens in C:
1. Keywords
Keywords are reserved words that have a special, predefined meaning in C. You cannot use these words for anything else. There are 32 standard keywords in C. Examples include int, return, if, else, and while.
2. Identifiers
Identifiers are the names you create yourself! When you create a variable, a function, or an array, the name you give it is an identifier. For example, if you create a variable named studentAge, that name is an identifier.
3. Constants
Constants are fixed values that do not change during the execution of the program. For example, the number 100, or the decimal 3.14.
4. Strings
A string is a sequence of characters wrapped in double quotes. In your first program, "Hello, World!\n" was a string token.
5. Special Symbols
These are characters that have special structural meaning, such as curly braces { }, parentheses ( ), commas ,, and semicolons ;.
6. Operators
Operators are symbols that perform mathematical or logical operations. Examples include + (addition), - (subtraction), and = (assignment).
Rules for Naming Identifiers (Variables & Functions)
When you create your own names (identifiers) in C, you must follow strict rules, otherwise the compiler will throw an error:
- They can only contain letters (a-z, A-Z), digits (0-9), and underscores ( _ ).
- They must begin with a letter or an underscore. They cannot begin with a number (e.g.,
1stPrizeis invalid, butprize1is valid). - You cannot use a C keyword (like
intorreturn) as an identifier. - They are case-sensitive.
Totalandtotalare considered two completely different identifiers.
Code Examples
/*
Program: Token and Comment Demonstration
Author: NeoGyan Developer
*/
#include <stdio.h>
int main() {
// This is an identifier (age) and a constant (25)
int age = 25;
// The line below uses a string and special symbols
printf("The user's age is %d\n", age);
return 0; // End of the program
}
Code Output
The user's age is 25
Code Explanation
/* ... */: A multi-line comment providing metadata about the program. The compiler ignores this.int: A Keyword token defining the data type.age: An Identifier token chosen by the programmer.=: An Operator token assigning a value.25: A Constant token representing a fixed integer.;: A Special Symbol token marking the end of the statement.
Common Mistakes
Nesting Multi-line Comments: You cannot put a
/* ... */inside another/* ... */. It will confuse the compiler and cause a syntax error.Using Keywords as Names: Trying to name a variable
int return = 10;will crash your program becausereturnis a reserved keyword.Starting Names with Numbers: Naming a variable
99bottleswill fail. Always start with a letter.
Best Practices
Don't Over-Comment: Do not write comments explaining what the code does if it is obvious (e.g.,
age = 25; // sets age to 25). Instead, use comments to explain why the code is doing something complex.Use CamelCase or Snake_Case: Keep your identifier names readable. Use
studentAge(CamelCase) orstudent_age(Snake_Case) rather thanstudentage.Be Descriptive: Name your variables logically. Use
totalPriceinstead of justtorx.
Interview Questions
Q: Can we use a special character like '@' or '$' in an identifier? Answer: No, the only special character allowed in a C identifier is the underscore (_).
Q: Are C tokens generated before or after preprocessing? Answer: Tokens are generated by the compiler during the lexical analysis phase, which happens after the preprocessor has already removed the comments and expanded the macros.
MCQs
Q1. Which of the following is an invalid identifier in C? A) user_name B) _temp C) 1stRank (Correct - starts with a number) D) totalAmount
Q2. How many standard keywords are there in the C language? A) 16 B) 32 (Correct) C) 64 D) 128
Practice Questions
Look at this line of code:
int sum = a + b;. Identify the keywords, identifiers, operators, and special symbols in this line.Write a program that prints your college name, and use a multi-line comment at the top to describe the program.
Mini Assignment
Create a cheat sheet in a notebook. Write down the 6 types of C tokens and give two examples of your own for each type. Then, write three examples of valid identifiers and three examples of invalid identifiers, explaining why they are invalid.
Summary
Comments and tokens are the fundamental text elements of C programming. Comments (// and /* */) help humans understand the code and are ignored by the machine. Tokens are the smallest individual parts of the code understood by the compiler, categorized into keywords, identifiers, constants, strings, special symbols, and operators. Following naming rules and best practices ensures your code is clean, professional, and bug-free.
FAQs
1. Does adding too many comments slow down my program? No. The preprocessor removes all comments before the code is even converted to assembly language. Your final executable file is exactly the same size, whether you have zero comments or ten thousand.
2. Can I write a comment at the end of a line of code?
Yes. This is called an inline comment. For example: int x = 5; // setting x to 5.
3. What happens if I forget to close a multi-line comment?
If you forget the closing */, the compiler will treat the rest of your entire program as a comment, and you will get an error saying there is no main() function.
4. Why is C case-sensitive?
C was designed to be precise. Treating Age and age differently gives programmers more flexibility in naming conventions, distinguishing between constants, variables, and custom types easily.
5. Are spaces considered tokens? No, spaces, tabs, and newlines are known as "whitespace." They are used to separate tokens but are not tokens themselves.
Related Posts
📚 Recommended Reading for Aspiring Developers
Before moving on to the next chapter, expand your knowledge of software engineering concepts with these helpful guides from NeoGyan:
- Version Control Mastery: Keep track of your code changes properly by reading What is Git and Why Should Every Developer Learn It?
- Language Comparisons: Wondering where C stands against modern scripting languages? Check out Python vs JavaScript: Which Should You Learn?
- Web Technologies: Interested in connecting your backend code to the web? Start with What is API? Explained Simply.
- Database Fundamentals: Every application needs to store data. Understand the basics with What is a Database? Explained Simply.
- Career Guidance: Confused about college degrees versus coding skills? Read our breakdown in The Skills vs Degree Debate: What Matters More?