Transfer characters----sizeof & strlen

Source: Internet
Author: User
Tags array definition

When the array size is defined:

sizeof is an operator that represents the amount of space allocated at compile time, that is, the size of the array definition, char t[20] = "SFA". sizeof:20; Strlen:3.
When the array size is undefined :

sizeof is determined based on the actual number of digits, Note that the string hidden in the .
Char s[] = "a\128\\\tcb\xdg\n";
sizeof calculates the size of S, 11 bits: a \12 8 \ t \ c b \xd g \ n
strlen when calculating the size of S, 10 bits:strlen ends when calculating the string length, but the size calculation does not include

Char a[] = "a0\0a0\0";
sizeof (a) size is 7, i.e.: a 0/a 0
Strlen (a) size is 2

Char s[] = "a\128\\\tcb\xdg\n"; Print character array s: escape character:
Any character represented by the \yyy:1~3 Octet, where the range of Y is 0~7
\xyy:1~2 a hexadecimal representation of any character, where the range of Y is 0~f

 for (int0sizeof(s); i++) {     printf ("%c", S[i]);} A8\    CBG

\12:2 bit octal number, decimal 10, control character LF, for line break, to start at the next line

\ \: Indicates \

\ t: Represents a horizontal tab, which is a tab

\xd:1 digit hexadecimal number, decimal 13, control character CR, indicates carriage return

Note the octal and hexadecimal questions of the translation word:

Char s[] = "A\12345b\x1221g\xay";

Octal up to 3 bits, that is, \123, when the octal number is converted to decimal than the ASCII Code table maximum of 127, is a garbled.

Hexadecimal is selected up to two bits, but the last 2 bits of the integer after intercepting \x are processed, the preceding bits are discarded, that is, the truncation, that is, the discard 12, the hexadecimal \x21, the decimal 33 represents!

So in the length calculation:

sizeof is 10 bits, namely: a \123 4 5 b \x21 g \xa y

Strlen is 9 bits, namely: a \123 4 5 b \x21 g \xa y

Transfer characters----sizeof & strlen

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.