Thread: int to char help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    int to char help

    if have use the following code

    [\code]int i = 81;
    char str[10];
    sprintf(str, "%s", i);[code]

    when i run the program nothing happens. It compiles fine but there is no output

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what, exactly, do you expect to be output? There is nothing in your code to perform such output - and you got the code-tags the wrong way around the one with the slash should be at the end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Where do you actually populate str?

    And I don't think that's the correct call of sprintf(), but I'm unsure from memory.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    My bad guys. I found the problem. Wasnt supposed to be a string, jus a char. I have it now.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not supposed to be a char.
    A string is an array of chars. You've got a buffer overrun if you've defined str as merely a char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe "%d" instead of "%s" would help?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM