Difference between null and 0 in C Language

Source: Internet
Author: User
Tags define null

Reprinted from: http://www.cnblogs.com/youxin/archive/2012/03/27/2420023.html

Let's take a look at the output of the following code:

#include<stdo.h>
int main(){    int *p=NULL;    printf("%s",p); }

Output (null). One-Step debugging shows that int * P = NULL is executed, and P is 0x00000000. It can be seen that null is 0 in the actual underlying call,

In C,

The values of null and 0 are the same,Is for the purpose and is easy to identify, null is used for pointers and objects, 0 is used as a numerical value.


For the end of a string,Use '\ 0', and its value is also 0,But let people know at a glance that this is the end of a string, not a pointer or a common value.

In different systems,

Null is not always the same as 0. null only indicates a null value, that is, pointing to an unused address. In most systems, 0 is used as the unused address, so there is a definition like this.

# Define null 0

However, this is not always the case. Some systems do not use 0 addresses as null, but use other addresses. Therefore, do not equivalent null to 0, especially in some cross-platform code, this will bring you a disaster.

See the following explanation:

Ask 0 '0' \ 0' "\ 0"

To me, when doing C/C ++:

0 wocould digit zero, that is, a numerical value.

'0' cocould be the character capital Oh or the character Zero. For example: Char word [10] = "Oxford"; char number [10] = "01234 ";

Depending on typeface used 'O' may look exactly like '0' making it difficult to tell them apart out of context.

'\ 0' is the null character used to terminate strings in C/C ++.

"\ 0" is an empty string.

Baidu encyclopedia;

\ 0 is the end sign of the string in C ++, which is stored at the end of the string. For example, char Cha [5] indicates a string that can contain five characters. Although it is not counted as the string length, however, the memory space is occupied, while a Chinese character is generally expressed in two bytes. in C/C ++, if an array Cha [5], there are 5 variables, which are
Cha [0], Cha [1], Cha [2], Cha [3], Cha [4], therefore, Cha [5] can contain 5 letters or 2 Chinese characters (one Chinese Character occupies 2 bytes and one letter occupies 1 byte ), cha [5] occupies 5 bytes of memory space.

For example:

# Include <stdio. h> int main () {char a [5]; A [0] = 'a'; A [1] = 'B'; A [2] = 'C '; A [3] = 'D'; // STR [4] = '\ 0'; // output abcd str [4] = 'E '; /* in this way, the output is ABCDE and a bunch of garbled characters, or even jumps out of the system error because there is no string Terminator */printf ("% s \ n", );}

Related Article

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.