This is the 9th day of my participation in the August Wen Challenge.More challenges in August

directory

  • Char character strings
  • 2. Character and string differences
    • 1. The value ranges are the same
    • 2. A string consists of multiple characters
    • 3. Use printf for strings and characters
  • Char Traversal of the string
  • Four. Guess you like it

C/C++ learning Directory >> C language basics

Char character strings

In C, in addition to int, float/double, bool… . Wait, char strings are also a very important data type;

String: String constants are usually represented by a string of characters enclosed in double quotation marks. By default, strings end with an escape character \0. String constants cannot be modified, for example:

"Hello!" , "\ aWarning! \a", "123abc\n", ""Copy the code

2. Character and string differences

1. The value ranges are the same

There are two types of characters, one is unsigned charactersunsignedcharOne is a signed characterchar, they have different value ranges:

Unsigned char Value range: 0 to 255 Signed char Value range: -127 to 127Copy the code

2. A string consists of multiple characters

Both strings and characters can be represented by char. Note the difference:

// string char* p = "123"; // String with double quotation marks // char p = '1'; Char p = '2'; Char p = '3'; // Characters use single quotation marksCopy the code

3. Use printf for strings and characters

Strings and characters need to be used differentlyA placeholder: strings use %s as placeholders and characters use %c as placeholders;

printf("%c ", 'A');
printf("%s ", "A");
Copy the code

Although it’s the same thingprintffunctionOutput A, but “A” is A string and “A” is A single character;

Char Traversal of the string

The ** string is made up of multiple characters, ending with the escape character \0 by default. We can loop through each character in a string with a break, for example:

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language char string // @time :2021/06/01 08:00 // @motto: Short step without a thousand miles, not small streams without rivers and oceans, the wonderful life of the program needs to be accumulated unrelentingly! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> void main() { char* p = "www.codersrc.com"; Printf (" string p: %s\n", p); While (1) {if (* p = = '\ 0') / / '\ 0' said string at the end, out of circulation break; printf("%c ", *p); // the current character p++; // String address offset +1} system("pause"); } /* Output: string p: www.codersrc.com www. C odersrc.com Press any key to continue... */Copy the code

Four. Guess you like it

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. Set the Visual Studio font/background/line number
  6. C format controller/placeholder
  7. C language logic operator
  8. C language ternary operator
  9. C language comma expression
  10. C (++ I/I ++)
  11. C language for loop
  12. C language break and continue
  13. C while loop
  14. C does while and while loops
  15. C switch statement
  16. C language GOto statement
  17. C char Character string

C char string

This article is published by the blog – Ape Say Programming Ape Say programming!