Hello World C Program
This is a program to print “Hello World” message on screen.
#include<stdio.h> void main() { printf("Hello World"); }
Hello World
Explanation
Now try to understand this program step by step.
1. #include<stdio.h>: First statement started with #, it is called pre-processor directive. We will learn about them thoroughly in later tutorials. #include<stdio.h> is used to include the stdio.h header file in our program. Header files contains the functions that we use in our program. Here we have used printf() function which is present in stdio.h header file.
2. void main(): Here main() is the function. A program always starts with the main() function and every program must have main(). Here void is the return type of this function. It means main() function will not return anything. The opening curly braces ({) and closing curly braces (}) shows the body of the function.
main() can also be called as a collection of statements.
3. printf(): Here printf() is another function. This is used to print the values on the screen. Its general form is
printf(“Statement you want to print on screen”);
4. Semicolon (;) is used for denoting the termination of statement. It is also called statement terminator in C Language.
How to compile and execute?
Here we are using Codeblocks IDE that comes with GCC compiler. If you have not downloaded it yet then follow below link to know how to download and install it.
Also Read: How to Install CodeBlocks IDE on Windows
Watch below video know how to compile and run this hello world C program in Codeblocks.
Well this is the easiest C program. Now lets move on and try slightly complicated program of multiplication of two numbers.
C Program to Multiply two Numbers
#include<stdio.h> void main() { int a, b, c; a=3; b=4; c=a*b; printf("Answer is %d",c); }
Answer is 12
Explanation
Now lets try to understand this program.
1. First two statements are same as above program. In the third statement I have written
int a, b, c;
Here int is the keyword for integer and a, b and c are the integer variables. So they can only store integer values.
2. In the next two statements I have written
a=3;
b=4;
In these statements we are storing the values 3 and 4 in a and b variables respectively.
3. In the next statement
c=a*b;
We are multiplying the values in a and b, and storing them in the variable c.
4. Now in the last statement we are printing the value which is in c. A new thing %d which we have used in this program. It is called format specifier. It usually tell the compiler that it has to print the integer value on screen which is present in a variable.
Some common format specifiers are given below
a. %d for integers
b. %c for characters
c. %f for floating point numbers or real numbers
A bit elaborated form of printf() is given below
printf(“string you want to print “, variable);
Suppose we have to print the value in b so we will use the function.
printf(“%d”,b);
Things to keep in mind
1. Use semicolon at the end of each statement.
2. Type the statements in main program as the way you want them to be executed.
3. Never try to memorise any program. Just try to understand it.
4. Learning programming is all about practical. So start doing practical from now.
Comment below if you are facing any difficulty to understand your first C program to print hello world message.
as i know ………..
int main means – this function will return int value
void main means – this function will return nothing
sir a big question in my mind – how is ……..void main ( )……. and……….. int main ( )……………… are different ??
i have used both but they returns same ” hello world ” output ….. no difference i had observed …….
a very good response that I never expected from anywhere.thanks a lotttttttt……
sir….in the program you have used “int main()”
why did you choose to use int main….you can even use “void main()” know sir….what diff. does it make??
Yes we can use void main() also. It doesn’t make any difference in this case.
Sir what do we mean by returning something…
Nd I didn’t get this return 0
thanks