c programming language #3
Post on 13-Sep-2014
802 Views
Preview:
DESCRIPTION
TRANSCRIPT
PROGRAMMINGLANGUAGE #3
Input & Output
Keyboard
Monitor
User Interfacee
User interface is the space where interaction
between humans and machines occurs.
3.1 轉換規格3.2 轉換修飾詞3.3 旗標欄位與寬度3.4 精確度欄位3.5 函式scanf()
3.6 特殊的*修飾詞3.8 轉換的意義
Chapter 3
3.1 CONVERSION SPECIFICATION
型態字元 代表意義
%c 單一字元。
%d 十進位整數
%e 科學記號
%E 科學記號
%f 浮數數
%o 八進位數值
%u 十進位數值
%x 十六進位整數
%X 十六進位整數
Example 3-1/* ch3 printfs.c */
#include <stdio.h>#include <stdlib.h> int main(){
char ch1 = 'a', ch2 = 'A';int i = 31, j = -1;float num = 123.456;double num1 = 123.456;char str[30]= "This is a car ... SAAB";
/* output character */printf(" Format conversion...\n\n");printf(" Character : %c %c\n\n", ch1, ch2);
/* output integer number */printf(" Decimal : %d %d\n", i, j);printf(" Unsigned : %u %u\n", i, j);printf(" Octal : %o \n", i);printf(" Hexdecimal : %x \n", i);printf(" Hexdecimal : %X \n\n", i);
/* output float point number */printf(" Float : %f %e %E\n", num, num, num);printf(" Double : %f %e %E\n\n", num1, num1, num1);
/* output string */printf(" String : %s\n\n", str);
/* output % symbol */printf(" Percent : 100%%\n\n");system("PAUSE");return 0;
Console Output
Format conversion...
Character : a A
Decimal : 31 -1Unsigned : 31 4294967295Octal : 37Hexdecimal : 1fHexdecimal : 1F
Float : 123.456001 1.234560e+002 1.234560E+002Double : 123.456000 1.234560e+002 1.234560E+002String : This is a car ... SAAB
Percent : 100%
3.2 CONVERSION SPECIFIERS
欄位 意義
Flags +、-、#、
Width 列印所佔的寬度
Precision 精確度
Size short、long、double
Conversion Specidiers
3.3 FLAG AND WIDTH
Example
printf(“%d”, 100);
printf(“%5d”, 100);
001
001
Flag 意義 預設情況
- 向左靠齊。 向右方對齊
+ 加上正負符號。 負數才有 '-'號
空格 加上空白。 不列印空白
# 列印字首0、0x、或0X。
0 以數字0補足不足的寬度。 以空白補足寬度
/* ch3 flag2.c */
#include <stdio.h>#include <stdlib.h> int main(){
int decimal = 31;
printf("Flags...\n\n");printf("|%d|\n", decimal);printf("|%8d|\n", decimal);printf("|%#8o|\n", decimal);printf("|%#8x|\n", decimal);printf("|%08d|\n", decimal);system("PAUSE");return 0;
}
Console Output
Flags...
|31|| 31|| 037|| 0x1f ||00000031|
3.4 PRECISION
Example
printf(“%8.5f”, 1.4567);
07654.1
/* ch3 width3.c */
#include <stdio.h>#include <stdlib.h> int main(){
double fl = 2.5e5;
printf("|%15f|\n", fl);printf("|%-15.0f|\n", fl);printf("|%-#15.0f|\n", fl);
printf("|%-15.4f|\n", fl);printf("|%-15.4e|\n", fl);system("PAUSE");return 0;
}
Console Output
| 250000.000000||250000 ||250000. ||250000.0000 ||2.5000e+005 |
3.5 SCANF()
scanf
• Reads data from stdin and stores them into the locations pointed by the arguments.
printf
• Writes a formatted string to stdout.
#include <stdio.h>
int main( )
{
int num1;
int num2;
int sum;
printf(“Enter your number”");
scanf(“%d”, &num1);
scanf(“%d“, &num2);
sum = num1 + num2;
printf(“The Sum is %d”, sum);
getch();
return 0;
}
User Interface?
User Interface: Keyboard
User Interface: Monitor
Types of user interfaces in computer
DOS & UNIX
Mac OS & Windows 7
User Interface: Mouse
Other UI?
stdio.h = Standard Input & Output
「標準輸出」(Standard Output)?
「標準輸入」(Standard Input) ?
/* ch3 scanfs.c */
#include <stdio.h>#include <stdlib.h> int main(){
int num1, num2;double fl;char str[20];
printf("Input two number :\n");scanf("%d %d", &num1, &num2);printf(" ===> %d + %d = %d\n\n", num1, num2,
num1+num2);
printf("Input a floating point :\n");scanf("%lf", &fl);printf(" ===> %f is %e\n\n", fl, fl);
printf("Input a string :\n");scanf("%s", str);printf(" ===> %s \n\n", str);printf("Input at most 10 chars :\n");scanf("%10s", str);printf(" ===> %s\n", str);system("PAUSE");return 0;
}
Input two number :14 5060===> 14 + 5060 = 5074
Input a floating point :119.117===> 119.117000 is 1.191170e+002
Input a string :abcdefghijklmnopq===> abcdefghijklmnopq
Input at most 10 chars :abcdefghijklmnopq===> abcdefghij
Console Output
3.6 * SPECIFIER
37
/* ch3 star1.c */
#include <stdio.h>#include <stdlib.h> int main(){
int width, precision;double d_num = 1234.56789;
printf("Source Number : %f\n\n", d_num);printf("Input the width : ");scanf("%d", &width);
printf("\nInput the precision : ");scanf("%d", &precision);
printf("\nFormat ===> \"%%-%d.%df\"\n", width, precision);printf(" Formatted Number:|%-*.*f|\n", width, precision, d_num);system("PAUSE");return 0;
}
Source Number : 1234.567890
Input the width : 15
Input the precision : 4
Format ===> "%-15.4f"Formatted Number : |1234.5679 |
Console Output
3.8 TYPE CONVERSION
/* ch3 cut.c */
#include <stdio.h>#include <stdlib.h> int main(){
short int num = 321;
printf("Decimal : %d\n", num);printf("Character : %c\n", num);system("PAUSE");return 0;
}
65 (‘A’)
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 12 bytes
0 1 0 0 0 0 0 11 bytes
Binary expression
0 0 0 00 0 0 1 0 1 0 0 0 0 0 1
num = 321
Low byteHigh byte
Decimal : 321
Character : A
num = 321;
3.10 EXERCISE
#include <stdio.h>
#include <stdlib.h>
int main( )
{
double f = 678.90;
printf(“%%f……………….| %f|\n”, f);
printf(“%%3.2f……………….| %3.2f|\n”, f);
printf(“%%7.2f……………….| %7.2f|\n”, f ");
printf(“%%7.0f……………….| %7.0f|\n”, f);
printf(“%%*.1f……………….| %*.1f|\n”, 7, f);
printf(“%%*.*f……………….| %*.*f|\n”, 7, 1, f ");
system(“PAUSE”);
return 0;
}
Homework #3
• 3.11 No.5 (P.29)
• Write or Print in A4Paper
• Name & I.D.
C Programming is Easy!
Any Question?
aragornyeh@gmail.com
top related