Programmer --- C language details 26 (boolean type, continue details in C language, sizeof example, strlen example)

Source: Internet
Author: User

Programmer --- C language details 26 (boolean type, continue details in C language, sizeof example, strlen example)
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  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

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


#include
int b [100];
void fun (int b [100])
{
printf ("in fun sizeof (b) =% d \ n", sizeof (b)); // I feel that the array in the function is processed as a pointer
}
int main ()
{
char * p = NULL;
printf ("sizeof (p) =% d \ n", sizeof (p));
printf ("sizeof (* p) =% d \ n", sizeof (* p));
Ranch
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 (& a));
printf ("sizeof (& a [0]) =% d \ n", sizeof (& a [0]));
printf ("----------------------- \ n");
fun (b);
return 0;
}
Output:



Iv. Example of strlen


#include
#include
#include
#include
/ *
Problem
How are -0 and +0 stored in memory?
2.int i = -20;
unsigned j = 10;
What is the value of i + j? why?
3. What's wrong with the following code?
unsigned i;
for (i = 9; i> = 0; i--)
{
printf ("% u \ n", i);
}
* /
int main ()
{
bool bool2 = 0; / * C language bool type is only available in C99, if the header file stdbool.h is not included, using bool directly will report an error * /
_Bool bool1 = 1; / * Use _Bool directly without including stdbool.h * /
char a [280];
char * str = "12340987 \ 0 56";
char test [] = "12340987 56";
int i;
printf ("strlen (a) =% d \ n", strlen (a));
for (i = 0; i <280; i ++)
{
a [i] = -1-i; // a [256] = 0
// printf ("a [% d] =% d \ n", i, a [i]);
}

printf ("strlen (a) =% d \ n", strlen (a)); // why is 255
printf ("strlen (str) =% d \ n", strlen (str));
printf ("strlen (test) =% d \ n", strlen (test));
Ranch
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.