C Statements
- What is a Statement?
- A computer program is a list of instructions for the computer to execute.
- In a programming language, these instructions are called statements.
- Each statement tells the computer to perform a specific action (like print, calculate, or stop).
- Example Statement
printf(“Hello, World!”);
Explanation:
- This statement instructs the compiler to print “Hello, World!” on the screen.
- Every statement in C must end with a semicolon ( ; ) — this marks the end of the instruction.
- Importance of Semicolon
- The semicolon ( ; ) is very important in C.
- If you forget to write ;, your program will show an error and won’t run.
Example:
printf(“Hello World!”) // ❌ Missing semicolon
Error:
error: expected ‘;’ before ‘return’
- Many Statements in a Program
- Most C programs contain many statements.
- The statements are executed one by one, in the same order as they are written.
Example:
printf(“Hello World!”);
printf(“Have a good day!”);
return 0;
- Example Explained
|
Statement |
Action |
|
printf(“Hello World!”); |
Prints “Hello World!” |
|
printf(“Have a good day!”); |
Prints “Have a good day!” |
|
return 0; |
Ends the program successfully |
➡️ The computer runs them top to bottom, one after another.
🗣️ Simple Summary
💬 A statement is a single instruction.
Every statement must end with a semicolon ( ; ).
The computer executes statements in order, one by one.
C Output (Print Text)
- What is Output?
- In C programming, output means displaying text or values on the screen.
- To show output, we use the printf() function.
- Syntax of printf()
printf(“text”);
- The text or message you want to display must be written inside double quotes (” “).
- Each printf statement ends with a semicolon ( ; ).
- Example Program
#include <stdio.h>
int main() {
printf(“Hello World!”);
return 0;
}
Output:
Hello World!
🔹 4. Important Rule — Use Double Quotes
- Text (also called string) must be inside double quotes (” “).
- If you forget them, you will get an error.
✅ Correct:
printf(“This sentence will work!”);
❌ Wrong:
printf(This sentence will produce an error.);
Error Message:
error: expected expression before ‘This’
- Using Many printf() Functions
You can use multiple printf() statements in one program.
Example:
#include <stdio.h>
int main() {
printf(“Hello World!”);
printf(“I am learning C.”);
printf(“And it is awesome!”);
return 0;
}
Output:
Hello World!I am learning C.And it is awesome!
- Note
- By default, printf() does not add a new line between outputs.
- If you want to print on a new line, use \n (newline character).
Example with newline:
printf(“Hello World!\n”);
printf(“I am learning C.\n”);
Output:
Hello World!
I am learning C.
C Comments
- What are Comments?
- Comments are used to explain code and make it more readable.
- They help programmers understand what the code does.
- The compiler ignores comments, meaning they do not get executed.
- Comments are also useful when testing or disabling code temporarily.
- Types of Comments in C
There are two types of comments in C:
- Single-line comments
- Multi-line comments
- Single-line Comments
- A single-line comment starts with //.
- Anything written after // on that line will be ignored by the compiler.
Example 1:
// This is a comment
printf(“Hello World!”);
Example 2:
printf(“Hello World!”); // This is a comment
💬 Single-line comments are mostly used for short notes or quick explanations.
- Multi-line Comments
- A multi-line comment starts with /* and ends with */.
- Everything written between them will be ignored by the compiler.
Example:
/* The code below will print the words
Hello World! to the screen */
printf(“Hello World!”);
💬 Multi-line comments are useful for long explanations or when you want to comment out multiple lines of code.
- Choosing Between the Two
|
Type |
Symbol |
When to Use |
|
Single-line |
// |
For short explanations or end-of-line comments |
|
Multi-line |
/* … */ |
For longer notes or commenting multiple lines |
- Historical Note 🕓
Before C99 version (1999), C supported only multi-line comments (/* … */).
From C99 onward, single-line comments (//) were added.
🗣️ Simple Summary
Comments make code easier to read and understand.
The compiler ignores all comments.
Use // for short comments and /* … */ for long comments.