ICS 103: Computer Programming in C
Handout-3
Topic: Assignment, Input and Output statements.


Objective :

• To know about assignment statement of C.
• To know how to perform I/O operations in C.
• To know use of new line character \n and tab character \t in printf function.
• To know about general form of C programs.
• To know how to format numbers in program outputs.

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 string, input list separated by coma );

Example :

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


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

Syntax :

printf(format string, print list);
OR
printf(format string);

Example :

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

OR

printf(“Enter radius of circle : “ );

Note : as explained in 2nd Lecture placeholders/conversion specifiers/ format characters for printf and scanf functions are :

Variable type scanf / printf Placeholders or 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 C language\t programming \n\n\n Welcomes you all \n”) ;

Causes the output to be printed as :

Line # 1 This is
2 ICS 103
3
4 C language programming
5
6
7 Welcomes you all


General Form of C-Programs :

The 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 parenthesis { 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. There are four main data types used in C programs: the integer data type – this variable can have values which are without decimal point, the float data type – this variable can have values which can have decimal point, the double data type – same as float data type but the precision is improved that is it can have big values, the char data type – this variable can have single characters.

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

Then the program should have the reading statement (scanf) to read the input values for the variables used as input.

Then the program should have arithmetic or other statements to find the values for output variables.

Then the program should have print statement (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 x by using the formula
x = a x b + c
and prints the value of x 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 , x ; /* float variables declaration */
double c ; /* double variable declaration */
char d ; /* character variable declaration */

/*asking 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) ;

x = a * b + c ; /* computing the value of x using arithmetic statements */
printf (“The character = %c The value of x = %f \n”, d, x) ; /* printing the output values */

} // end of main

Formatting Numbers in Program Outputs :

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 is 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 on 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.

Exercise

a) Show the exact form of the output lines when x is 5.671856

printf(“x are : %4.1f %5.3f %0.3f\n”, x ,x, x);

(Note : use b to show blank space).

b) Write single C-Statement to print the message ”This is a C Program” with each word on a separate line.


 

 

حلول نت

http://student.kfupm.edu.sa/s200372670/

Overview of C | ICS 103: Computer Programming in C | Arithmetic Expressions | Assignment, Input and Output statements | Control Structures (Repetition & Loop Statements) | Control Structures (Selection Structures) | Data Files | Recursive Functions | Simple Standard Mathematical Library Functions | Function Output Parameters Implemented with Pointers(OR Function with Output Parameter) | Function with Input Parameter | 1-D Array | 1-D Array (How to use array with function)