In this lesson, we will write our first C program and understand each part of the program step by step. We will use the printf() function to display a message on the screen.
.c extension,
for example hello.c.
Here is a simple C program:
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
This program displays Hello World on the screen.
First, create a new file and save it with the .c extension.
hello.c
The .c extension tells us that the file contains C source code.
The first line of our program is:
#include <stdio.h>
This includes the standard input/output header file so that functions such as
printf() can be used.
stdio.h stands for Standard Input Output Header.
It provides declarations for commonly used input and output functions.
For example, printf() is declared through the standard I/O
facilities provided by this header.
Every hosted C program has a function called main() where program
execution begins.
int main()
{
}
The int before main() means that the function returns
an integer value to the environment when the program finishes.
int main()
The curly braces { } define the body of the main()
function.
int main()
{
// Program statements
}
Statements written between these braces belong to the function.
The printf() function is used to display formatted output.
printf("Hello World");
The text inside quotation marks is displayed on the screen.
We can print different messages using printf().
printf("Welcome to C Programming");
The output will be:
Welcome to C Programming
Most C statements end with a semicolon ;.
printf("Hello");
return 0;
Forgetting the semicolon can cause a compilation error.
The statement:
return 0;
returns the integer value 0 from main().
A zero return value is conventionally used to indicate successful completion.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
This is a basic complete C program.
When the program is compiled and executed, the output will be:
Hello World
We can use printf() to print our name.
#include <stdio.h>
int main()
{
printf("My name is Rahul");
return 0;
}
Replace the name with your own name when practicing.
We can use multiple printf() statements in one program.
#include <stdio.h>
int main()
{
printf("Hello\n");
printf("Welcome to C");
return 0;
}
The escape sequence \n moves the output to a new line.
printf("Hello\nWorld");
Output:
Hello
World
Numbers can also be printed using printf().
printf("%d", 100);
Output:
100
The %c format specifier can be used to print a character.
printf("%c", 'A');
Output:
A
The %f format specifier can be used to print a floating-point
value.
printf("%f", 10.5);
The output is displayed as a floating-point value according to the default formatting.
Comments help explain the code and are ignored by the compiler.
#include <stdio.h>
int main()
{
// Display a message
printf("Hello World");
return 0;
}
A single-line comment starts with //.
// This is a comment
The compiler does not treat the comment as an executable statement.
A multi-line comment starts with /* and ends with */.
/*
This is a
multi-line comment
*/
If the file is named hello.c, GCC can compile it using:
gcc hello.c -o hello
If there are no compilation errors, an executable program will be created.
After successful compilation, run the generated program.
Windows:
hello
Linux/macOS:
./hello
#include <stdio.h>
int main()
{
printf("My name is Rahul.\n");
printf("I am learning C.");
return 0;
}
The \n places the second message on a new line.
Beginners commonly make these mistakes:
#include <stdio.h> when using printf().main or printf.
Text passed to printf() should normally be written inside double
quotation marks.
printf("Hello World");
Do not replace the programming quotation marks with typographic “smart quotes”.
The basic execution flow is:
main().printf() statement executes.return 0; ends the main() function.Write a program that displays your name, city, and course.
#include <stdio.h>
int main()
{
printf("Name: Rahul\n");
printf("City: Patna\n");
printf("Course: C Programming\n");
return 0;
}
Change the values and practice the program yourself.
A basic C program generally contains a header inclusion, the
main() function, statements, and a return statement.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
You are now ready to learn the basic syntax rules of the C programming language.
main() is the starting function of a hosted C program.#include <stdio.h> provides standard input/output declarations.printf() is used to display formatted output.\n is used to move output to a new line.return 0; returns zero from main()..c extension.Question: Which function is the starting point of a basic C program?