A khác b viết trong c như thế nào

Trong quá trình học về C/C++ một số bạn có hỏi mình là tràn số là gì và cách khác phục nó như thế nào. Hôm nay, mình sẽ cùng các bạn xử lý tràn số trong C/C++ nhé.

1. Khái niệm và ví dụ tràn số

Tràn số hiểu đơn giản là khi bạn cố gắng gán giá trị cho một biến nào đó mà giá trị đó nằm ngoài phạm vi của kiểu dữ liệu biến đó. Ví dụ như dưới đây:


# include<iostream>
using namespace std;
int main(){
  int a = 2147483648;
  cout<<a;
}

Kiểu int trong c++ sẽ nhận giá trị -2147483648 ... 2147483647.

Như hình trên thì chúng ta đã gán cho biến n một giá trị nằm ngoài kiểu int nên dẫn tới lỗi tràn số, biến n không thể lưu được giá trị đó, chương trình trên nếu chạy sẽ bị ra kết quả: -2147483648

Mệnh đề

// Program to display a number if it is negative

# include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }
    printf("Done.");
}

0 trong C được sử dụng để kiểm tra một biểu thức điều kiện (condition) nào đó có đúng hay không, nếu đúng thì thực thi những câu lệnh bên trong khối lệnh

// Program to display a number if it is negative

# include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }
    printf("Done.");
}

1 và ngược lại nếu sai thì nó sẽ bỏ qua những câu lệnh đó. Có ba dạng của câu lệnh

// Program to display a number if it is negative

# include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }
    printf("Done.");
}

1 trong C.

  • ` // Program to display a number if it is negative

include <stdio.h>

int main() {

int number;  
printf("Enter an integer: ");  
scanf("%d", &number);  
// true if number is less than 0  
if (number < 0) {  
    printf("You entered %d.\n", number);  
}  
printf("Done.");  
}
1
  • ` // Program to display a number if it is negative
# include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // true if number is less than 0 if (number < 0) { printf("You entered %d.\n", number); } printf("Done."); }
0

  • ` // Program to display a number if it is negative

include <stdio.h>

int main() {

int number;  
printf("Enter an integer: ");  
scanf("%d", &number);  
// true if number is less than 0  
if (number < 0) {  
    printf("You entered %d.\n", number);  
}  
printf("Done.");  
}
5

Trước khi tìm hiểu luôn về 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

0 thì chúng ta cần tìm hiểu và ghi nhớ một số toán tử tiếp theo vì những toán tử này được dùng rất nhiều trong condition ở mệnh đề 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1.

Bạn có thể xem lại bài viết về toán tử tại đây.

## 

Một toán tử quan hệ kiểm tra mối quan hệ giữa hai toán hạng. Nếu quan hệ là 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8, nó trả về 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

9; nếu quan hệ là 
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0, nó trả về giá trị 
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

1.

Vì vậy, hãy nhớ trong lập trình C - giá trị 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

9 đại diện cho 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8, giá trị 
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

1 đại diện cho 
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0.

Toán tửMô tảVí dụ (a = 3, b = 5)Kết quả==Bằnga == b
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0!=Kháca != b
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8>Lớn hơna > b
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0<Nhỏ hơna < b
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8>=Lớn hơn hoặc bằnga >= b
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0<=Nhỏ hơn hoặc bằnga <= b
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8

## 

Một biểu thức chứa toán tử logic trả về 
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

1 hoặc 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

9 tùy thuộc vào kết quả biểu thức đúng hay sai.

Toán tửMô tảVí dụ (a = 3, b = 5)Kết quả&&Toán tử AND - 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8 nếu tất cả các toán hạng là 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8(a == 3 && b < 5)
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0| |Toán tử OR - 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8 nếu một toán hạng là 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8(a == 3 | | b < 5)
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8!Toán tử NOT - 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

8 nếu toán hạng bằng 
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0!(a == 3)
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

0

## 

### 

  • Câu lệnh điều kiện "if" trong ngôn ngữ lập trình C cho phép bạn kiểm tra một điều kiện nào đó và thực hiện các hành động khác nhau dựa trên kết quả của điều kiện đó. Hãy bắt đầu với cú pháp cơ bản:
if (condition) {
// khối lệnh này được thực thi nếu condition = true
}

  • `
