C Programs

1 C Programs

C program to print a message on the screen


void main()
 clrscr(); //to clear the screen
 printf(“nnnnttt***** Welcome to C Programming *****”);
 getch(); //to stop the screen

Basics of a C program, such as

  • The #include directive
  • Header files
  • Comments
  • The main() function
  • The void data type


The #include Directive


#include <stdio.h>

You see that this line starts with a pound sign, #, which is followed by include. In C, #include forms a preprocessor directive that tells the C preprocessor to look for a file and place the contents of the file in the location where the #include directive indicates.

The preprocessor is a program that does some preparations for the C compiler before your code is compiled. More details about the C preprocessor are discussed in Hour 23, "The C Preprocessor."

Also in this line, you see that <stdio.h> follows #include. You may guess that the file the #include directive asks for is something called stdio.h. You are exactly right! Here, the #include directive does ask the C preprocessor to look for and place stdio.h where the directive is in the C program.

The name of the stdio.h file stands for standard input-output header file. The stdio.h file contains numerous prototypes and macros to perform input or output (I/O) for C programs. You'll see more program I/O in Hour 5, "Reading from and Writing to Standard I/O."


Header library of Console Input and Output from BIOS Interrupt. NOT MS-DOS!
Eg: clrscr(); gotoxy(); textattr; kbhit; etc

Header Files

The files that are required by the #include directive, like stdio.h, are called header files because the #include directives are almost always placed at the head of C programs. Actually, the extension name of .h does mean "header."

Besides stdio.h, there are more header files, such as stdlib.h, string.h, math.h, and so on. Appendix A, "ANSI Standard Header Files," gives a list of all the ANSI standard header files.


This  line contains a comment:

/* 02L01.C: This is my first C program */

You notice that this line starts with a combination of a slash and an asterisk, /*, and ends with */. In C, /* is called the opening comment mark, and */ is the closing comment mark. The C compiler ignores everything between the opening comment mark and closing comment mark.

The only purpose of including comments in your C program is to help you document what the program or some specific sections in the programs do. Remember, comments are written for programmers like you. For example, when you read someone's code, the comments in the code help you to understand what the code does, or at least what the code intends to do.

The main() Function


main ()

This is a very special function in C. Every C program must have a main() function, and every C program can only have one main() function. You can put the main() function wherever you want in your C program. However, the execution of your program always starts with the main() function.

The main() function is the only function defined in the program. Within the main() function body, a C library function, printf(), is called in order to print out a greeting message.

One more important thing about main() is that the execution of every C program ends with main(). A program ends when all the statements within the main() function have been executed.

The void Data Type

 Void is a keyword for a data type in C. When a void is placed prior to a function name, it indicates that the function does not return a value.

As you have learned, the exit() function does not return any values, but, by default, the main() function does.

C printf() function:

    • printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
    • We use printf() function with %d format specifier to display the value of an integer variable.
    • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
    • To generate a newline,we use “\n” in C printf() statement.


getch is used to hold the screen in simple language, if u don't write this the screen

will just flash and go away...


This is used for clearing the output screen i.e console.

Suppose you run a program, alter it and run it again you may find that the previous output is still stuck there itself, at this time clrscr(); would clean the previous screen.

One more thing to remember always use clrscr(); after the declaration like

int a,b,c;

float total;




Input with scanf

  • To read an int, supply scanf with a format string containing the conversion specification %d, and include an int variable preceded by an ampersand (&) as the second parameter.
  • To read a double, supply scanf with a format string containing the conversion specification %lf (that's a lower case L, not a one), and include a double variable preceded by an ampersand as the second parameter.
  • To read more than one number, include more than one conversion specification and more than one extra parameter.

Importance of the Ampersand

It is important to put the ampersand in front of the variables that appear as parameters to scanf, and it is easy to forget to do this. Remove the ampersand and compile and run the program. The program will crash before it runs to completion. Put the ampersand back. If you see this behavior in the future, check your scanf statements.

How scanf Consumes Input

When scanf is called, it skips over all leading white space (spaces, tabs, and newlines). Try recompiling and running scanfdemo. Each time it prompts for a number, try entering a bunch of newlines, spaces, and tabs before typing the number. The extra white space will have no effect.

After scanf has skipped over white space, it reads until it finds something that doesn't belong in the type of number than it is looking for and then quits. It leaves any remaining input behind for the next call to scanf. Run scanfdemo, and when it prompts for the first number enter

1.2 3.4

You will see than scanf reads the 1 as an integer, stopping when it encounters the decimal point (which can't be part of an integer). It later reads the .2 as a double, stopping when it encounters the white space. Then it reads the 3 as an integer, and the .4 as a double. Notice that if there is input left over from a previous call, scanf will use it.

Now run the program and enter


None of the calls to scanf can get past the x, so the uninitialized values of the variables are displayed.

Be careful when you supply input to your programs that you only type in properly formatted numbers and white space.





void main()


int x,y;

int sum;


printf(“Enter the value of x and y”);



printf(“The value of sum is %d”,sum);




Enter the value of x and y

23    12

The value of sum is 35