<< 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 microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
what's microbiome?
john Reply
Microbiology is the scientific study of microorganisms
Ibra
the microorganisms in a particular environment (including the body or a part of the body).
Ibra
describe the bacterial cell
Akello
The biggest populations of microbes reside in the gut.other popular habitats include the skin genitals.The microbial cells and their genetic material ,the microbiome ,live with human from birth .
Zahreen
can agriculture be integrated into biology
David
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