Chemistry Learning

  • About
  • Physics
  • Chemistry
    • Colloids
  • Mathematics
    • Trigonometry
  • Biology
    • Carbohydrates
  • Information Technology
Information Technology65132

Writing C program

A Program is a sequence of instructions, which is used to implement an algorithm. Realistically, variables, constants and keywords ....

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

  1. ‘%d’ – For ‘Integer’ value
  2. ‘%f’ – For ‘Real’ value (decimal-point)
  3. ‘%c’ – For ‘Character’ value
  4. ‘%u’ – For ‘Pointers’
  5. ‘%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

  1. All the header files are appended at the beginning of the program.
  2. For the compiler to start compiling, it needs to know the starting address of the program.
  3. 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.

  1. Single line comment ( // )
  2. 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.

  1. ‘Type declaration’ to declare type of variables. For example, int a;
  2. ‘Arithmetic instruction’ for variables and constants. For example, C = a + b ;
  3. ‘Control instruction’ to control sequence of operations like selection, decision, loop or case control.
  4. ‘Input/Output’ instructions to perform input/output operations.
Jun 5, 2009admin
Mass Energy RelationProperties of Colloids
Home Writing C program
June 5, 2009 Information TechnologyInformation Technology65133
Popular Posts
  • Adsorption Isotherm - 665763 Views
  • Langmuir Adsorption Isotherm - 554223 Views
  • Freundlich Adsorption Isotherm - 471373 Views
  • Colloidal Solution - 467163 Views
  • Adsorption - 462115 Views
  • Examples of Colloids - 394893 Views
  • Banking of Roads - 357747 Views
  • Rubber - 264502 Views
  • Colloidal Dispersions - 259576 Views
  • Difference between Lyophobic and Lyophilic - 237251 Views

Copyright © 2024 Xamplified | All Rights are Reserved