Computer Programming Chapter2part2
Computer Programming Chapter2part2
By
Name / Title / Address
Content Title
• UTHM At A Glance
• Subtitle 2
• Subtitle 3
Introduction
Formatting Output
Chap 4
I/O
Process
printf() function
Other I/O functions
scanf() function
Introduction
• Input and output are two important operations in computer
programming.
• Data will be
• read as input to computer, and
• will be processed as output.
• Input and output (I/O) can be done either through interactive or file
batch processing.
Input/Output
operation
input output
use use
scanf() function printf() function
printf() function
Send data to output device regarding to a
given format
format printf(“output_format”,print_list) ;
format printf(“output_format”,print_list) ;
text
may
output_format ““
Consist of placeholder
combination
may variable
print_list Consist of constant
expression
function_name
printf() function (continued..)
Example 1:
Example 2:
printf(“%lf”, salary);
variable
Format output- placeholder
Output:
printf() function (continued..)
Example 3:
placeholder
Output:
Monthly salary is RM
printf() (function continued..)
Example 4:
Exercise
What are the output produced from the following statements?
1.printf(“Welcome to C programming world!>”);
2.printf(“Now “);
printf(“See “);
printf(“Heart “);
3.printf(“DIT 1064\n”);
printf(“Programming Principle”);
printf() function (continued..)
escape Character
function
To tell the compiler additional formatting
to be performed with the printf() function
Character Exaplaination
\n New line
\t Tab
\a Beep sound
printf() function (continued..)
Placeholder
function
A symbol beginning with % in a format string that
indicates where to display the output value
Output:
ABU
I live in BP
printf() function (continued..)
My house number is 26
Number and 10Number 2 and 15
printf() function (continued..)
Output:
The value of pi ialah 3.142
The value of sahih1 is 23.46 The value of sahih2 is 15.78
printf() function (continued..)
Exercise
1. Given a = 2, b = 4. What are the output produced by the following
statements?
a) printf(“The additionresult of two number >> %d”,a + b);
b) printf(“\nThe addition of %d and %d “, a,b);
2. Write a valid C programming statement to display the following output
(use variables):
(Notes: Given the value of variable x = 2.5 and y = 3.1
The value of x is 2.5cm and y is 3.1cm
The mutiplication result of x and y is 7.75cm
scanf() function
format scanf(“input_format”,&variable_list) ;
example scanf(“%d“,&nom) ;
scanf(“%s %d %lf”,&name,&age,&weight) ;
scanf() function (continued..)
Consists of example
input_format placeholder %d @ %c @%lf
Called as ampersand
‘&’ symbol
Variable address/location reference
Must consist
variable_list variable
Not allowed!!!
scanf() function (continued..)
Example1:
Placeholder Variable
Important Note!!!!!
Every variables to be used in the program MUST be declared
first!!!
Example 2:
scanf(“ %d ”, 15);
#include <stdio.h>
void main()
{ int semester;
float CPA; ?
printf(“Enter the semester : “);
scanf(“%d”, &semester);
printf(“\nEnter your CPA : “);
scanf(“%lf”,&CPA);
?
printf(“\nYour CPA for semester %d is %f”,semester,CPA);
}
?
scanf() function (continued..)
Output:
Exercise
1).Write the scanf() statement to accept the following type of variables:
a. char jantina;
b. float berat;
c. int tempoh;
2).State whether the following scanf() statements is VALID or INVALID.
Given the following declaration:
int a, b;
char c;
a) scanf(“%d”, &a + b);
b) scanf(“%c”, &c);
c) scanf(“%d”,&a,&b);
Other I/O functions
format gets(variable);
example
char name[20];
printf(“Enter your name >>”);
gets(name);
Other I/O functions
Example 4: gets()
If we use scanf() function to read string input, it will only takes
the first data between two words (if any)
#include <stdio.h>
void main() Enter your name: Siti Aminah
{ char name[20]; Your name is Siti
example
printf(“Your name is ”);
puts(name);
Formatting Output
format %spaced
Where to use? Use the integer formatting in the
output statement
example output
printf(“%3d”,123); 123
example output
printf(“%5d”,123); _ _123
example output
printf(“%10d”,123); _ _ _ _ _ _ _123
example output
printf(“%-6d”,123); 123_ _ _
example output
printf(“%-4d%4d”,123,456); 123_ _456
Formatting Output
Example 1: Formatting Integer
#include <stdio.h>
void main()
{
printf(“%d “,123);
printf(“\n%3d “,123);
printf(“\n%5d “,123);
Output?
print(“\n%10d”,123);
print(“\n%-6d”,123);
print(“\n%-4d%d4d”,123,456);
}
Formatting Output
Formatting To format the display of the floating
Floating functions number value
number
format %precisionf
Where to use?
Use in the output statement
example output
printf(“%10f”,57.78); _57.780000
example
output
printf(“%15f”,57.78); _ _ _ _ _ _57.780000
example
output
printf(“%-10f”,57.78); 57.780000_
example output
printf(“%15f”,57.78); 57.780000_ _ _ _ _ _