Hey there!!
Welcome to ClearUrDoubt.com.
In this post, we will look at the basic C program to print “Hello World” message to the console. If you are working on a Windows machine, you can use Dev-C++ to write and execute C programs.
Here is the C program:
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main() { printf("Hello World.\nExcited to learn C."); return 0; } |
Let’s go through each line and explore the details.
1 |
#include <stdio.h> |
This statement will include a file called “stdio.h” in your code. “stdio” means Standard Input/Output. So all the predefined/inbuilt functions related to Standard IO are defined in “stdio.h” header file.
1 |
int main() |
main is the function where the execution starts. the above statement tells the compiler that this function is going to return an integer to the operating system once the program is completed.
1 |
{} |
these curly brackets are used to group all the statements in this function.
1 |
printf("Hello World.\nExcited to learn C."); |
“printf()” is an inbuilt function defined in “stdio.h” which will output the message passed as a parameter to it. In the above statement, “\n” tells the compiler to add a new line character(called “escape sequence” in C) in between the statements so that it will print the two sentences in two lines.
1 |
return 0; |
Finally, this statement returns the value “0” to the operating system and the execution stops.
Below is the output generated once the program is executed:
Also, C language supports “comments” in the programs so that they can be made readable. There are two types of comments 1) Single Line comments and 2) Multi-Line comments.
Single Line comments are given using “//” and Multi-line comments are included in between “/*” & “*/”.
1 2 3 4 |
// This is a Single Line comment. /* This is a Multi-Line comment. */ |
Congratulations on learning the first program in C :). Please leave a reply in case of any queries.