C-language leak checking--single and double quotes

Source: Internet
Author: User

There has been some confusion about single and double quotes, and this section discusses the issue of single and double quotation marks.


First, the basic knowledge

1. Single quotation marks in C language are used to denote character literals

2. Double quotation marks in C language are used to denote string literals


For example: ' A ' represents the character literal, which accounts for 1 bytes in memory, ' a ' +1 means ' a ' of the ASCII code plus 1, the result is ' B '

"A" represents the string literal, which accounts for 2 bytes in memory, where the first byte is "a", the second byte is Terminator ' + "," a "+1 means the pointer operation, and the result points to" a "Terminator '-".


Second, code Analysis

There is a code like the following

#include <stdio.h>int main () {char* P1 = 1;    char* P2 = ' 1 ';    char* P3 = "1";        printf ("%s,%s,%s", p1, p2, p3);    printf (' \ n ');        printf ("\ n"); return 0;}

So is this code legal?

Under Linux, the results are as follows after the compilation is run

Why do you report a mistake? Let's do the analysis below.

First, we look at

char* P1 = 1;    char* P2 = ' 1 '; char* P3 = "1";

Char* is a pointer, according to the concept of pointers, the corresponding pointer variable stored should be an address, then the memory distribution of the above code can be understood as follows

In other words, p1 and P2 are at the low address of memory. One thing to remember here is that the memory address of the program must be less than 0x08048000, or it will produce a segment error.



Third, expansion

#include <stdio.h>int main () {char c = ""; while ((c = = "\ T") | | (c = = "") | |    (c = = "\ n"))    {scanf ("%c", &c); } return 0;}

This program, running in Linux results in the following

You can see that the program is running, it is finished, and there is no need to enter the situation we imagined. Why is that?

Analyzed as follows

char c = "";

This code is equivalent to

Char c = "string";

1, the memory address of the compiled string "string" is assigned to the variable C

2, memory address occupies 4 bytes, and variable c only occupies 1 bytes

3, due to different types, after the assignment of a truncation


Therefore, it is necessary to change all the double quotation marks to single quotation marks to achieve this.



Iv. Summary

1. Single characters enclosed in single quotes represent integers

2. The characters enclosed in double quotes represent the character pointer.

3, C compiler accepts the character and the string comparison, does not have any meaning

4.C compiler allows string to assign value to character variable, only get error


C-language leak checking--single and double quotes

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.