Wednesday, 7 January 2015

Write Down Your First C program!

Write Down your First C program

After talking about compilers it is time to make our first program. We like to make C programs under Windows platform so we will use code::blocks IDE and the gcc compiler.
Let's  start by saluting our world! Type the following program into your  code blocks IDE. Please do not copy past below sample code . Please write down your own hand and enjoy your learning…

#include <stdio.h>

int main()
{
   /* my first program in C */
   printf("Hello, World! \n");
  
   return 0;
}
 Brief explanation about the sample code:

What Does Mean #include<stdio.h>  in c?

Take a look  this line of code that we include a file called stdio.h. It is actually Standard Input/Output header file.In 'C' programming language all library functions are included in different header files in different categories with ".h" extension.

# is called as "preprocessor". Means it will include the header file before comipling the code. "include" gives command to include something

"stdio" stands for "standard input output". As it is a header file so .h extension is there. It contains some standart functions related to input and ouput such as "printf", "scanf"

Now you may have question What is function? There are two type of function in c
  • Built in function
  • User define function 

I will make a brief explanation about function later .
A program begins by calling the function main.Actually a function is declared in the following manner:

return-type function-name(parameter-list,...)
 {
           body... 
}
return-type is the variable type that the function returns.
The int means it will return a success or failure code to the operating system, where return 0 is success.



What does mean { } in C?

The two curly brackets first one  the beginning and other one is the end are used to group all commands statement together. In this case all the command statements between the two curly brackets belong to main(). The curly brackets are often used in the C programming language to group command statements  together.


What does printf in C?

A printf statement is a command that simply prints the text you type in nothing else.
heres and example of a printf.

printf("This is what you will see when you run the program");
 Output will be   This is what you will see when you run the program
Its pretty simple.
printf also creates a formatted output to the screen. there a variations printf 
for example
int exampleInt = 3;  
printf(("Hello exampleInt = %d\n",exampleInt );

Outputs
"Hello exampleInt = 3"
the \n is a new line feed.


What's the point of the int - return (0), Is there any real purpose to this?

When we wrote the main function the first line “int main()”.we declared that main must return an integer int value.
Return 0 means it will return a success or failure code to the operating system, where return 0 is success.

Note, even if the program fails in certain ways, the programmer may choose to still say return 0 because it isn't expected that the operating system will do anything with the return value


Compilation:

If you have typed everything correct format there should be no problem to compile your program. After compilation you now should have a hello.exe file Windows operating system or hello binary file UNIX or Linux. You can now run this program by typing hello.exe (Windows) or ./hello (UNIX/Linux).

Congratulations!!!!!
You have just made your first program in C language.


No comments:

Post a Comment