Programmer --- C language details 26 (Boolean Type in C language, continue details, sizeof example, strlen example), --- cstrlen

Source: Internet
Author: User

Programmer --- C language details 26 (Boolean Type in C language, continue details, sizeof example, strlen example), --- cstrlen
Main Content: boolean type, continue details in C language, sizeof example, strlen example
I. boolean type

Many people may not know that the C language already has the Boolean Type: Starting from the C99 standard, the type name is "_ Bool"

Before the C99 standard, we often mimic and define the boolean type. There are several common methods:

1. method 1

#define TURE 1#define FALSE 0

2. method 2

typedef enum {false, true} bool;

3. Method 3

typedef int bool

Idle int is a waste of memory and is used by memory-sensitive programs.

typedef char bool

The header file added in the C99 standard introduces the bool type, which is compatible with the bool type in C ++. The header file is stdbool. h, and its source code is as follows:

#ifndef _STDBOOL_H #define _STDBOOL_H  #ifndef __cplusplus  #define bool    _Bool #define true    1 #define false   0  #else /* __cplusplus */  /* Supporting <stdbool.h> in C++ is a GCC extension.  */ #define _Bool   bool #define bool    bool #define false   false #define true    true  #endif /* __cplusplus */  /* Signal that all the definitions are present.  */ #define __bool_true_false_are_defined 1#endif /* stdbool.h */ 
Ii. continue

The detail of continue is used in the loop. Otherwise, an error is reported.

# Include <stdio. h> int main () {int a = 1; switch (a) {case 1: printf ("1 \ n"); continue; // continue must be used in the break loop; case 2: printf ("2 \ n"); default: break;} return 0 ;}
Iii. sizeof example

# Include <stdio. h> int B [100]; void fun (int B [100]) {printf ("in fun sizeof (B) = % d \ n", sizeof (B )); // int main () {char * p = NULL; printf ("sizeof (p) = % d \ n ", sizeof (p); printf ("sizeof (* p) = % d \ n", sizeof (* p); printf ("--------------------- \ n "); char a [100]; printf ("sizeof (a) = % d \ n", sizeof (a); printf ("sizeof (a [100]) = % d \ n ", sizeof (a [100]); printf (" sizeof (& a) = % d \ n ", sizeof (& )); printf ("sizeof (& a [0]) = % d \ n", sizeof (& a [0]); printf ("--------------------- \ n "); fun (B); return 0 ;}
Output:

Iv. Example of strlen

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <stdbool. h>/* Question 1: how to store-0 and + 0 in the memory? 2. int I =-20; unsigned j = 10; what is the value of I + j? Why? 3. What is the problem with the following code? Unsigned I; for (I = 9; I> = 0; I --) {printf ("% u \ n", I) ;}*/int main () {bool bool2 = 0;/* C bool type is available only in C99, if it does not contain the header file stdbool. h. If you use bool directly, an error will be reported */_ Bool bool1 = 1;/* You can directly use _ Bool instead of stdbool. h */char a [280]; char * str = "12340987 \ 0 56"; char test [] = "12340987 56"; int I; printf ("strlen () = % d \ n ", strlen (a); for (I = 0; I <280; I ++) {a [I] =-1-i; // a [1, 256] = 0 // printf ("a [% d] = % d \ n", I, a [I]);} printf ("strlen (a) = % d \ n", strlen (a); // Why is it 255 printf ("strlen (str) = % d \ n ", strlen (str); printf ("strlen (test) = % d \ n", strlen (test); char s = '\ 0 '; printf ("\ 0 = % d \ n", s); return 0 ;}
Output:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.