In this lesson, you will learn
Let’s go through the first Hello World program in C, followed by a line-by-line explanation of each part of the program.
#include<stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Hello, World
#include <stdio.h>#include is a preprocessor directive.
<stdio.h> stands for Standard Input Output Header file.
This line tells the compiler to include the standard I/O library, which contains the definition of the printf() function used later.
int main()main() is the starting point of every C program.
int means the function will return an integer to the operating system.
Every program must have one main() function, where execution begins.
{This opening curly brace { indicates the beginning of the function body.
All the statements inside the function are enclosed in { }.
printf("Hello, World!\n");printf is a standard output function that displays text on the screen.
"Hello, World!\n" is a string literal.
The text inside quotes is printed as is.
\n is an escape sequence that moves the cursor to a new line after printing.
The ; is used to end the statement (very important in C).
return 0;This statement returns 0 from the main() function.
In C, returning 0 typically means that the program executed successfully.
It is passed back to the operating system.
}This closing brace } indicates the end of the function body.
| Code | Meaning |
|---|---|
#include <stdio.h> | Tells the compiler to include standard I/O library |
int main() | Entry point of the program; returns an integer |
{ } | Denotes the block of code in the function |
printf("Hello, World!\n"); | Prints “Hello, World!” followed by a newline |
return 0; | Sends a success code to the OS after program ends |
#include<stdio.h>
int main() {
int a = 5, b = 7, sum;
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Sum = 12
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.