C-Constants:Previous                                                                          Next: C-Instructors

C Programming Examples

Like an other languages C also have its own program structure which defines the sequence of writing statements or some rules or constraints which are need to follow when writing a program. Skelton of C program that remains same in every program is:

#include <stdio.h> 
#include<conio.h> /header files 
void main ()
< variable declaration part >;
(Body Part)
Some important points while writing a program:
  • Header files are needed to be declared first before starting the void main ( ) section.
  • void main ( ) is a function which defines that a program is started from here. Without ‘main ( )’ program can`t be executed by a compiler.
  • Starting and Ending braces ‘{}’ are compulsory to write. Without it program can`t be executed.
  • Every statement after a variable defines the logic of a program and changes according to user`s problem.
  • Variables are compulsory to be declaring just after the starting bracket.
  • Every statement of a C program is terminated by a semicolon ( ; ).
C-Basic Programs:
Example 1: Write a C program to calculate sum of two numbers?
void main ( )
int num1, num2, sum = 0;
printf(“Enter a value of Number1 : ”);
printf(“Enter a value of Number2 : ”);
sum = num1 + num2;
printf(“The SUM is : %d ”, sum);
In the above example “sum” variable is initialized with ‘0’ it means it will not store a garbage value rather store Zero.
Sometimes we need to take an input from user because every user have a input according to their requirements that input can be taken from printf( ) and scanf( ) functions. We can say that these functions are used to make an program user interactive.

What are printf( ) and scanf( ) functions in C programming?

1. printf( ): – this function is used to show the formatted output on computer screen. It means if we want to display something on a computer output screen then this function is used. printf( ) contains the string, so whatever we want to display is to be enclosed in a double quotes. This function can be written in a two form:
1. Only format string, No argument list.
2. Format string with argument list.
Only format string, No argument list: in the above mentioned example we have written printf(“Enter a value of Number1 : ”); this an format string only because no variable or formatted specifies like ‘%d’ is written inside printf( ) function. This statement will exactly get print on output screen.
Format string with argument list: in above example we have written printf(“The SUM is %d ”,sum); this statement consists a format specifier i.e. %d and a variable named sum. Now what this statement will do, it will replace a ‘sum’ variable`s value at the place of format specifier (%d) and then display.
Suppose input has given 10 and 20. The value of sum will be 30. So this will print like this:
The SUM is : 30
The more the format specifier the more variables are needed to be required and separated with comma.
E.g.: printf(“The two Numbers are : %d %d”,num1, num2);
Note: Format specifier are needed to be separated with space and argument list with comma.
The definition of this function is defined in header file <stdio.h> so whenever this function is used the <stdio.h> header file is compulsory to be defined.
2. scanf( ): This function is used to take input typed by the keyboard from the user and sends to the memory of CPU for further calculation. This will read only till the next white space character, it means read only one word. The definition of this is also defined in <stdio.h> header file. This function can`t only contain the format strings, it will always contain format specifiers with argument list.
Address Of (&) operator is needed to specify before an argument list as mentioned in above example.
Note: The printf() and scanf() both functions are called “FORMATTED CONSOLE I/O FUNCTIONS”.
 I taught you how the program was written on a paper, Now it’s time when you will really get interaction with the C screen.

How and Where to write a ‘C’ Program?

I would like to tell you now where they program being typed, how to execute the program, what steps are needed to be follow:
The Path where you will get the actual screen to type the program is c:turboc2tc. The extension of every C file would be .c means suppose the above example is to be saved then it will get saved with <filename>.c
To execute executable file, extension is not needed.
To Save source code file shortcut is f2.
Following steps are needed to be required for setting Path for source code:
1. Press Alt+f for file menu and then change directory and type the path
2. For another path for library functions:
3. Press Alt+o  in Include directories type: – C:Turboc2Include
4. And output directory is also need to set where you want to save file.
Whatever we typed is a source code but to see the output the source code is need to get convert into Binary code. This process is known as Compilation and Compiler is used for this. If there is any syntax related error in code like semicolon missing or invalid variable name then program`s execution will get stopped. For this process press Alt+f9
Now this is not the final object file or executable file because there is not a linking of predefined function [ printf( ), scanf( ) ]
The definitions of these functions are defined in <stdio.h> how they function will work is need to be replace at their places.
This process is called Linking process. After this process a new file will get generate with same name but with different extension i.e. .exe. for this process press Ctrl+f9
To view output press Alt+f5

C-Constants:Previous                                                                          Next: C-Instructors