Thread: Doesn't print out?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Question Doesn't print out?

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char str2[2], str1[2]= "a";
    int n = 127;

    str2[1] = 0;
    str2[0] = n;

    strcat(str1 , str2 );

    if( str1[1] == n )
    printf( "%s\n" , str1 );

    return 0;
    }

    I don't know what's happenning. When n = 97, 98 ... 127, the printf prints out str1 but from n = 128 onwards, it doesn't print out anymore. Why and how do I fix it?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:

    unsigned char str2[2], str1[2]= "a";
    unsigned int n = 240;

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Analyze the table of int to char conversions. Maybe 128 is a different number. Theoretically, it should work (logically) out. char and int are interchangeable.

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is a no brainer:

    The range of a 'char' is: -128/+127

    Ok, so a "string" is what? It's an array of characters.
    Given this, is it really a surprise that it doesn't print them?

    for( c=-128; c < 128; c++ ) if( isprint(c) ) putc(c);

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    quzah is right.

    quzah, if I'm correct, don't you have to unsign the char to use the value of 128?

    --Garfield
    1978 Silver Anniversary Corvette

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    don't you have to unsign the char to use the value of 128?
    Yes for positive 128, but negative 128 is fine using two's complement.
    zen

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yeah, that's what I thought.
    1978 Silver Anniversary Corvette

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    I think you can't use the number 128 cause the number 128 is formed in the binary system by 10000000 and the fist bit on the left (the 1) is used to define if the number is positive or negative, cause that te maximum number you can write in binary is 1111111 = 127 (decimal)

  9. #9
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Then you would need the 32-bit.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM