C000 Beginner Write your first C program – Hello World [explained]

By | July 12, 2013

If you have just started learning C programming language, here is a example C program explained so that you can understand the very basic terminology before you write more complex C Applications. First C Program simply prints a text message “Hello World” on Screen.

[codesyntax lang="c"]

#include<stdio.h>
void main()
{
printf(“Hello World”);
getch();
}

[/codesyntax]

First Line – #include <stdio.h>

#include is a directive used for including files, which are Header Files. stdio stands for STanDardInputOutput. Standard Input is Keyboard and Standard Output is Monitor (Screen). The functions which are used in Standard Input and Standard Output are arranged and Saved in a Header File called stdio.h

All files with Extension dot h (.h) are called Header Files. There are many Header files in C Language.

Next Line – void main()

void means empty. It says ‘i am not going to return anything’. Functions in C Language are supposed to return something in back.

main() is a Function. In C Programming there has to be only one Main Function Written in a Program. The Compiler complies Main Function first.

Next Line – {

{ – is to set the beginning Scope of the Main Function for this (Program) i.e. Start of a Main Function for this (Program)

Next Line – printf(“Hello World”);

printf() stands for Print Function. The Text to be printed on the screen is to be written inside “here”. The empty brackets i.e. () are considered to be void. If we write something inside it they are called arguments here “Hello World” is an argument passed to printf Function.

; – is termed to be a terminator which terminates the line of code written (end of line).

Next Line – getch();

getch() – stands for Get Character Function. getch() is Function which is used for getting a Character from the User. The computer is very fast the output shown in a flash of a second. So to show you the result and hold the output screen unless you Enter any key (character) as mentioned Get Character.

Last Line – }

} – is to set the ending Scope of the Main Function for this (Program) i.e. End of a Main Function for this (Program)

Execution of program explanation – Hello World

First save the program with HelloWorld.c filename. No Space is allowed in the name of the Program File and extension as .c (dot c because it’s a C program). The written Program has to be complied. The Program with No Errors will only run and could show you the desired output. Just see the screenshots below.

c-program-hello-world c-program-hello-world-output

video:

Now we will write another C program which does some Arithmetic Operations.

Second Program is of Arithmetic Operation Program which can be used for either for Addition, Subtraction, Multiplication and Division. This can be done by only Changing the Operator. Arithmetic Operators are + – * /. (Note: – * stands for Multiplication)

[codesyntax lang="c"]

#include<stdio.h>
void main()
{
int a,b,c;
a=5; b=10;
c=a+b; or c=a-b; or c=a*b; or c=a/b;
printf(“Value of C is %d”,c);
getch();
}

[/codesyntax]

Explanation : 

In this program we are declaring variables – int a,b,c;

int – stands for integer data type. int data type is used to store integer values(numbers). a b c are called variables to store values depending upon variable types selected. comma operator are used to separate variables from one another.

Next Line – a=5;b=10;

a=5; b=10; – a is assigned with a numeric value 5. b is assigned with a numeric value 10.These values 5 & 10 are stored in the variables a & b respectively. In this Line there are two semicolons (;) that means two terminators. we can do so but compiler understands it to be two different Lines.

Next Line – printf(“Value of C is %d”,c);

printf() stands for Print Function. The Text to be printed on the screen is to be written inside “here”. The empty brackets i.e. () are considered to be void. If we write something inside it they are called arguments here “Value of C is” is an argument passed to printf Function. %d is a format specifier and used to replace variable value in the text to be printed on screen for printf() Function. d stands for decimal. Since integer values are all decimal values hence %d is used to print integer variable.

Next Line – getch();

getch() – stands for Get Character Function. getch() is Function which is used for getting a Character from the User. The computer is very fast the output shown in a flash of a second. So to show you the result and hold the output screen unless you Enter any key (character) as mentioned Get Character.

c-program-output-value c-program-output-value1

video:

Now let’s modify above C Program for taking User inputs for variables.

[codesyntax lang="c"]

#include<stdio.h>
void main()
{
int a,b,c;
printf(“\n Enter the values for a and b: \n”);
scanf(“%d %d”,&a,&b);
c=a+b; or c=a-b; or c=a*b; or c=a/b;
printf(“Value of C is %d”,c);
getch();
}

[/codesyntax]

New Line – printf(“\n Enter the values for a and b: \n”);

printf() function is written for giving the message to user on screen (i.e. Enter the values for a and b: ). \n is Next Line character. wherever you find “\n” in printf function a new line begins from that point.

New Line – scanf(“%d %d”,&a,&b);

scanf() stands for Scan Function. The Input(s) to be scanned from the screen is to be written inside “here” with %d which is a format specifier and used to scan variable value entered on screen for scanf() Function. d stands for decimal. Since integer values are all decimal values hence %d is used to print integer variable.

c-program-taking-user-input c-program-taking-user-input-1

Here you can enter two variable values on same line or on different line. If on same line you leave space in between the values or for different line you need to enter the enter key and type the second value on the screen.

Note:- To understand program for sequence in detail Please SEARCH numerically example: C000, C001, C002, etc.

Leave a Reply