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

IX. Chuyeån ñoåi döõ lieäu vaø haøm toaùn hoïc

Noäi dung: Töø khoùa chuyeån ñoåi döõ lieäu:

Caùc haøm toaùn hoïc cô baûn:

 

Töø khoùa chuyeån ñoåi signed

signed char ch;

Bieán char coäng hoøa coù daáu,nghóa laø khoaûng giaù trò cuûa noù seõ laø -128 ñeán 127. Bieán char khoâng daáu coù khoaûng töø 0 – 255.

Ghi chuù

Ñeå chuyeån moät soá döông thaønh 1 soá aâm ôû daïng nhò phaân, ta laáy phaàn buø cuûa soá döông daïng nhò phaân roài coäng theâm 1.
Ví duï: Bieåu dieãn daïng nhò phaân cuûa -12345 ?
 12345
à 0011000000111001.
~ 0011000000111001 
à 1100111111000110

1100111111000110 + 1 à 1100111111000111 laø daïng nhò phaân cuûa -12345.

Töø khoùa chuyeån ñoåi unsigned

unsigned int x;

soá nguyeân 16 bits maëc ñònh coù khoaûng [-32768, 32767] bieán ñoåi thaønh soá nguyeân x coù khoaûng giaù trò [0, 65535] (216_1).

Chuaån ANSI cho pheùp chæ thò haèng soá thaønh khoâng daáu baèng tieáp vó ngöõ U hoaëc u.Ví duï:

unsigned int x, y;
x = 12345U;
y = 0xABCDu;

Ví duï:

1:  /* Using signed and unsigned modifiers */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     signed char  ch;
7:     int          x;
8:     unsigned int y;
9:
10:    ch = 0xFF;
11:    x = 0xFFFF;
12:    y = 0xFFFFu;
13:    printf("The decimal of signed 0xFF is %d.\n", ch);
14:    printf("The decimal of signed 0xFFFF is %d.\n", x);
15:    printf("The decimal of unsigned 0xFFFFu is %u.\n", y);
16:    printf("The hex of decimal 12345 is 0x%X.\n", 12345);
17:    printf("The hex of decimal -12345 is 0x%X.\n", -12345);
18:    return 0;
19: }
 
Keát quaû:
 
The decimal of signed 0xFF is -1
The decimal of signed 0xFFFF is -1.
The decimal of unsigned 0xFFFFu is 65535.
The hex of decimal 12345 is 0x3039.
The hex of decimal -12345 is 0xCFC7.

Töø khoaù thay ñoåi kích thöôùc döõ lieäu

Töø khoùa short

short x;
unsigned short y;

Töø khoùa Long

long int x;

Chuaån ANSI cho pheùp chæ thò haèng soá thaønh long baèng tieáp vó ngöõ L hoaëc l.Ví duï:

long int x, y;
x = 123456789l;
y = 0xABCD1234L;

Ví duï:

1:  /* Using short and long modifiers */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     printf("The size of short int is: %d.\n",
7:         sizeof(short int));
8:     printf("The size of long int is: %d.\n",
9:         sizeof(long int));
10:    printf("The size of float is: %d.\n",
11:        sizeof(float));
12:    printf("The size of double is: %d.\n",
13:        sizeof(double));
14:    printf("The size of long double is: %d.\n",
15:        sizeof(long double));
16:    return 0;
17: }

Keát quaû:

The size of short int is: 2.
The size of long int is: 4.
The size of float is: 4.
The size of double is: 8.
The size of long double is: 10.

Theâm h, l, or L to caùc ñaëc taû Format : %hd, %hi, hoaëc %hu

Ví duï:. Söû duïng %hd, %ld, and %lu.

1:  /* Using %hd, %ld, and %lu specifiers */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     short int         x;
7:     unsigned int      y;
8:     long int          s;
9:     unsigned long int t;
10:
11:    x = 0xFFFF;
12:    y = 0xFFFFU;
13:    s = 0xFFFFFFFFl;
14:    t = 0xFFFFFFFFL;
15:    printf("The short int of 0xFFFF is %hd.\n", x);
16:    printf("The unsigned int of 0xFFFF is %u.\n", y);
17:    printf("The long int of 0xFFFFFFFF is %ld.\n", s);
18:    printf("The unsigned long int of 0xFFFFFFFF is %lu.\n", t);
19:    return 0;
20: }

Keát quaû:

The short int of 0xFFFF is -1.
The unsigned int of 0xFFFF is 65535.
The long int of 0xFFFFFFFF is -1.
The unsigned long int of 0xFFFFFFFF is 4294967295

Haøm toaùn hoïc trong C

Moät caùch cô baûn coù theå chia laøm 3 nhoùm:

Khi duøng chuùng phaûi goïi #include math.h laø header ñaëc bieät daønh cho caùc haøm toaùn hoïc.
ÔÛ ñaây chuùng ta chæ giôùi thieäu vaøi haøm caên baûn.

sin(), cos(), vaø tan()

#include <math.h>
double sin(double x);
 
#include <math.h>
double cos(double x);
 
#include <math.h>
double tan(double x);

Bieán x coù ñôn vò laø rad vaø kieåu double.

Ví duï:

1:  /* Using sin(), cos(), and tan() functions */
2:  #include <stdio.h>
3:  #include <math.h>
4:
5:  main()
6:  {
7:     double x;
8:
9:     x = 45.0;                 /* 45 degree */
10:    x *= 3.141593 / 180.0;    /* convert to radians */
11:    printf("The sine of 45 is:    %f.\n", sin(x));
12:    printf("The cosine of 45 is:  %f.\n", cos(x));
13:    printf("The tangent of 45 is: %f.\n", tan(x));
14:    return 0;
15: }

Keát quaû:

The sine of 45 is:    0.707107.
The cosine of 45 is:  0.707107.
The tangent of 45 is: 1.000000.

pow() vaø sqrt()

#include <math.h>
double pow(double x, double y);
 
#include <math.h>
double sqrt(double x);

Trong caû 2 haøm treân bieán x laø soá khoâng aâm.

Ví duï:

1:  /* Using pow() and sqrt() functions */
2:  #include <stdio.h>
3:  #include <math.h>
4:
5:  main()
6:  {
7:     double x, y, z;
8:
9:     x = 64.0;
10:    y = 3.0;
11:    z = 0.5;
12:    printf("pow(64.0, 3.0) returns: %7.0f\n", pow(x, y));
13:    printf("sqrt(64.0) returns:     %2.0f\n", sqrt(x));
14:    printf("pow(64.0, 0.5) returns: %2.0f\n", pow(x, z));
15:    return 0;
16: }

Keát quaû:

pow(64.0, 3.0) returns: 262144
sqrt(64.0) returns:     8
pow(64.0, 0.5) returns: 8
 
Baøi taäp:
 
1. Cho ñoïan leänh sau:
int x;
unsigned int y;
x = 0xAB78;
y = 0xAB78;
Haõy vieát chöông trình bieåu dieãn caùc giaù trò x vaø y baèng soá thaäp phaân leân maøn aûnh.
 
2. Laäp chöông trình tính caùc bieåu thöùc sau :   
 

 
 
 

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