How to use Turbo C/C++ to run your first C program - LEKULE

Breaking

17 Dec 2015

How to use Turbo C/C++ to run your first C program

There is no one in this world who is born as a programmer .To write efficient programs using any languages you have to learn it by doing! You have to write as many different programs using as many different algorithms. You have to break codes, make errors, debug errors and sometimes you have to approach the same problem using 2 or 3 different logics. To learn “C language” efficiently, you must write a lot of different C programs using the concepts of C language. To do this you need a good compiler for C language setup on your computer. Turbo C is one such compiler for windows operating system. If you are running a Linux operating system, you can use the GCC compiler. A compiler does the job of converting codes written in C language to machine language, so that it can be executed.
First of all you need to install and setup Turbo C compiler on your computer. If you are using Windows XP – you can download  Turbo C and install it directly. On the other hand if you are using Windows Vista or Windows 7 – you may read the following article to setup Turbo C on your computer.

How to install Turbo C/C++ on Windows 7/Vista

Okay. I assume that you have installed Turbo C on your computer perfectly. Now lets see how to use Turbo C to RUN your C programs.
Open Turbo C from your Desktop or Programs menu. Select “File” from Menu bar and select option “New”
If there are any default lines of code present inside editor please remove all of them. The text editor should be blank. Now you may type in the following program in your Turbo C editor. This is a program to print “Hello World” on the first line of your output screen and to print “Thank You” on the next line of your output screen. 

Note:- You don’t need to type any lines that comes after // in your Turbo C editor. Any text that is placed after // are comments and are written here with C code for your ease of understanding.

#include//Preprocessing standard input output header file

##include

void main() // Main function which is the entry point to any C program

{

printf("Hello World\n"); // Function to print any data on output screen

printf("Thank You");

getch(); // Function get an input data from keyboard

}


If you are really new to C programming you may not understand this simple lines of code perfectly. Don’t worry about that, we will learn those in coming chapters.   First of all keep in mind that C is a procedural programming language – which means these codes are executed line by line; beginning from the first line #include. The first two lines that begins with #include are 2 preprocessors (which we will explain later). The line void main() is where the real codes of program begin. You may note 2 parentheses { } that follows void main(). The whole program must be kept within these 2 parentheses. So your C programs skeleton would be like
Preprocessors

void main()

{

Lines of code/Program

}
printf() is a standard command/library function in C language which performs the task of printing some data on the output screen. What ever data we pass through the enclosed parentheses () of printf function will get printed on the output screen and the data can be integers,characters or strings. So the line printf(“Hello World\n”); will print Hello World on the output screen. You may note the \n that follows Hello World. That \n is the command for new line. The printf function will move the cursor of output screen to next line upon reading the \n sequence inside the parentheses. Now in next line printf(“Thank You”); will print the words Thank You
Note:- Almost all the lines of code inside the void main () { lines of code } must end with a semicolon. There are exceptions in some cases like usage of loops (for, do while) etc which we will learn later.

The last line of code getch() is actually a standard input library function in C language. getch() -known as get character- is a function get an input from the keyboard. Upon executing the getch() function the program control will wait until a character is inputted by the user from keyboard. Here in this program this getch() function is used as a trick to hold the output screen live for a desired period of time by the user. The output screen will get closed only if the user makes a key press.

Note:- You may try executing this program without getch() function. Observe yourself what happens!

I hope that’s enough for a basic explanation of the program. If you still have doubts please ask through comments. Now lets RUN this program using Turbo C. Before going into the steps, you may SAVE your C program. Select “File” from menu -> click-> Save. Name the files as ->hello.c or some other name with a .c extension. See the screen shot below.
Turbo C tutorial

How to Compile a C program in Turbo C ?

The first step is compiling. Compiling makes sure your program is free of syntax errors. How ever compiler won’t check for any logical/algorithmic errors. There is a lot of process that happens while the compiler compiles a program – which we will discuss later in coming articles. To do compiling – Select -> Compile from menu and click-> compile. See the image below.

Compiling a C program in Turbo C

After compiling, you will see a dialog box as shown below.If the compilation is success – you will see a “success” message. Else you will see the number of errors. Both are shown using screen shots.
The screen shot of a “success” compilation

Compiling a C program in Turbo C
The screen shot of an “Error” compilation.
How to compile a C program
Here the 2 errors are because I removed semicolons from the 2 lines
printf(“Hello World\n”)

printf(“Thank You”)
You may run the program only after a “Successful” compilation. Make your program free of errors before you RUN the program.

How to RUN a C Program in Turbo C compiler?

To RUN the program – you may select ->Run from menu and click -> Run (as shown in the image below). 
How to RUN a C program in Turbo C
Now you will see the output screen as shown in the screen shot below.
Output of a C program

To close the output screen press any key (please remember/refer the part I mentioned about getch() function in this article).

No comments: