Lecture 05 - Interaction
Lecture 05 - Interaction
Hossein Zeinali
Slides by Dr. Bahador Bakhshi
CE Department, Amirkabir University of Technology
Interaction
Produce output
2
Interaction
Produce output
3
Printing
Printing messages
printf("This is message \n");
Printing variables
printf("format", parameters);
int i = 20;
char c = 'a';
printf("%d, %c", i, c);
6
Printing Floats
%f, %e, %E, %lf
printf("%f", 100.5f);
100.500000
float f = -2;
double d = 100;
printf("%f, %lf", f, d);
-2.000000, 100.000000
7
Printing Chars
%c
printf("%c", 'a');
a
printf("%c, %c", 'a','b');
a, b
char c1 = 'a';
printf("%c, %c, %c", c1, 'b', 65);
a, b, A
8
Special Character
Characters in printf result
\n newline
\t tab
\r carriage return
\b backspace
\" "
\% %
%% %
9
Printing Strings
%s
printf("This is message");
This is message
10
Field length
Field length is a number
Comes after % (and before the type char)
It is the minimum space reserved for print
If value is smaller than the space
Empty space
If value is larger than the space
No effect
11
Field length
printf("|%4d|\n", 1); // | 1|
printf("|%4d|\n", 12345); // |12345|
printf("|%4d|\n", -12345); // |-12345|
printf("|%4f|\n", 1234.0f); // |1234.000000|
printf("|%15f|\n", 1234.0f); // | 1234.000000|
printf("|%4c|\n", 'A'); // | A|
printf("|%-4c|\n", 'A'); // |A |
printf("|%4s|\n", "ABC"); // | ABC|
printf("|%4s|\n", "ABCDE"); // |ABCDE|
printf("|%6d|\n", 1234); // | 1234|
printf("|%-6d|\n", 1234); // |1234 |
12
Precision
Precision is a .number and comes after %
For Integer
The minimum number of digits
If (# of digits < precision) empty space = 0
For floats
With %f, %e
The number of digits after .
For strings
The maximum number of characters
13
Precision
printf("|%.4d|\n", 1); // |0001|
printf("|%.4d|\n", 12345); // |12345|
printf("|%.4d|\n", -12345); // |-12345|
printf("|%.4f|\n", 1234.0f); // |1234.0000|
printf("|%.8f|\n", 234.0f); // |234.00000000|
printf("|%.4s|\n", "ABC"); // |ABC|
printf("|%.4s|\n", "ABCDEF"); // |ABCD|
14
Field length and Precision
This is a number with format a.b
Comes after %
15
Field length and Precision
printf("|%10.5d|\n", 12);
| 00012|
printf("|%3.5d|\n", 12);
|00012|
printf("|%10.5lf|\n", 1.234567890123);
| 1.23457|
printf("|%0.5lf|\n", 1.234567890123);
|1.23457|
printf("|%15.10s|\n", "Hello, world");
| Hello, wor|
printf("|%5.10s|\n", "Hello, world");
|Hello, wor|
16
Variable Field Length & Precision : *
* can be used to specify field length and
precision which is replaced by a variable
int i = 30;
int j = 2;
float f = 1.23456789;
printf("%0*.*f\n", i, j, f);
000000000000000000000000001.23
17
Cast in printing (do NOT use)
int i = -60;
unsigned int j = 4147482648;
float f = -700.05;
18
Interaction
Produce output
19
Reading
Read from keyboard (console)
What should be determined in reading
Keyboard enters “characters”, so, how to read int, char, …?
Which type the chars should be converted?
Where should be saved?
scanf(“format”, parameters)
Format: The type that input should be converted to
Parameters: Where should be saved
scanf("%d", &i);
scanf("%u", &j);
scanf("%ld",&l);
21
Reading Integers (cont’d)
%o, %x, %X, %i
scanf("%o", &i);
Input: 12 i = 10
scanf("%x", &i);
Input: 1a i = 26
scanf("%i", &i);
Input: 12 i = 12
Input: 012 i = 10 (It reads in base 8)
Input: 0x12 i = 18 (It reads in base 16)
22
Reading floats and doubles
%f, %lf, %e
float f;
double d;
scanf("%f", &f);
scanf("%lf", &d);
90.9 90.9 is saved in memory f
88.123456789 88.123456789 saved in
memory d
Spaces at the beginning are ignored
23
Reading floats and doubles
float f1, f2;
scanf("%f", &f1);
scanf("%e", &f2);
Input:
1.23 f1 = 1.23
4.56 f2 = 4.56
Input:
1.23e+1 f1 = 12.3
4.56e-1 f2 = 0.456
24
Reading chars
%c
char c1, c2, c3;
25
Reading chars (cont’d)
White spaces (space, tab, enter) are not
ignored when reading char
To ignore white spaces, use “ “ before %c
26
Reading chars (cont’d)
getchar()
Read char after Enter
getch()
Read char without Enter, does NOT show the
char
getche()
Read char without Enter, shows the char
27
Reading Strings
%s
char str[20];
scanf("%s", str);
Input: ABC str = "ABC"
scanf("%s", str);
Input: AB C str = "AB"
28
Reading Strings
How to read a line
Contains spaces (read until end of line)
gets(s)
char str[20];
gets(str);
Input: ABC DEF str = "ABC DEF"
29
Field length in scanf
Field length specifies the maximum number of
input characters (in the buffer) used for scanning
int i, j;
scanf("%5d", &i);
Input: 122 i = 122
Input: 1234567 i = 12345
30
Special input format
If input data has special format with extra
characters
scanf can ignore them
Input: 1389/12/1
sal = 1389, mah = 12, rooz = 1
31
Format of actual input data
The format of actual input data MUST
match with the format of scanf
int a, b;
float f;
32
#include <stdio.h>
#include <stdlib.h>
int main(void){ برنامهايكهباتوليدپيغامهايمناسبوروديهاي
int i;
unsigned int j; راازكاربربگيردودرانتهاليستوروديها رابه
unsigned long int k; .كاربرنشاندهد
char c;
float f;
printf("Enter a char:\n");
scanf(" %c", &c);
printf("Enter an int:\n");
scanf("%d", &i);
printf("Enter an unsigned int:\n");
scanf("%u", &j);
printf("Enter an unsigned long int:\n");
scanf("%lu", &k);
printf("Enter a float:\n");
scanf("%f", &f);
return 0;
}
33
Common Bugs
Casting in printf or scanf
printf("%d", 120.23);
double d; scanf("%f", &d);
34
Reference
Reading Assignment: Chapter 9 of “C How
to Program”
35