The C Language

Programming & Language
Telling the computer what to do
2005.04 綠園
Programming

Why programming?


For solving problem.
Who program the program?

The programmer is programming a program!
The Programmer


Convert problem solutions into
instruction for the computer.
Coordination meetings with users,
managers, system analysts, and with
peers who evaluate your work.
The Programming Process





defining the problem
planning the solution
coding the program
testing the program,
documenting the
program.
Preparing a program for execution
The C Language
2004.09 綠園
History of C





西 元 1960 年 Algo 60 語 言 ( International
Committe )
西 元 1963 年 CPL 語 言 ( 劍 橋 與 倫 敦 大 學 )
西 元 1966 年 BCPL 語 言 下 ( 劍橋大學
Martin Richards )
西 元 1970 年 B 語 言 ( AT&T Ken Thompson )
西 元 1972 年 C 語 言 ( AT&T Dennis Ritchie )
Program I : Hello, I am …
#include <stdio.h>
#include <stdlib.h>
int main( )
{
printf("Hello, I am Jenny!\n");
printf("This is output from my first program!\n");
system("PAUSE");
return 0;
}
Program II : 5 + 7 = ?
執行結果
#include <stdio.h>
#include <stdlib.h>
int main()
a
b
c
{
5
7
12
int a, b, c;
a = 5;
b = 7;
5+7
c = a + b;
printf("%d + %d = %d\n", a, b, c);
system("PAUSE");
return 0;
}
Program III : How old are you ?
#include <stdio.h>
#include <stdlib.h>
int main( )
{
int myage;
執行結果
myage = 16;
printf("I am %d years old. \n", myage);
printf("Next year, I will be %d years old. \n", myage+1);
system("PAUSE");
return 0;
}
How printf( ) work?






printf("Hello");
printf("Hello\n");
printf("%d", b);
printf("The temperature is ");
printf("%d", b);
printf(" degrees\n");
printf("The temperature is %d
degrees\n", b);
printf("%d + %d = %d\n", a, b, c);
資料型態
(character, 字元) char
(integer, 整數) int
(long integer, 長整數) long
(short integer, 短整數) short
(浮點數) float
(倍精度浮點數) double
佔1
佔4
佔4
佔2
佔4
佔8
byte
bytes
bytes
bytes
bytes
bytes
How printf( ) work?




int (整數) uses %d
float (浮點數) uses %f
char (字元) uses %c
character strings(字串)use %s
變數


變數名稱要有意義,以增加程式的可讀
性。(如num表數字、stud表學生人數)
變數名稱的限制:





不能使用C語言的關鍵字。
變數名稱的有效長度是前八位元。
可以是英文字母、數字或底線。
不能有空白字元。
第一個字元不能是數字。
計算BMI (一)



已知:身高 height = 168;
體重 weight = 55;
求:BMI
運算式:
BMI = weight / (height)2
(身高單位為公尺、體重單位為公斤。)
 BMI = weight/(height*height/10000);


輸出:你的 BMI 值為xx.xx (%.2f)
執行結果
Program IV : a + b = ?
執行結果
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c;
printf("Enter the first value:");
scanf("%d", &a);
printf("Enter the second value:");
scanf("%d", &b);
c = a + b;
printf("%d + %d = %d\n", a, b, c);
system("PAUSE");
return 0;
}
Program V : How old are you ?
#include <stdio.h>
執行結果
#include <stdlib.h>
int main()
{
int yourage;
printf("How old are you?");
scanf("%d", &yourage);
printf("Next Year, you will be %d years old.\n", yourage+1);
system("PAUSE");
return 0;
}
How scanf() work?

scanf("%d", &b);







int uses %d
float uses %f
char uses %c
character strings use %s
scanf("%d %d", &a, &b);
scanf("%d,%d", &a, &b);
範例程式、執行結果
計算BMI (二)


輸入:身高(height), 體重(weight)
運算式:



BMI = weight / (height)2
BMI = weight / (height * height)
(身高單位為公尺、體重單位為公斤。)
輸出:你的BMI值為xx.xx
執行結果