You are on page 1of 18

COMPUTER SCIENCE

PRACTICES AND
APPLICATIONS
PROGRAMMING LANGUAGE C
PROGRAMMING

Source program
Preprocessing ( 前處理 )
Compiling ( 編譯 )
Linking ( 連結 )
Execution (0101……code)
IDE

• INTEGRATED DEVELOPMENT ENVIRONMENTS (IDE)


• C ON LINE
• DEV C++:
• ENBARCADERO

• VISUAL STUDIO
A SIMPLE PROGRAM
• #include <stdio.h>
• int main()
• {
• printf(“hi”);
• return 0;

• }

1. 大小寫有區別
2. 程式執行是從 main 開始
3. Main program 一般稱之主程式
4. int main(void)
5. There is only one main program…
6. return 0 代表主程式的正常結束
7. # 符號開頭指令代表前處理器 (preprocessor) 所處理的指令
8. <stdio.h> 代表系統標準函示庫
9. 每一個 statement 都是以分號結尾
FREE STYLE
COMMENTS 註解

• /* */, MULTIPLE LINES


• //, SINGLE LINE
PRINT STRINGS

• printf(“abc \n”);
• \n 是換行符號 (newline)
VARIABLES ( 變數 )

• Variable declaration
• Type:
• int 整數 (integer)
• Float 浮點數 (floating point number)
• Name
• Type Name;
• int number1; // int denotes integer
• Type Name1, Name2, Name3;
ASSIGNMENTS

• = is a assignment.
• 指定符號
• 將 = 符號右邊的值指定給 = 符號左邊的變數
INITIALIZATION

• 初始化
• int a=3;

• =======
• int a;
• a=3;
PRINT THE VALUE OF VARIABLES

• int a;
• printf(“%d”,a);
• %d means a decimal number
• printf(“The first a is equal to %d, and the second one is equal to %d”,a,a);
• printf(“%f”,a);
• %f means a floating number
• %.2f 小數點後兩位
READING INPUT

• scanf(“%d”,&a);
• & denotes an address
• &a means the address of the variable a
IDENTIFIER

• Illegal identifiers
• 10times
• A-b-c
• Main, int
DEFINE A CONSTANT

• #define pi 3.14
EXPRESSIONS

• +
• -
• *
• /
• %, 6%4=2
• ()
EXERCISES

• SEE PROGRAMMING PROJECTS IN PP. 34-35 IN THE TEXTBOOK


EXERCISE 1

• Write a program that asks the user to enter a dollars-and-cents amount, then displays the amount
with 5% tax added:
• Enter an amount: 100.00
• With tax added: $105.00
EXERCISE 2

• Write a program that asks the user to enter a U.S. dollar amount and then shows how to pay that
amount using the smallest number of $20, $10, $5 and $1 bills:
• Enter a dollar amount: 93
• $20 bills: 4
• $10 bills: 1
• $5 bills: 0
• $1 bills: 3

You might also like