16 Oct C First Program
In the previous lesson, we learned how to set up the environment for C. Now, we will see how to run C first program. It’s quite easy. Let’s begin with launching Turbo C and running C first program.
Go to Start > Turbo C
Open Turbo C and open a new project,
File > New
Firstly, save it and give any name with extension c. Here, we have given the name Study1.c,
Now, let’s add a demo program
Let’s work on our first C program,
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> int main() { printf("Studyopedia C Language Tutorial"); return 0; } |
Here,
#include
It is the preprocessor and the first word read by the compiler
stdio.h
It is the header file.
int main()
main() function with return type int.
printf
Used to print the formatted string to stdout. The standard output is stdout.
return
Terminates the function execution and the return statement returns the control to the calling function.
When you will run the above program, you won’t get an output in Turbo C, but the program runs correctly. To allow the output to be visible on the screen, remove the return statement and add the return type to the main() function as shown below, with getch() method.
Also, add <conio.h> header file and getch() for output to be visible in Turbo C++. The MS-DOS compiler uses the <conio.h> header file to show console input/output.
getch() is used to read a character from the screen and is added just before the program ends. It is a predefined function in <conio.h> header file. To clear the screen, you can also add clrscr(); just after the declaration.
We’re using the <conio.h> header file and getch() to show output on Turbo C, which is our MS-DOS compiler.
Here’s or final C program,
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> #include <conio.h> void main() { printf("Studyopedia C Language Tutorial"); getch(); } |
Here’s our C program on Turbo C compiler,
For output, press Ctrl+F9.
The following output is visible,
In this lesson, we learned how to run our first C program in Turbo C. Similarly, create a new file in Turbo C, write C code in it and run it as shown above.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments