int to char help

This is a discussion on int to char help within the C Programming forums, part of the General Programming Boards category; if have use the following code [\code]int i = 81; char str[10]; sprintf(str, "%s", i);[code] when i run the program ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21