printf outdoor for

This is a discussion on printf outdoor for within the C Programming forums, part of the General Programming Boards category; Hello, suppose me to solved it... Code: for (i = 0; i < 4; i++) { const unsigned int id ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05: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, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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