Write First Program in C
Here we are going to write first program in C
First go and open up your dev c++ compiler and go to file, new, source file ( See below image).
So its gonna open up a blank file, Go to this File then save as to anywhere in the computer and name it first then make sure that it has the extension c source files. Save it. So as you can see here you know our first.c is ready to go ( See below image ).
Before writing the statements in our program, i just want to explain you about the functions.
Function in C program
A function is a collection of statements to do some particular task. So in every program we have to do some task again and again. for example we have to print something to the screen, or to take the input from the keyboard etc etc.
So the code for all these task are grouped into functions and they are stored in some files which are called header files.
How to use Function in C program
To use that functions in our program, we just have to include that header files. So in c we can include the header files by using a pre processor directive. A pre processor is a software which processes our source code you know before the compilation. So this pre processor provides some directives to include the files.
Meaning of #include stdio.h
We are gonna use #include then, its a pre processor directive and we are gonna write the file name. In this case we want to include a file which contains the functions for printing something to the screen and taking the input from the keyboard. So that file is called “stdio.h” . Here this stdio means that standard input output and .h is the extension for the header file.
Another file we are including is called “conio.h”. so i am gonna explain you why we are including this file later. When we encounter the statements. Next we have to write our main function. Every c program begins with a main function.
So we can say that you know a main function is the entry point of your c program. If you won’t write your main function then your compiler is gonna give an error like ok i can’t find the main function so i can’t execute your code.
So its gonna be int main, then a pair of parenthesis, then a pair of curly braces (see below image).
Here this int means that our main function is gonna return some integer value. Thats because you know every function can return some values. So then this main is the name of the function. In every c program, we should have a main function. Within this parenthesis if we want we can pass the parameters, you know thats the main function can take. So now we are not passing any parameters. Thats why we are leaving it blank.
With in this curly braces we have to write the statements of this main function. Another thing i wanna tell you is you know in c our statements will be executed one after other or in sequential order. In this tutorial we just want to print some thing to the screen. So for that purpose we can use a function provided by this stdio.h header file and that function is called printf.
With in this parenthesis in double quotes we have to specify the string we want to print to the screen. So here i am gonna write hello learner.
Why to add semi colon after every C statement ?
Add a semi colon. This is important because you know every c statement ends with a semi colon. So if you forget to add a semicolon, you know after c statement then its gonna give you an error. Now as promised here that we are returning some integer value, we have to return it. we are gonna use return keyword to return some value, then we are gonna write 0. Now we are gonna save it compile it and run it.
So i don’t think whether you guys have seen a you know a box just appeared and vanished. So that means that our program has successfully executed from here to here and once our statements ended or finished, you know our program terminated.
The execution speed is so fast that we can’t see the output. So if you want to see the output then we have to add a pause in our program . Here you know after this printf statement we want to add a pause so that you know whatever
we have printed here, we can see that. we can add a pause by using one of the method available from this conio.h header file and that function is called getch() This getch function is not specifically made for adding a pause. Actually by using this function we can receive a character from your keyboard. But here when we use this function, you know it makes the program to wait for some thing to be inputed from the keyboard.
At that time you know we can see the output. So i am gonna save it, compile it and run it. So as you can see we got this output hello learner and you guys can see a blinking cursor here. You know it just waiting for us to enter some
key or enter some character from the keyboard. so i am gonna press a key in my keyboard, you can press any key. boom thats it. It just terminated our program. This is it, this is how to Write First Program in C
The key point for Absolute Beginners in Programming :
This is about the introduction to the Computer programming and types of programming languages, what is its history and why you should study this C programming language. Now if you are an absolute beginner and you really don’t know about computer programming and types of programming languages and all these things, Then its good to read a book called ” Programming in ANSI C ” edition seven by Mac graw hills. Click here buy it from Amazon