Description
A Program is a sequence of instructions, which is used to implement an algorithm. Realistically, variables, constants and keywords may be combined to form a C program.
Steps in writing a ‘C’ program
- ‘C’ is a case-sensitive language. All keywords must be written in small case letter.
- Every ‘C’ statement must end with a semicolon ( ; ).
- No blank spaces are allowed within a variable, constant or a keyword.
Program 1: Printing a message
To print ‘HELLO’ on screen, a small program can be written. Displaying some text on screen requires just a few commands in C.
Keyword ‘Printf’ is used to print some command output on screen
Keyword ‘Scanf’ is used to input some data from the end user.
Syntax of ‘Printf’
Printf (“< format string>”,” < text to print in variable form >”);
< format string> can be
- ‘%d’ – For ‘Integer’ value
- ‘%f’ – For ‘Real’ value (decimal-point)
- ‘%c’ – For ‘Character’ value
- ‘%u’ – For ‘Pointers’
- ‘%S’ – For ‘Strings’
In case of only text, use
Printf (“Text to be printed”) ;
Using Printf or Scanf:
To use the above commands, one needs to include its declaration file which is called as HEADER FILE.
Header files have ‘.h’ extension and are used to give description of particular commands. That is, what it has to do to the compiler.
- Header file for Printf( ) and Scanf( ) functions is Stdio.h
- Syntax for giving a header file is
For example
# include < stdio.h >
# include “c:\tc\stdio”
Writing the program
- All the header files are appended at the beginning of the program.
- For the compiler to start compiling, it needs to know the starting address of the program.
- To identify any program, a program should start from a function called as ‘main ( )’. ‘( )’ indicates that ‘main’ is a function.
For example, a program to display ‘HELLO’ would look like
# include <stdio> | – | Header file at top |
Main ( ) | – | Starting of program |
{ | – | Starting braces |
Printf (“HELLO”); | – | Actual body |
Return 0; | – | To end function main ( ) |
} | – | Closing braces |
Things to Note:
- ‘Return’ is a keyword. When the computer encounters ‘Return’, it ends the program there itself.
- By default, main ( ) function needs to get an integer value to exit. Hence ‘0’ is returned by using the ‘Return 0’ command.
- To print a text, it needs to be enclosed in ( ” ” ) quotes.
Compiling the program:
‘Compiling’ means converting the program written in ‘C’ to a form that’s easily understood by the machine (Low-level form).
A program after being written can be compiled in an IDE (Integrated Development Environment) in Turbo C++. The program is saved as ‘.C’ extension.
Ctrl +F9 compile the program.
Alt + F5 is used to new result.
On compilation, the source file converts to object file and then to exe (executable) file.
- Function getch( ) can also be used to view output without using Alt+F5. Header file conio.h needs to be included within the program while using the function getch().
Comments:
In order to make the program more readable, we indicate the purpose of the entire program and the function of the commands being used in plain simple English, while coding the entire program.
There are two types of comments in C.
- Single line comment ( // )
- Multiple line comment (/* _______ */)
Note: Any number of comments can be written at any place in the program.
- Comments cannot be nested.
For example,
/*
Nesting in Comments not possible /* Nesting */
*/
It is an invalid commenting syntax.
Single line comment restricts the comment to a single line while Multiple line comment can be split over more than one line.
For example,
Single line comment
// Single line comment
Multiple line comment
/* Multiple
line comment */
Program 2: Get some input from the user and print it
For the above program, Scanf( ) function is used.
Syntax of scanf ( )
Scanf ( <String format>, &variable );
Int <variable name>; can be used to declare an integer variable.
Program:
#include<stdio.h>
Void main()
{
Scanf ( “%d”,&get _int) ;
Printf (“%”, get_int) ;
Return 0;
}
This is the required program.
Using ‘&’ in Scanf ( )
‘&’ in C language is known as an ‘address of’ operator. It gives the location in memory where the declared variable is stored.
‘C’ Instructions
C program has four types of instructions.
- ‘Type declaration’ to declare type of variables. For example, int a;
- ‘Arithmetic instruction’ for variables and constants. For example, C = a + b ;
- ‘Control instruction’ to control sequence of operations like selection, decision, loop or case control.
- ‘Input/Output’ instructions to perform input/output operations.