Thread: printf outdoor for

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    77

    printf outdoor for

    Hello, suppose me to solved it...
    Code:
      for (i = 0; i < 4; i++)
      {
        const unsigned int id = (address >> (24 - i*8)) & 0xFF;
    
        sprintf(p->adr, "%u%c", id, (i == 3 ? '\n' : '.'));
      printf("%s\n", p->adr); //good 10.10.10.3
      }
      printf("%s\n", p->adr); // printed just latest value 0.0.0.3, why? me need print it not into "for" area.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post the smallest compilable example that demonstrates the problem.
    This is not much to work with.
    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.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because once again your sprintf starts at the beginning of the buffer not skiping the data coied there on the previous iteration of the loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    because once again your sprintf starts at the beginning of the buffer not skiping the data coied there on the previous iteration of the loop
    which's way I can take successful useful printf outside this loop? Hm, I think what if I'll tried usage construction strca and itoa it's not change result of printf outside of the loop.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your for loop was as you had posted it, then the printf outside the loop would print the same as the last of the four print statements inside the loop. On the other hand, if the loop was as you had posted it, there's no way you would get "10.10.10.3" either -- you might get
    10.
    10.
    10.
    3
    but each time through the loop you destroy what was previously printed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM