Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Computer Programming Chapter2part2

The document discusses various input/output functions in C programming. It explains the printf() and scanf() functions in detail, including their proper usage and examples. It also briefly covers other I/O functions like gets() and puts().

Uploaded by

breakdamt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Computer Programming Chapter2part2

The document discusses various input/output functions in C programming. It explains the printf() and scanf() functions in detail, including their proper usage and examples. It also briefly covers other I/O functions like gets() and puts().

Uploaded by

breakdamt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Presentation Title

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

Read data (input) Write out data (output)

Include <stdio.h> include


printf() function

printf() function
Send data to output device regarding to a
given format

Write out data and display as output

format printf(“output_format”,print_list) ;

example printf(“First example“) ;


Output
printf(“The bread price is RM %lf“,price) ;
printf() function (continued..)

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:

printf(“Universiti Tun Hussein Onn Malaysia”);

Format output –text/string


Output:

Universiti Tun Hussein Onn Malaysia


printf() function (continued..)

Example 2:

printf(“%lf”, salary);

variable
Format output- placeholder

Output:
printf() function (continued..)

Example 3:

printf(“Monthly salary is RM %lf”, salary);

Output format -Text variable

placeholder
Output:
Monthly salary is RM
printf() (function continued..)

Example 4:

const float porosity 16.78;


printf(“The porosity of sand = %lf ”, porosity);

Output format -Text


constant
placeholder
Output:
The porosity of sand = 16.78000
printf() function (continued..)

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

format Using percent symbol (%) followed by character


which represent particular data type

Placeholder Output to be print


%d Integer number
%lf Floating number
%c Character
%s String
printf() function (continued..)

Example 5 (Printing character value ) :


char huruf1, huruf2;
huruf1 = ‘B’;
huruf2 = ‘P’;

printf(“%c %c %c”, ‘A’, ‘B’, ‘U’);


printf(“\nI live in %c%c”,huruf1,huruf2);

Output:
ABU
I live in BP
printf() function (continued..)

Example 6 (Printing integer value) :

int number1, number2;

printf(“My house number is %d”, 26);


number2=10;
printf(“\n\tNumber %d and %d”,number1,number2);
number2=2;
number1= 15;
printf(“Number %d and %d”,number2,number1);
Output:

My house number is 26
Number and 10Number 2 and 15
printf() function (continued..)

Example 6 (Printing floating point number) :

double sahih1, sahih2;

printf(“The value of pi is %lf”, 3.142);


sahih1= 23.46;
printf(“\nThe value of sahih1 is %lf”,sahih1);
sahih2= 15.78;
printf(“\tThe value of sahih2 is %lf’,sahih2);

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

Read data with specified


scanf() function
format

Read input from keyboard

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

expression constant function

Not allowed!!!
scanf() function (continued..)

Example1:

scanf(“%lf %d %c”,&float_num, &num,&letter);

Placeholder Variable

The above statement is valid!

Important Note!!!!!
Every variables to be used in the program MUST be declared
first!!!

Faculty of Information Technology & Multimedia


scanf() function (continued..)

Example 2:
scanf(“ %d ”, 15);

Placeholder Constant value

The above statement is INVALID!

scanf(“ %lf ”, &price + 0.05);

Conversion character Expression

The above statement is INVALID!


scanf() function (continued..)

Example 3(program code):

#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:

Enter the semester : 3


Enter your CPA : 3.45
Your CPA for semester 3 is 3.45
scanf() function (continued..)

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

There are some functions can be used in the input and


ouput operations in C programming such as:

❑gets() dan puts() function


Other I/O functions

To read string input from input device


gets() function

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

printf(“Enter your name : “);


scanf(“%s”,&name);
printf(“Your name is %s “,name);
getch();
}
Other I/O functions
Example 5: gets()
If we use gets() function to read string input, it will takes all the
data including space between two words (if any)
#include <stdio.h>
void main() Enter your name: Siti Aminah
{ char name[20]; Your name is Siti Aminah

printf(“Enter your name : “);


gets(name);
printf(“Your name is %s\n“,name);
getch();
}
Other I/O functions

To display string output to output device


puts() functions

format puts (variable);

example
printf(“Your name is ”);
puts(name);
Formatting Output

formatting To format the display of an integer


Integer functions value

format %spaced
Where to use? Use the integer formatting in the
output statement

How it works? •Provide spaces according to formatting


•Put the value to display starting from right to left
side
•If there is – sign in the formatting, values will
be printed starting from left to right side
Formatting Output
Assume _ is
an empty
Formatting Integer space

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

How it works? •Provide spaces according to formatting


•Put the value to display starting from right to left
side
•If there is – sign in the formatting, values will
be printed starting from left to right side
Formatting Output
format
Formatting Floating number %precisionf

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_ _ _ _ _ _

Spaces is including floating point


Formatting Output (cont..)
format
Formatting character %precisionc

example char letter = ‘A’; output A


printf(“%c”,letter);
printf(“%2c”,letter);
_A
printf(“%-5c”,letter); A_ _ _ _
Formatting Output (cont..)
format
Formatting String %precisions

char ayat[] =“KUITTHO PARIT RAJA”


printf(“%s”,ayat);
example printf(“%4s”,ayat);
printf(“%22s”,ayat);
printf(“%.7s”,ayat);
printf(“%-20.13s”,ayat);
output
KUITTHO PARIT RAJA
KUITTHO PARIT RAJA
_ _ _ _KUITTHO PARIT RAJA
KUITTHO
KUITTHO PARIT_ _ _ _ _ _ _
Formatting Output (cont..)
Exercise
1. What is the output of this statement?
printf(“%8.5s”,”BIT 1033”)

2.State the output of these statements:


float h = 253.7654;
int g=6789;
printf(“%5d %5.1f ”,g,h);
printf(“%10d %10.3f ”,g,h);
printf(“%-7d %-7.5f ”,g,h);
Lab exercise 2b : Submit during lab session
• Questions:
Imagine you are teaching a primary class on basic arithmetic operations. You required to develop a Simple
Calculator Program. The program should take the following information from the user:
1. Integer number
2. Integer number
Then, the program should perform the following operation:
1. Add.
2. Subtract.
3. Multiply
4. Power
Here are some specific requirements need to fulfil in Simple Calculator Program:
1. The program should be written in C.
2. The program should use appropriate data types for the input and output values
3. If the output value is real number should be display in two decimal places.
4. User-friendly interface
5. Well organized program
Terima kasih

You might also like