# include <stdio.h> int main() { int age; printf("Enter your age:"); scanf("%d", &age); if (age >= 18) printf("You are eligible for voting."); else printf("You are not eligible for voting."); }
3 là biểu thức logic bạn muốn kiểm tra.

  • Nếu `

include <stdio.h>

int main() {

int age;  
printf("Enter your age:");  
scanf("%d", &age);  
if (age >= 18)  
    printf("You are eligible for voting.");  
else  
    printf("You are not eligible for voting.");  
}
3 là đúng, các câu lệnh bên trong khối lệnh { } sẽ được thực thi. Nếu  

include <stdio.h>

int main() {

int age;  
printf("Enter your age:");  
scanf("%d", &age);  
if (age >= 18)  
    printf("You are eligible for voting.");  
else  
    printf("You are not eligible for voting.");  
}
3 là sai, các câu lệnh này sẽ được bỏ qua.
Tại vị trí # include <stdio.h> int main() { int age; printf("Enter your age:"); scanf("%d", &age); if (age >= 18) printf("You are eligible for voting."); else printf("You are not eligible for voting."); } 3, chúng ta sẽ viết một điều kiện để kiểm tra sử dụng các toán tử quan hệ đã được hướng dẫn ở trên.
Đây là một ví dụ:
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

Khi bạn nhập một số âm:

Enter an integer: -5 You entered -5\. Done.

Khi bạn nhập một số dương:

Enter an integer: 5 Done.

  • Trong ví dụ này, chúng ta kiểm tra nếu số bạn nhập vào nhỏ hơn 0, thì chúng ta sẽ thông báo rằng bạn đã nhập số đó (You entered -5). Ngay cả khi điều kiện không đúng, chương trình sẽ tiếp tục chạy và hiển thị thông báo "Done"
###
  • Đôi khi, chúng ta cần thực hiện các hành động khác nếu điều kiện không đúng. Đó là lúc chúng ta sử dụng câu lệnh "else."
if (condition) {
// khối lệnh này được thực thi nếu condition = true
} else {
// khối lệnh này được thực thi nếu condition = false
}

  • Nếu `
# include <stdio.h> int main() { int age; printf("Enter your age:"); scanf("%d", &age); if (age >= 18) printf("You are eligible for voting."); else printf("You are not eligible for voting."); }
3 trả về
// Program to display a number if it is negative  

# include <stdio.h>  
int main() {  
    int number;  
    printf("Enter an integer: ");  
    scanf("%d", &number);  
    // true if number is less than 0  
    if (number < 0) {  
        printf("You entered %d.\n", number);  
    }  
    printf("Done.");  
}  
8 thì các câu lệnh bên trong phần thân của
// Program to display a number if it is negative  

