Thread: Nill

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Nill

    Hi guys, I just want to verify that '\0' denotes a character with value zero in c or not? for example if I have
    Code:
     char* a="Hello"
    then I can write like this:
    Code:
     
    if (a[strlen(a)]==0) 
    printf("stop, you arrived to the final of the string");
    ???


    thanks !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I just want to verify that '\0' denotes a character with value zero in c or not?
    Yes.

    Quote Originally Posted by RyanC
    for example if I have
    That's a bad example. Use const char* to point to string literals.

    Quote Originally Posted by RyanC
    then I can write like this:
    Yes, but don't. Use '\0' as it is the appropriate integer character constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    [...]
    Quote Originally Posted by RyanC View Post

    Code:
     
    if (a[strlen(a)]==0) 
    printf("stop, you arrived to the final of the string");
    [...]

    This is unintuitive. Later you or somebody else may argue about such codes. In C use char array (or pointer) to point to current char. Last char in a string would be null char : '\0' .

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Yes.
    Thanks, but I would like to ask that how is something char also presented in integer?! and it's the same to say '\0' in char or 0 integer....?!

    to clear more ... in generally for being the "same" must be the operand/data on the same type, we can't say something in integer is equal to something in char .. don't make sense, am I wrong?!

    thanks!!
    Last edited by RyanC; 12-31-2018 at 08:37 AM.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by RyanC View Post
    Thanks, but I would like to ask that how is something char also presented in integer?! and it's the same to say '\0' in char or 0 integer....?!

    to clear more ... in generally for being the "same" must be the operand/data on the same type, we can't say something in integer is equal to something in char .. don't make sense, am I wrong?!

    thanks!!
    Zero is zero, whether it's an int or a char.

    By the way, a[strlen(a)] is always the null character ('\0') because strlen stops when it reaches the first null character in the string.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I would like to ask that how is something char also presented in integer?! and it's the same to say '\0' in char or 0 integer....?!
    Oh cool, I haven't had the pleasure of doing a C standard verbatim vomit in awhile, and here's my chance:
    Quote Originally Posted by C11 Clause 6.2.5 Paragraphs 3-6, 15, 17
    An object declared as type char is large enough to store any member of the basic execution character set. If a member of the basic execution character set is stored in a char object, its value is guaranteed to be nonnegative. If any other character is stored in a char object, the resulting value is implementation-defined but shall be within the range of values that can be represented in that type.

    There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types. 38) The standard and extended signed integer types are collectively called signed integer types.

    An object declared as type signed char occupies the same amount of storage as a ‘‘plain’’ char object. A ‘‘plain’’ int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).

    For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements. The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types. The unsigned integer types that correspond to the extended signed integer types are the extended unsigned integer types. The standard and extended unsigned integer types are collectively called unsigned integer types.

    The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

    The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types. The integer and real floating types are collectively called real types.
    Quote Originally Posted by C11 Clause 6.4.4.4 Paragraphs 2a, 5, 10a,d, 12
    An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

    The octal digits that follow the backslash in an octal escape sequence are taken to be part of the construction of a single character for an integer character constant or of a single wide character for a wide character constant. The numerical value of the octal integer so formed specifies the value of the desired character or wide character.

    An integer character constant has type int. (...) If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

    EXAMPLE 1 The construction '\0' is commonly used to represent the null character.
    That was fun!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread