- Hiểu được Toán tử gán
- Hiểu được biểu thức số học
- Nắm được toán tử quan hệ (Relational Operators) và toán tử luận lý (Logical Operators)
- Hiểu toán tử luận lý nhị phân (Bitwise Logical Operators) và biểu thức (Expressions)
- Hiểu khái niệm ép kiểu
- Hiểu độ ưu tiên của các toán tử.
Trong C, toán tử gán có thể được dùng cho bất kỳ biểu thức C hợp lệ
Toán tử quan hệ được dùng để kiểm tra mối quan hệ giữa hai biến, hay giữa một biến và một hằng
Toán tử quan hệ và ý nghĩa
Toán tử
|
Ý nghĩa
|
>
|
lớn
hơn
|
>=
|
lớn
hơn hoặc bằng
|
<
|
nhỏ
hơn
|
<=
|
nhỏ
hơn hoặc bằng
|
==
|
bằng
|
!=
|
không
bằng
|
Toán tử luận lý và ý nghĩa
Toán tử
|
Ý nghĩa
|
&&
|
AND: trả về kết quả là true khi cả 2 toán hạng đều true
|
||
|
OR : trả về
kết quả là true khi chỉ một trong hai
toán hạng đều true
|
!
|
NOT: Chuyển đổi giá trị của toán hạng duy nhất từ true thành false và ngược lại.
|
Ép kiểu dữ liệu là chuyển kiểu dữ liệu từ kiểu dữ liệu này thành kiểu khác,phù hợp với mục đích tính toán mặc định là từ kiểu bé về kiểu lớn. Ngoài ra có thể chỉ định ép kiểu dữ liệu mà mình mong muốn.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int s, m = 3, n = 5, r, t;
float x = 3.0 , y;
t = n/m;
printf ("\n t = : %d",t);
r = n%m;
printf ("\n gia tri r: %d",r);
y = n/m;
printf ("\n gia tri y: %f",y);
t = x*y-m/2;
printf("\n gia tri t: %d", t);
x = x*2.0;
printf("\n gia tri x: %f",x);
s = (m+n)/r;
printf("\n gia tri s: %d",s);
y = --n;
printf("\n gia tri y: %f", y);
return 0;
}
Bài 2
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
float cm, ft, in;
scanf("%f",&cm);
in = cm/2.54;
printf("\n%f centimeters tuong duong %.1f inches", cm, in);
ft = in/12;
printf("\n%f centimeters tuong duong %.1f feet", cm, ft);
return 0;
}
Bài 3
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int iResult, a=10, b=8, c=6, d=5, e=2;
iResult = a-b-c-d;
printf("\niResult = a-b-c-d = %d", iResult);
iResult = a-b+c-d;
printf("\niResult = a-b+c-d = %d", iResult);
iResult = a+b/c/d;
printf("\niResult = a+b/c/d = %d", iResult);
iResult = a+b/c*d;
printf("\niResult = a+b/c*d = %d", iResult);
iResult = a/b*c*d;
printf("\niResult = a/b*c*d = %d", iResult);
iResult = a%b/c*d;
printf("\niResult = a%%b/c*d = %d", iResult);
iResult = a%b%c%d;
printf("\niResult = a%%b%%c%%d = %d", iResult);
iResult = a-(b-c)-d;
printf("\niResult = a-(b-c)-d = %d", iResult);
iResult = (a-(b-c))-d;
printf("\niResult = (a-(b-c))-d = %d", iResult);
iResult = a-((b-c)-d);
printf("\niResult = a-((b-c)-d) = %d", iResult);
iResult = a% (b%c)*d*e;
printf("\niResult = a% (b%%c)*d*e = %d", iResult);
iResult = a+ (b-c)*d-e;
printf("\niResult = a+ (b-c)*d-e = %d", iResult);
iResult = (a+b)*c+d*e;
printf("\niResult = (a+b)*c+d*e = %d", iResult);
iResult = (a+b)* (c/d)%e;
printf("\niResult = (a+b)* (c/d)%%e = %d", iResult);
return 0;
}
0 nhận xét