<< Chapter < Page Chapter >> Page >

Khai báo các biến ngoài (các biến toàn cục) phần này không bắt buộc: phần này khai báo các biến toàn cục được sử dụng trong cả chương trình.

Chương trình chính phần này bắt buộc phải có<Kiểu dữ liệu trả về>main(){Các khai báo cục bộ trong hàm main: Các khai báo này chỉ tồn tại trong hàm mà thôi, có thể là khai báo biến hay khai báo kiểu.Các câu lệnh dùng để định nghĩa hàm mainreturn<kết quả trả về>; // Hàm phải trả về kết quả}

Cài đặt các hàm<Kiểu dữ liệu trả về>function1( các tham số){Các khai báo cục bộ trong hàm.Các câu lệnh dùng để định nghĩa hàmreturn<kết quả trả về>;}…

Lưu ý: Một số tập tin header thường dùng:

Một chương trình C bắt đầu thực thi từ hàm main (thông thường là từ câu lệnh đầu tiên đến câu lệnh cuối cùng).

Các tập tin thư viện thông dụng

Đây là các tập tin chứa các hàm thông dụng khi lập trinh C, muốn sử dụng các hàm trong các tập tin header này thì phải khai báo #include<Tên tập tin>ở phần đầu của chương trình

1) stdio.h: Tập tin định nghĩa các hàm vào/ra chuẩn (standard input/output). Gồm các hàm in dữ liệu (printf()), nhập giá trị cho biến (scanf()), nhận ký tự từ bàn phím (getc()), in ký tự ra màn hình (putc()), nhận một dãy ký tự từ bàm phím (gets()), in chuỗi ký tự ra màn hình (puts()), xóa vùng đệm bàn phím (fflush()), fopen(), fclose(), fread(), fwrite(), getchar(), putchar(), getw(), putw()…

2) conio.h : Tập tin định nghĩa các hàm vào ra trong chế độ DOS (DOS console). Gồm các hàm clrscr(), getch(), getche(), getpass(), cgets(), cputs(), putch(), clreol(),…

3) math.h: Tập tin định nghĩa các hàm tính toán gồm các hàm abs(), sqrt(), log(). log10(), sin(), cos(), tan(), acos(), asin(), atan(), pow(), exp(),…

4) alloc.h: Tập tin định nghĩa các hàm liên quan đến việc quản lý bộ nhớ. Gồm các hàm calloc(), realloc(), malloc(), free(), farmalloc(), farcalloc(), farfree(), …

5) io.h: Tập tin định nghĩa các hàm vào ra cấp thấp. Gồm các hàm open(), _open(), read(), _read(), close(), _close(), creat(), _creat(), creatnew(), eof(), filelength(), lock(),…

6) graphics.h: Tập tin định nghĩacác hàm liên quan đến đồ họa. Gồm initgraph(), line(), circle(), putpixel(), getpixel(), setcolor(), …

Còn nhiều tập tin khác nữa.

Cú pháp khai báo các phần bên trong môt chương trình c

Chỉ thị #include để sử dụng tập tin thư viện

Cú pháp:

#include<Tên tập tin>// Tên tập tin được đạt trong dấu<>

hay #include “Tên đường dẫn”

Menu Option của Turbo C có mục INCLUDE DIRECTORIES, mục này dùng để chỉ định các tập tin thư viện được lưu trữ trong thư mục nào.

Nếu ta dùng #include<Tên tập tin>thì Turbo C sẽ tìm tập tin thư viện trong thư mục đã được xác định trong INCLUDE DIRECTORIES.

Ví dụ: include<stdio.h>

Nếu ta dùng #include”Tên đường dẫn” thì ta phải chỉ rõ tên ở đâu, tên thư mục và tập tin thư viện.

Ví dụ:#include”C:\\TC\\math.h”

Trong trường hợp tập tin thư viện nằm trong thư mục hiện hành thì ta chỉ cần đưa tên tập tin thư viện. Ví dụ: #include”math.h”.

Ví dụ:

#include<stdio.h>

#include<conio.h>

#include “math.h”

Chỉ thị #define để định nghĩa hằng số

Cú pháp:

#define<Tên hằng><Giá trị>

Ví dụ:

#define MAXINT 32767

Khai báo các prototype của hàm

Cú pháp:

<Kiểu kết quả trả về>Tên hàm (danh sách đối số)

Ví dụ:

long giaithua( int n); //Hàm tính giai thừa của số nguyên n

double x_mu_y(float x, float y);/*Hàm tính x mũ y*/

Cấu trúc của hàm “bình thường”

Cú pháp:

<Kiểu kết quả trả về>Tên hàm (các đối số)

{

Các khai báo và các câu lệnh định nghĩa hàm

return kết quả;

}

Ví dụ:

int tong(int x, int y) /*Hàm tính tổng 2 số nguyên*/

{

return (x+y);

}

float tong(float x, float y) /*Hàm tính tổng 2 số thực*/

{

return (x+y);

}

Cấu trúc của hàm main

Hàm main chính là chương trình chính, gồm các lệnh xử lý, các lời gọi các hàm khác.

Cú pháp:

<Kết quả trả về>main( đối số)

{

Các khai báo và các câu lệnh định nghĩa hàm

return<kết quả>;

}

Ví dụ 1:

int main()

{

printf(“Day la chuong trinh chinh”);

getch();

return 0;

}

Ví dụ 2:

int main()

{

int a=5, b=6,c;

float x=3.5, y=4.5,z;

printf(“Day la chuong trinh chinh”);

c=tong(a,b);

printf(“\n Tong cua %d va %d la %d”,a,b,c);

z=tong(x,y);

printf(“\n Tong cua %f và %f là %f”, x,y,z);

getch();

return 0;

}

Bài tập

Bài 1: Biểu diễn các hằng số nguyên 2 byte sau đây dưới dạng số nhị phân, bát phân, thập lục phân

a)12b) 255c) 31000d) 32767e) -32768

Bài 2: Biểu diễn các hằng ký tự sau đây dưới dạng số nhị phân, bát phân.

a) ‘A’b) ’a’c) ‘Z’d) ’z’

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình và ngôn ngữ lập trình. OpenStax CNX. Aug 06, 2009 Download for free at http://cnx.org/content/col10886/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình và ngôn ngữ lập trình' conversation and receive update notifications?

Ask