Sunday, December 29, 2013

Hello World

Traditionally all programming text books begin with same a common program that prints Hello World on the screen

So now lets walk through a simple procedure of writing this simple program that prints hello world on the screen in C.

Open the text editor and write the following program and save it as a file with .c extension say helloWorld.c  in CProgs folder on desktop
---------------------
 /* Program that prints Hello World on the screen */  
#include<stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
-----------------------

Compiling and running the Program
1. Open the command prompt (Start->Run->type cmd and press enter or  Start->All Programs-> accessories->Command Prompt)

2. Navigate to the directory where you stored the C program. say 
C:\Users\September31\Desktop\CProgs>

3. Write the following command to compile the  program using following command
C:\Users\September31\Desktop\CProgs> gcc helloWorld.c -o helloWorld

This will produce a helloWorld.exe file in CProgs folder

4. Now you can run the file at Command Prompt 
C:\Users\September31\Desktop\CProgs>helloworld

5. Output will be displayed.
Hello world

Going through the Programs
/* Represents multi-line comments which do not effect result and are only there to make the program more readable and understandable */
#include <stdio.h> - includes the library header file stdio.h that is for standard input and output. It is used here because we need a library function "printf".

int main() - main() function is the where the processing of a C program starts from.
usually syntax of a function in c is :
return-type function-name(type parameter1,type parameter2){ }

printf("Hello world\n");
printf(" Type text to be printed in double quotes \escape sequence");
is the syntax for printf which is a built-in library function of defined in stdio.h header file. The text to be printed is written in double quotes. Whenever backslash '\' is encountered within double quotes it means a Escape Sequence. \n does not print '\n' on screen,rather it signifies newline which moves the cursor to the beginning of next line. There are various other escape sequence or backslash characters that can be used such as \t represents tab spaces and \b represent backspace.
the printf statement is followed by a semicolon (;) which is a terminator.

return 0;
A function should always return some value if it is defined with a return type. a function with no return type is void and should be declared in following manner.
void function-name(type parameter)
A function may also not have a parameter list. It can be declared as following
void function-name(void)
or simply
void function-name()

A function in C begins with opening curly brace and end with a closing curly brace.They represent the scope of a function and tell us where a program has begun and where it has end.
They are used with decision control statements and looping statements and also with arrays , structures and have different uses and significance.

Special care should be taken in putting terminators after appropriate statements and closing all opened curly braces as these are sources of most commonly occurring errors in the program.




No comments:

Post a Comment