String knowledge note in C

Source: Internet
Author: User

String knowledge note in C
1: In C, we say "H" is a string with two bytes in total, followed by a '\ 0' pseudo zero Terminator.

'H' is a number. Verify with the following program.

#include <stdio.h>int main(void){printf("%d %d\n",sizeof("c"),sizeof('s'));}

2: String creation process

#include <stdio.h>int main(void){char * p = "hello world!";printf("%s\n", p);}

Here, Why can a string be assigned to a char * type pointer variable?
1: When we write hello, it tells the compiler to store 6 bytes in the read-only data segment.
2: the value of the "hello" expression represents the address of the first character of the string.

# Include <stdio. h> int main (void) {char * p = "world"; // you can see from the code that the string represents an address. Printf ("% s \ n", p); printf ("% c \ n", * ("hello"); return 0 ;}

// When the output data exceeds the threshold, the output data increases. # include <stdio. h> void func (void) {int * p = "hello! "; Write (1, p, 10);} int main (void) {int * q =" hello world "; func (); return 0 ;}



3: Passing parameters in the string:

#include <stdio.h>int main(void){char * p = "hello word! %d\n";printf(p,5);return 0;}
4: Modify the string content

# Include <stdio. h> int main (void) {char * p = "hello word! \ N "; p [0] = 'H'; // This will cause compilation problems, because the content of the read-only data segment cannot be changed to printf (" % c \ n ", p [0]); // printf (p, 5); return 0 ;}

// If we want to modify the content in the read-only string, we need to put the data in an array, which is equivalent to copying the content in the read-only data segment.


For example

#include <stdio.h>#include <string.h>int main(void){        char * p = "hello world!";        char ch[64];        strcpy(ch,p);        ch[0]='H';        printf("%s\n",ch);        return 0;}



5: Evaluate the valid byte length of a string:

# Include <stdio. h> # include <string. h> int main (void) {char * p = "hello"; // p [0] = 'H'; // printf ("% c \ n ", p [0]); // printf (p, 5); char a [] = "hello"; // strlen () this function is used to evaluate the valid byte length of a string. printf ("% ld \ n", strlen (a); return 0;} is determined based on the pseudo-zero end time ;}



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.