Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Boolean type of C language

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the Boolean type of C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Boolean type of C language.

The C language standard (C89) does not define Boolean types. If you use true and false, the following errors will occur:

Infinite.c:5:12: error: use of undeclared identifier 'true' while (true) {1 error generated.make: * [infinite] Error 1

We can use the macro definition of C language to set up:

/ / Macro defines the Boolean type # define BOOL int#define TRUE 1#define FALSE 0 / / defines a Boolean variable BOOL flag = FALSE

It can also be defined by enumerating classes:

Typedef enum {true=1, false=0} bool

Example

# the value of include// computing n defines int main (void) {int n = 10 in main; / / calculates the superposition multiplier int sum = 1; / / used to store the result of superposition typedef enum {true=1, false=0} bool; bool flag = false; / / superposition tag int num = n / / number of cycles while (! flag) {sum = sum * (num--); / / end loop if when num=1 (num= = 1) {flag = true;}} printf ("% d's superposition value is% d\ n", n, sum); return 0;}

The output is as follows:

The multiplication of 10 is 3628800.

C99 also provides a header file that defines bool for _ Bool,true for 1pm false for 0. As long as you import stdbool.h, you can easily manipulate Boolean types.

Example

/ / Import stdbool.h to calculate the value of n using the Boolean type # include#include// define int main (void) {int n = 10; / / calculate the stack multiplier int sum = 1 in main; / / store the result of superposition bool flag = false; / / superposition tag int num = n; / / number of cycles while (! flag) {sum = sum * (num--) / / end loop if (num= = 1) {flag = true;}} printf when num=1 (superposition value of "% d is% d\ n", n, sum); return 0;}

The output is as follows:

The multiplication value of 10 is 3628800. I believe you have a deeper understanding of "how to understand the Boolean type of C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 206

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report