Your First C Program
86You might be thinking that you have learnt lot about C language background and now eagerly want to try your first program in C. To do so first of all you need a C compiler, I have been using Turbo C++ compiler for this purpose. Why Turbo C++ compiler why not Turbo C compiler? Because Turbo C++ compiler supports both C & C++ program and if you want to learn C++, you don’t need to change your compiler. One more thing, Turbo C++ compiler provides mouse support which Turbo C compiler doesn’t.
I assume that you have installed C compiler in your PC and now want to try your first program. Here is an example of your first C program which has been developed using Turbo C++ compiler.
Example - 1
#include <stdio.h>
void main()
{
printf("Welcome to C");
}
Just type the above line in the C editor and press Alt + F9 to compile that program. If there will be any mistakes, it will show “Error” message otherwise it will show “Success” message. Now press Ctrl + F9 to run compiled program and you will see similar output. If you cannot see anything then press Alt + F5 to see output window.
Program Output
Let’s understand each and every line of this program. The very first line #include <stdio.h> tells the compiler to include a header file “stdio.h” in the program. Header file in C is nothing but collection of various functions. Here we are using printf() from stdio.h header file. The next line is void main() which is the beginning of the program. Every program must have a main() because program starts execution from this function. But what does the keyword void means before the main(). This means that main function will not return any value to the compiler. I know this is tough to understand now; these things will be discussed in detailed in function page.
The curly open and close braces { } defines the scope of the “main()” function means “main()” function’s boundaries. The line between curly braces printf("Hello, World!"); is the one of function defined in stdio.h whose job is to print message specified in double quotation. At the end of the line there is a semicolon (;) which is the end of that statement. C statements always end with a semicolon (;).
One thing you should keep in your mind that in C program, instructions declared in “main()”, are executed from top to bottom. To understand it better let’s extend the above program.
|
C Programming Language (2nd Edition)
Price: $37.90
List Price: $60.33 |
|
Programming in Objective-C 2.0 (2nd Edition)
Price: $25.00
List Price: $44.99 |
|
Programming in C (3rd Edition)
Price: $29.13
List Price: $49.99 |
|
C Programming for the Absolute Beginner
Price: $18.72
List Price: $29.99 |
C Language Books
|
Programming in the Key of C#: A Primer for Aspiring Programmers (Step By Step (Microsoft))
Price: $15.85
List Price: $29.99 |
|
|
C/C++ Programmer's Reference
Price: $6.00
List Price: $21.95 |
|
Jamsa's C/C++ Programmer's Bible
Price: $17.99
List Price: $65.95 |
|
TCP/IP Sockets in C#: Practical Guide for Programmers (The Practical Guides)
Price: $14.50
List Price: $26.95 |
Example - 2
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Welcome to C. ");
printf("C is a Programming Language. ");
printf("Hello! How are you?");
getch();
}
The top two lines include two header files “stdio.h” and “conio.h” respectively. The “stdio.h” header file contains “printf()” and “conio.h” file contains “clrscr()” and “getch()”. The “clrscr()” clears the output screen means any output shown by previously run program will be erased and that why we always put them at the beginning. Next is three “printf()” statements which will print three different messages. The “getch()” waits for a keyboard input and if we press any key on keyboard the program exits. So we don’t need to press Alt + F5 to see the output as we did in above program.
The “getch()” will be discussed later in detail.
Other C Programming Tutorials
- A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called... - What is Function in C Language?
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also ... - Types of Function in C Programming Languages:
In my previous c programming tutorial I tried to explain what the function, its advantages is and how to declare a C function. And I told you that there are five types of functions and they are: ... - Array in C programming Programmer's view
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int,... - How to work with Two Dimensional Arrays in C
We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two... - Turn Your PC Keyboard to Musical Keyboard using C Program
To accomplish this task you dont need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you... - File Copy Program in C Language
Today we are going to learn a simple file copy program in C language. As I said this is a simple file copy program so you should not expect its output like DOS copy command has. Ok lets start. The main... - How to work with Multidimensional Array in C Programming
C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array...
Useful Links
- How to work from home on internet
How to make money online? This is the common and highest searched term on internet. You will also get numerous answers to this query. But, before following any answer, please make sure which type of money... - What is LAN (Local Area Network) ?
LAN is acronyms of Local Area Network. LAN is required everywhere, whether it is office home or somewhere else. LAN is a small computer network (small version of internet) covering a small area like home,... - Hack Google for Better Search Result
Google, on internet, by far is the most popular search engine which is used by about 80 percent of total internet users. Google’s this popularity is because of excellent search results with extensive range... - Extend Your Computer’s Life
A computer is a complex electronic device which we own proudly. But to remain its proud owner we need to take care of it. To extend life of computer, it must be regularly examined as we do for various...
PrintShare it! — Rate it: up down flag this hub










brad4l says:
8 months ago
Cool Hub :)I really think C or C++ are some of the best first languages to learn. I started working through a C book and then switched to C++. By the time I started learning Java, it was really easy to apply what I had already learned to Java and quickly pick it up.