ICS 103: Computer Programming in C

Handout-03

Topic: Assignment, Input and Output

 

Instructor: M. Waheed Aslam

 

Objectives:

 

Executable Statements:

·       Follow the declarations in a function.

·       Used to code the algorithm in C program

·       Are translated by the C compiler into machine language.

·       When you run the program, the computer executes the machine language version of these executable statements.

 

Assignment Statements:

Syntax:

result = expression;

 

In this left hand side must be single variable name.

 

Example:

x = x+z+2.0;

              OR

a = 5;

 

In this “=” is known as assignment operator.

 


Input/Output Operations:

 

Use of scanf function for reading input from keyboard:

 

Syntax:

scanf (format, input list separated by comma);

 

Example:

scanf (“%d  %c”, &age, &first_initial);

 

Use of printf function for displaying output on the screen of the Computer System:

 

Syntax:

printf (format, print list);

                      OR

printf (format);

 

Example:

printf(“I am %d years old, and my GPA is %f\n”, age, gpa);

                          OR

printf(“Enter radius of circle: “);

 

Note: Placeholders / conversion specifires / format characters for printf and scanf functions are:

 

Variable type           scanf/printf  Placeholders/Conversion Specifiers       

 

char                                                             %c

int                                                                %d

float                                                             %f

double                                                         %lf

 

 

 

Use of new line character  \n and  tab character \t in printf function:

 

This new line character \n in the printf statement causes the next output to be printed on the new line. The tab character \t causes the next output to be printed at the next tab position.

 

Example:  The statement:

printf (“This is \n ICS \t 103 \n\n Intro. to \t C Language. \n\n\n Welcome you all \n”);

 

Causes the output to be printed as:

 

Line #        1       This is

2                   ICS   103

3                    

4                   Intro. to     C Language.

5

6

7                   Welcome you all

 

General Form of C-Programs (Section 2.4):

·       C program should always include library files at the beginning of the program. The library file stdio.h which includes Input and Output functions like scanf and printf should always be included as almost all programs use these functions.

 

·       The program should always have a main function from where the execution of the program starts.  After writing main function declaration an opening brace { should be used which indicates the start of writing of the program.

 

·       Then all the variables of different data types should be declared. It is very important that all the input and output variables should be first declared before using them in the program.

·       As we have seen, four main data types are used in C programs:

 

1.    The integer data type – this variable can have values which are without decimal point,

 

2.    The float data type – this variable can have values which can have decimal point,

 

3.    The double data type – same as float data type but the precision is improved that is   it can have big values,

 

4.    The char data type – this variable can have single characters.

 

·       The program should include the print statement (printf) asking the user what type of input should be entered.

 

·       The program generally has the reading statement (scanf) to read the input values for the variables used as input.

 

·       The program also may have arithmetic or other statements to find the values for output variables.

 

·       The program may have one or more print statement(s) (printf) to print the values of the output variables.

 

·       Then the program should close by using the closing parenthesis }.

 

·       While reading and printing, the format characters for integer variables is %d, for float variables is %f, for double variables is %lf, for character variables is %c.

 

·       While writing the formulas note that for addition use +, for subtraction use -, for multiplication use * and for division use /.

 

You can write any explanations for the reader of the program to understand the program by enclosing them within the opening symbol /* and closing symbol */. These explanations are called as comments.

 

Example:

Write a simple program that reads an integer variable a, a float variable b, a double variable c, a character variable d. Then it computes the value of a float variable z by using the formula:

                                                z = a x b + c 

and print the value of z and the character read in variable d.

 

#include <stdio.h>      /* the statement to include library file stdio.h */

void main (void)                    /* the main function declaration */

{         int         a ;                   /* integer variable declaration */

          float b , z ;                   /* float variables declaration */

          double    c ;                   /* double variable declaration */

          char        d;         /* character variable declaration */

           /* ask the user to enter the input values */

             printf(“Enter the values for integer, float, double and character variables. \n”);  

           /* reading of input variables with format character */

         scanf (“%d  %f  %lf  %c”, &a, &b, &c, &d) ;                                          

          z = a * b + c /* computing the value of z using arithmetic statements */

          printf (“The character = %c The value of z = %f \n”, d, z);/* printing the output values */

} // end of main

Formatting Numbers in Program Outputs (Section 2-5):

Formatting means arranging the way the output should be printed either on the screen or in an output file.

 

Formatting values of type   int:

To format an integer value write the %nd in the quoted parameter of the printf statement, where n is the number of columns or places to be used for the display of digits. n is called the field width.

 

Example: 

Let the variable x has -234 as its value.

 

Then if the format is given as %5d the printed output is b-234, where b here denotes a blank.

Similarly, %6d will cause the printed output to be bb-234.

 

So, if the field width is greater than the number of digits in the value then extra places are filled with blanks on the left of the number.

 

If the format is given as %2d then the printed output will be -234 without any blanks. So, if the field width is less than the number of digits in the value then the field width is ignored and the number is printed as it is without any blanks.

 

Formatting values of type   double and float:

To format a float value write a %n.mf, a double value write a %n.mlf in the quoted parameter of the printf statement, where n is the total number of digits before and after decimal point, including the decimal point; m is the number of places after the decimal point.

 

Example: 

·       Let the variable x has 3.14159 as its value, then if the format is given as %5.2f the printed output is b3.14, where b is a blank.

·       Similarly, if x is –0.006, then %.4f will cause the printed output to be -0.0060

·       So, if the total number of digits n is more than the number then blanks are placed to the left before the decimal point.

·       If the total number of columns is less then the field width, n is ignored, and the number is printed as it is by taking the number of places after the decimal point as it is given.

·       If the number of places after decimal point m is more than the number of decimal places in the value then the extra places are filled with 0’s on the right side of the digits after decimal point.