create your own

Your First C Program

86
rate or flag this page

By rajkishor09


You 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

Turbo C Editor Window.
Turbo C Editor Window.
Output after executing program.
Output after executing program.

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) C Programming Language (2nd Edition)
Price: $37.90
List Price: $60.33
Programming in Objective-C 2.0 (2nd Edition) Programming in Objective-C 2.0 (2nd Edition)
Price: $25.00
List Price: $44.99
Programming in C (3rd Edition) Programming in C (3rd Edition)
Price: $29.13
List Price: $49.99
C Programming for the Absolute Beginner C Programming for the Absolute Beginner
Price: $18.72
List Price: $29.99

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.

Comments

RSS for comments on this Hub

brad4l profile image

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.

puarl  says:
2 weeks ago

how we understand program

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working