Test the C-style string Terminator and output the characters using ASCII code.

Source: Internet
Author: User

 

// Szstring. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include <iostream>
# Include <cassert>
Using namespace STD;

Int _ tmain (INT argc, _ tchar * argv [])
{
Char const * pstr = "10 45"; // in order to test the effect of a space, replace 3 with a space.
Char const * pstr2 = pstr;
Cout <"strlen (pstr) =" <strlen (pstr) <Endl;
Assert (strlen (pstr) = 5 );

/*
For (; * pstr! = '\ N'; pstr ++) // The end character is incorrect! Prints this is a string and other content of the internal code segment.
{
Cout <* pstr;
}
*/

// Correct syntax: All the characters to be pointed to except the null characters are true values.
While (* pstr)
{
Cout <* pstr;
Pstr ++;
}
Cout <"*** finish" <Endl; // to display the end of the string, add the * sign. The result shows 10 45 **** finish

// Another incorrect method
While (* pstr2 ++)
{
Cout <* pstr2; // insert a breakpoint here for debugging and observe pointer changes * pstr2 => 0 ('\ 060')> ''> 4> 5> 0, last pstr2 = ""
}
Cout <"*** finish" <Endl; // The result is displayed as 0 45 *** finish (the empty character value is ASCII 0 (it is '\ 0' rather than' \ 060 ') and occupies a blank space)
Cout <'\ 077' <0 <'\ 0' <' \ 60' <Endl; // The ASCII code of the character 0 is 48, and the octal value is 060. Output :? 0 0 line feed
: System ("pause ");
Return 0;
}

/* Note: output characters using ASCII codes directly
For example, check the ASCII code table and find the question mark character (?) The ASCII value of is 63,
Then we can convert it into an octal value: 77, and then use '\ 77' To Represent '? '. Because it is octal,
Therefore, it should be written as '\ 077', but because C and C ++ do not allow the use of a slash plus a 10-digit number to represent characters,
Therefore, the value 0 can be left empty.

With ultraedit32, you can press Alt + press enter at the cursor to view the code value of the current character.
*/

 

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.