# include <stdio.h>  
int main() {  
    int number;  
    printf("Enter an integer: ");  
    scanf("%d", &number);  
    // true if number is less than 0  
    if (number < 0) {  
        printf("You entered %d.\n", number);  
    }  
    printf("Done.");  
}  
1 được thực thi và các câu lệnh bên trong phần thân của
if (condition1) {  
    // khối lệnh này được thực thi nếu condition1 = true  
} else if (condition2) {  
    // khối lệnh này được thực thi nếu condition1 = false và condition2 = true  
    ...  
} else {  
    // khối lệnh này được thực thi nếu nếu tất cả những condition trên = false  
}  
0 bị bỏ qua.

  • Nếu `

include <stdio.h>

int main() {

int age;  
printf("Enter your age:");  
scanf("%d", &age);  
if (age >= 18)  
    printf("You are eligible for voting.");  
else  
    printf("You are not eligible for voting.");  
}
3 trả về  
if (condition) {
// khối lệnh này được thực thi nếu condition = true  
} else {
// khối lệnh này được thực thi nếu condition = false  
}
0 thì các câu lệnh bên trong phần thân của  
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;  
printf("Enter an integer: ");  
scanf("%d", &number);  
// true if number is less than 0  
if (number < 0) {  
    printf("You entered %d.\n", number);  
}  
printf("Done.");  
}
1 bị bỏ qua và các câu lệnh trong  
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true  
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true  
...  
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false  
}
0 được thực thi.

Đây là một ví dụ:

include <stdio.h>

int main() {

int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
// Câu lệnh này sẽ được thực thi nếu điều kiện trên (age >= 18) trả về true
printf("You are eligible for voting.");
} else {
// Câu lệnh này sẽ được thực thi nếu điều kiện trên (age >= 18) trả về false
printf("You are not eligible for voting.");
}
}

Khi bạn nhập số tuổi thỏa mãn điều kiện lớn hơn hoặc bằng 18:

Enter your age: 22 You are eligible for voting.

Khi bạn nhập số tuổi không thỏa mãn điều kiện lớn hơn hoặc bằng 18:

Enter your age: 14 You are not eligible for voting.

Chú ý:  
Nếu chỉ có một câu lệnh xuất hiện trong thân 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 hoặc 
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true
...
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false
}

0 thì bạn không cần phải sử dụng cặp dấu 
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true
...
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false
}

7\. Ví dụ, chương trình trên có thể được viết lại như sau:

include <stdio.h>

int main() {

int age;
printf("Enter your age:");
scanf("%d", &age);
if (age >= 18)
    printf("You are eligible for voting.");
else
    printf("You are not eligible for voting.");
}

### 

Câu lệnh 
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true
...
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false
}

8 rất hữu ích khi bạn cần kiểm tra nhiều điều kiện trong chương trình, có thể tránh việc lồng các khối 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

0 bằng cách sử dụng câu lệnh 
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true
...
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false
}

8.

Cú pháp của 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

5:
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true
...
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false
}

Và đây là một ví dụ minh họa:

include <stdio.h>

int main() { int var1, var2; printf("Input the value of var1: "); scanf("%d", &var1); printf("Input the value of var2: "); scanf("%d",&var2); if (var1 == var2) {

printf("\nvar1 is equal to var2.\n");
} else if (var1 > var2) {
printf("\nvar1 is greater than var2.\n");
} else {
printf("\nvar1 is less than var2.");
} }

Input the value of var1: 9 Input the value of var2: 8

var1 is greater than var2.

## 

Khi một câu lệnh 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 hoặc 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

0 xuất hiện bên trong phần thân của một 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 hoặc 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

0 khác thì đây được gọi là mệnh đề 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 lồng nhau. Các câu lệnh 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 bên trong được thực thi dựa trên các câu lệnh 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 bên ngoài.

Cú pháp:
if (condition 1) {
// if-else bên trong mệnh đề if
if (condition 2) {
   ...
} else {
   ...
}
} else {
if (condition 3) {
    ...
}
}

Cùng xem ví dụ sau:

include <stdio.h>

int main() { int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d", &var2); if (var1 != var2) {

printf("\nvar1 is not equal to var2.\n");
if (var1 > var2) {
    printf("var1 is greater than var2.\n");
} else {
    printf("var1 is less than var2.\n");
}
} else {
printf("var1 is equal to var2.\n");
} }

Input the value of var1: 5 Input the value of var2: 2

var1 is not equal to var2\. var1 is greater than var2.

## 

Trong câu lệnh 
// Program to display a number if it is negative

include <stdio.h>

int main() {

int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
    printf("You entered %d.\n", number);
}
printf("Done.");
}

1 hoặc 
if (condition1) {
// khối lệnh này được thực thi nếu condition1 = true
} else if (condition2) {
// khối lệnh này được thực thi nếu condition1 = false và condition2 = true
...
} else {
// khối lệnh này được thực thi nếu nếu tất cả những condition trên = false
}

8 chúng ta cũng có thể dùng 
if (condition 1) {
// if-else bên trong mệnh đề if
if (condition 2) {
   ...
} else {
   ...
}
} else {
if (condition 3) {
    ...
}
} `

1 để kết hợp nhiều conditions lại với nhau để xử lý nhiều điều kiện một lúc.