Thread: output

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    output

    Halo to all,
    I think I've increased my frequency of asking questions. But when I see a problem like this I've no choice but to ask.
    So, here's a piece of code.
    Code:
    #include<stdio.h>
    int main(void)
    {
    char p[] = "\123\xAB" ;
    printf("%d",sizeof(p));
    }
    How does the output to this question comes out to be 3?
    On a similar note, I just changed the code to
    Code:
    #include<stdio.h>
    int main(void)
    {
    char p[] = "\0" ;
    printf("%d",sizeof(p));
    }
    How does the output of this one is 2?
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    '\123' is legal as a character literal as is '\xAB' or the familiar '\0'. There is nothing unusual about the size since you are using escaped characters. sizeof is also not the same as strlen and the actual terminating zero will be counted.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> "\123\xAB"

    The '\' character is an escape sequence. It should only be followed by certain specific values (eg: "\n"). In your example, '\1' is meaningless, so the compiler is free to generates whatever it pleases. In the second case, you're instructing the compiler to insert a 0 into the buffer, which is already going to be null terminated so the compiler generates 2 total.

    EDIT:
    >> '\123' is legal as a character literal as is '\xAB' or the familiar '\0'.

    Ok, I've never seen that before. My mistake.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Then what does '\123' signify? I mean for what it is used. Is it a special character? Also Isn't this '\0' a NULL character which means the string should terminate here and the size should be 1byte.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Then what does '\123' signify?

    I looked it up, and it's used for octal numbers, so that's equivalent to '\x53'.

    >> Also Isn't this '\0' a NULL character which means the string should terminate here and the size should be 1byte.

    No, it inserts however many you specify and then terminates the whole thing with another zero for good measure. In other words, "\0\0\0\0" would be 5 bytes total.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And just for good measure, the standard has very little to say on what is or is not a char literal:

    6.4.4.4p10: "The value of an integer character constant
    containing more than one character (e.g., 'ab'), or containing a
    character or escape sequence that does not map to a single-byte
    execution character, is implementation-defined."

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes, but '\123' does map to a single-byte character. So no issue here.

    char p[] = "\123\xAB" ;

    means... assign the first three elements as follows:

    p[0] = 83; // octal 0123 or hex 0x53
    p[1] = 171; // octal 0253 or hex 0xAB
    p[2] = 0;

    Length of array is three bytes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM