Moät soá kieán thöùc cô sôû veà laäp trình baèng ngoân ngöõ C

Phaâàn tröôùc | Muïc luïc | Phaâàn sau

IV. Kieåu vaø teân döõ lieäu trong C

Noäi dung:

Vôùi phaàn naøy ta laøm quen vôùi caùc boán kieåu döõ lieäu cô sôû cuûa ngoân ngöõ C:

 

Töø khoùa

Töø khoùa laø töø daønh rieâng ñeå xaùc ñònh moät ñaëc tröng cuûa ngoân ngöõ, khoâng ñöôïc duøng laøm bieán, haèng hoaëc teân haøm. ANSI C coù 32 töø khoùa sau (vaø chæ vieát baèng chöõ thöôøng !!):

Keyword

Description

 

Keyword

Description

auto

Storage class specifier

 

int

Type specifier

break

Statement

 

long

Type specifier

case

Statement

 

register

Storage class specifier

char

Type specifier

 

return

Statement

const

Storage class modifier

 

short

Type specifier

continue

Statement

 

signed

Type specifier

default

Label

 

sizeof

Operator

do

Statement

 

static

Storage class specifier

double

Type specifier

 

struct

Type specifier

else

Statement

 

switch

Statement

enum

Type specifier

 

typedef

Statement

extern

Storage class specifier

 

union

Type specifier

float

Type specifier

 

unsigned

Type specifier

for

Statement

 

void

Type specifier

goto

Statement

 

volatile

Storage class modifier

if

Statement

 

while

Statement

Kieåu döõ lieäu char

Moät ñoái töôïng cuûa kieåu char bieåu thò moät kyù töï ñôn öùng vôùi baûng maõ kyù töï 8 bits ASCII.

Bieán kyù töï

char  variablename;

hoaëc:

char  variablename1, variablename2, variablename3;

Format Kyù töï: %c

Haèng kyù töï

x = `A';
x = 65;

hoaëc

 
x = `a';
x = 97;

Kyù töï thoùat  (\)

Kyù töï

Moâ taû

\b

BACKSPACE

\f

Keát thuùc trang (form-feed)

\r

Xuoáng haøng (RETURN)

\t

TAB

Xuaát caùc kyù töï

Duøng haøm printf(), ñònhnghi4a trong stdio.h, ñeå xuaát leân maøn aûnh.

Ví duï: In caùc kyù töï leân maøn aûnh.

 
1:  /* Printing out characters */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char c1;
7:     char c2;
8:
9:     c1 = `A';
10:    c2 = `a';
11:    printf("Convert the value of c1 to character: %c.\n", c1);
12:    printf("Convert the value of c2 to character: %c.\n", c2);
13:    return 0;
14: }
 
Keát quaû:
 
Convert the value of c1 to character: A.
Convert the value of c2 to character: a.

Ví duï: Bieán ñoåi soá thaønh kyù töï (duøng format %c)

1:  /* Converting numeric values back to characters */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char c1;
7:     char c2;
8:
9:     c1 = 65;
10:    c2 = 97;
11:    printf("The character that has the numeric value of 65 is: %c.\n", c1);
12:    printf("The character that has the numeric value of 97 is: %c.\n", c2);
13:    return 0;
14: }
 
Keát quaû:
 
The character that has the numeric value of 65 is: A.
The character that has the numeric value of 97 is: a.

Kieåu döõ lieäu int

Döõ lieäu soá khoâng coù thaønh phaàn thaäp phaân. Khoaûng lôùn cuûa noù phuï thuoäc vaøo ñoä daøi boä nhôù löu tröõ.

Soá nguyeân 32 bits : töø 2147483647 (231_1) ñeán -2147483648.
Soá nguyeân 16 bits : töø 32767 (215_1) ñeán -32768.

Bieán int

int  variablename;
int  variablename1, variablename2, variablename3;

Format int: %f

Ví duï: Bieán ñoåi kyù töï thaønh soá (duøng format %d)

1:  /* Showing the numeric values of characters */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char c1;
7:     char c2;
8:
9:     c1 = `A';
10:    c2 = `a';
11:    printf("The numeric value of A is: %d.\n", c1);
12:    printf("The numeric value of a is: %d.\n", c2);
13:    return 0;
14: }
 
Keát quaû:
The numeric value of A is: 65.
The numeric value of a is: 97.

Kieåu döõ lieäu float 

Döõ lieäu soá thöïc bieåu dieãn baèng daáu chaám ñoäng.

Soá thöïc 32 bits: Khoûang giaù trò +/-1.0*1037, coù ít nhaát 6 soá leû thaäp phaân. Caùc pheùp tính giöõ soá thöïc vaø soá thöïc hoaëc vôùi soá nguyeân cho keát quaû kieåu float.

Bieán float

float  variablename;
float  variablename1, variablename2, variablename3;

Format float: %f

Ví duï: Xuaát keát quaû chia soá int vaø float

1:  /* Integer vs. floating-point divisions */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int int_num1, int_num2, int_num3;   /* Declare integer variables */
7:     float flt_num1, flt_num2, flt_num3; /* Declare floating-point variables */
8:
9:     int_num1 = 32 / 10;    /* Both divisor and dividend are integers */
10:    flt_num1 = 32 / 10;
11:    int_num2 = 32.0 / 10;  /* The divisor is an integer */
12:    flt_num2 = 32.0 / 10;
13:    int_num3 = 32 / 10.0;  /* The dividend is an integer */
14:    flt_num3 = 32 / 10.0;
15:
16:    printf("The integer divis. of 32/10 is: %d\n", int_num1);
17:    printf("The floating-point divis. of 32/10 is: %f\n", flt_num1);
18:    printf("The integer divis. of 32.0/10 is: %d\n", int_num2);
19:    printf("The floating-point divis. of 32.0/10 is: %f\n", flt_num2);
20:    printf("The integer divis. of 32/10.0 is: %d\n", int_num3);
21:    printf("The floating-point divis. of 32/10.0 is: %f\n", flt_num3);
22:    return 0;
23: }
 
Keát quaû:
 
The integer divis. of 32/10 is: 3
The floating-point divis. of 32/10 is: 3.000000
The integer divis. of 32.0/10 is: 3
The floating-point divis. of 32.0/10 is: 3.200000
The integer divis. of 32/10.0 is: 3
The floating-point divis. of 32/10.0 is: 3.200000

Kieåu döõ lieäu double

Döõ lieäu soá thöïc coù ñoä chính xaùc gaáp ñoâi.

Soá thöïc 64 bits, coù ít nhaát 10 soá leû thaäp phaân.

Bieán double

double  variablename;
double  variablename1, variablename2, variablename3;

Format double: %e, %E

Quy öôùc ñaët teân bieán

Caùc kyù töï sau ñaây ñöôïc duøng ñaët teân bieán: A-Z, a-z, 0-9 (nhöng khoâng ñöôïc ñaët ñaàu), daáu gaïch döôùi (_).

Bieán khoâng ñöôïc chöùa kyù hieäu toaùn töû.
Bieán khoâng ñöôïc chöùa daáu (.).
Bieán khoâng ñöôïc chöùa (‘).
Bieán khoâng ñöôïc chöùa kyù hieäu ñaëc bieät nhö  *, @, #, ?, v.v..

Caûnh baùo

Khoâng bao giôø duøng töø khoùa cuûa C hoaëc teân caùc haøm thö vieän C laøm teân bieán maëc duø coù theå ñöôïc.

Baøi taäp

  1. Vieát chöông trình xuaát caùc giaù trò soá cuûa caùc kyù töï Z vaø z..
  2. Vieát chöông trình xuaát caùc caùc kyù töï coù giaù trò soá 72 and 104.
  3. Cho soá double dbl_num = 123.456;,vieát chöông trình xuaát giaù trò treân theo format float vaø doule.
  4. Vieát chöông trình xuaát giaù trò baèng soá cuûa kyù töï xuoáng haøng (\n).

Phaâàn tröôùc | Muïc luïc | Phaâàn sau