Introduction to C Language ─ C 語言的基本概念
description
Transcript of Introduction to C Language ─ C 語言的基本概念
Introduction to C Language ─ C 語言的基本概念
大綱 C 編譯器簡介 Turbo C++ 的安裝與操作介紹 如何編寫 C 語言程式 輸出與輸入函數 Homework 資料參考網站
C 編譯器簡介 C 語言是 1972 年 Dennis Ritchie 和 Ken
Thompson 兩人一起設計 Unix 作業系統時發展出來的
C 語言版本眾多,如 Turbo C 、 Borland C 、 ANSI C 、 Microsoft C 等等
C 語言到了 1980 年時,由 AT&T 貝爾實驗室的 Bjarne Stroustrup 將物件導向觀念加入 C 語言中,產生了“ C with Classes” 1985 年,更加入 operator
overloading 、 virtual function ,而完成 C++語言的設計
C 編譯器簡介 ( 續 ) C 語言的編寫可直接使用文字編輯器來編輯,像 Windows 下的記事本、 UltraEdit 、 DOS 下的 PE II 、 UNIX 下的 vi 或 joe 等
C 語言也可以用 C 語言的程式編輯器軟體來編輯,如 Windows 下的 Visual C++ 、Borland C++ 、 Turbo C++ 等 已整合編輯、編譯、連結、執行和偵錯的功能
C 編譯器簡介 ( 續 ) 編譯式語言和直譯式語言
原始程式Source Code
執行
直譯程式Interpreter
原始程式Source Code
執行目的碼Object Code
編譯程式Compiler
連結程式Linker
可執行碼Executable
Code
程式庫Library
Turbo C++ 的安裝與操作介紹1. 複雜“ Turbo C++ Version 3.0.zip” 至
C:\ 下2. 解壓縮 ( 會產生 TCPP 的目錄 )
3. 執行 C:\TCPP\BIN\tc.exe
Turbo C++ 的安裝與操作介紹 ( 續 )
4. Options -> Directories -> Include Directories -> c:\tcpp\include
5. Options -> Directories -> Library Directories -> c:\tcpp\lib
6. Options -> Directories -> Output Directory -> c:\tcpp
Turbo C++ 的安裝與操作介紹 ( 續 )
Turbo C++ 的安裝與操作介紹 ( 續 )File
Edit
Search
Project
Run
Compile
Options
Help
Windows
Debug
newopensaveprintquit
compile (Alt+F9)makelinkbuild allinfomation
run (Ctrl+F9)
watches
breakpoints
add watches (Ctrl+F7)
如何編寫 C 語言程式 C 語言程式的架構#include <stdio.h>int sum(int);
void main(){ int i, sum(); printf ("The sum of 1 to 100 is %d", sum(100));}
int sum(int n){ int s=0, i; for (i=1;i<=n;i++) s=s+i; return s;}
函數
函數
如何編寫 C 語言程式 ( 續 )#include <stdio.h>int sum(int);
void main(){ int i, sum(); printf ("The sum of 1 to 100 is %d", sum(100));}
int sum(int n){ int s=0, i; for (i=1;i<=n;i++) s=s+i; return s;}
單一敍述
多重敍述
如何編寫 C 語言程式 ( 續 )#include <stdio.h>int sum(int);
void main(){ int i, sum(); printf ("The sum of 1 to 100 is %d", sum(100));}
int sum(int n){ int s=0, i; for (i=1;i<=n;i++) s=s+i; return s;}
宣告區
主程式區
函式區
如何編寫 C 語言程式 ( 續 ) C 語言程式的關鍵字與識別字
void main()
{
int i=100;
printf(“The sum of 1 to 100 is %d, sum(i)”)
}
關鍵字
識別字
保留字
設計師可自定
如何編寫 C 語言程式 ( 續 ) C 語言程式設計的基本規則
一個 C 語言程式一定要有主程式,名稱為main,程式內容以 {} 包含起來
所有的變數都要先宣告其型態 每一行程式敍述結束後,必須加上一個分號(;)表示結束
每一區塊的程式敍述是以 {} 來包含 關鍵字除非另有規定,否則一律用小寫來撰寫之
程式中如有註解文字需置於雙斜線 (//) 之後,或置於 /* 和 */ 之間
如何編寫 C 語言程式 ( 續 ) Ex. 讓電腦螢幕出現訊息如“ Hello, this is my first
C program!” 執行“ c:\tcpp\bin\tc.exe” 出現 tc++ 主畫面後,選擇 File 功能表單,在下拉選單中選擇 New 選項
在視窗中央寫上#include<stdio.h>
void main(){ printf("Hello, this is my first C program!");} 輸入完成後,選擇 Compile 功能表單,在下拉選單中選擇 Compile 選項 ( 或直接按 Alt+F9)
如何編寫 C 語言程式 ( 續 )
Compile 結束後,選擇 Run 功能表單,在下拉選單中選擇 Run 選項 ( 或直接按 Ctrl+F9) ,就可執行你的第一支 C 語言程式如有 error 或 warning 出現就要開始debug 了
如何編寫 C 語言程式 ( 續 ) 程式註解編寫
/* This is my first c program File name: 1-26.c author: jbli*/
#include<stdio.h> //include the header to use printf function
void main(){ printf("Hello, this is my first C program!"); //print in screen}
輸出與輸入函數 輸出函數 printf
printf(“There are many children in this class”);
printf(“This class has %d children”, s);
printf(“ 這個人的身高是 %d 公分,體重是 %d 公斤” , height, weight);
欲輸入的字串
欲輸入的字串
s 的值將取代 %d 的位置
輸出與輸入函數 ( 續 )
格式符號 資料型態 範例%d 整數 printf("The name is %d feet tall.", h);%f 浮點數 printf("The average score is %f.", score);%s 字串 printf("The girl's name is %s.", name);%c 字元 printf("This hotel is a %c class hotel", level);
輸出與輸入函數 ( 續 ) 輸入函數 scanf
scanf(“%d”, &s);
scanf(“Input:%d”, &value);
printf(“Input:”);
scanf(“%d”, &value);
使用者輸入一個整數到變數 s 中
Homework 安裝 Turbo C++ 寫一支 C 語言程式
讓使用者輸入他的年齡,並在螢幕上顯示下列文字: You are 30 years old.You are 30 years old.
資料參考網站 http://www.strath.ac.uk/IT/Docs/Ccourse/ http://www.cs.cf.ac.uk/Dave/C/CE.html gopher://gopher.csie.nctu.edu.tw/11/NCTU/CS
IE/0Announce/comp/programming/pl/c/cpp