Thread: printf effects vars ?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Question printf effects vars ?

    Hi !
    As a beginner in C I use a lot of printf() in my code to verify what's happening in my app. I just wrote an app. that scans thru a directory and made some tests on the fieldname. When I was pleased with the result, I deleted all printf() except the last one.

    When I deleted them, I got wrong result on my last print().
    The one I deleted, contained the same var as the last one except for one.

    In my app I use DIR struct. I found that if I did not printf( "%s\n", dir->d_name ) in the loop, I got another result in my var....

    My question is, can the use of printf() effect the variables ?

    Best regards
    Micael

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It all depends. If you do something like:

    Code:
    int num = 55;
    
    printf( "%i", num++ );
    printf( "%i", num );
    Obviously you will get two different answers. However with simply outputting a string like you state in your post I don't see how it could change. Why don't you post your code and we can look more closely.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    3
    As a side remark....

    Using traces (printf statements) is one was to verify what
    your code is doing but you might be better off learning
    how to use a debugger (e.g. gdb + ddd) or using
    assertions (see assert.h) in your code - as a bonus these
    will also help you understand how C works ;-)

    A.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>My question is, can the use of printf() effect the variables ?
    If your program has memory overlay related bugs (writing to memory locations incorrectly), use of any code can affect how it runs (or doesn't run!).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    6

    I found it !

    I got it working, but I'm not sure of my conclusion..

    I did :
    strncpy( buff, &dir->d_name[ipos+1], 8 );

    I guess &dir->d_name[ipos+1] gave me a mem address to the structure and not the value of dir->d_name and caused position skips in the memory (?)

    Member d_name is the first member in struct DIR, and I guess it share the same mem position as the struct itself.

    When doing a printf("%s\n", dir->d_name);
    It seems like this notation resets the structs pointer to the start again ?

    I made a for statement and took char by char to fill my buff[] instead of using strncpy() so I got it working.

    It would be very valuble for me to understand this correct, since I guess this will not be the last time I'll bump in to this kind of a problem again :-)

    Thank's all for your answer !

    Best regards
    Micael

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some of your actual code (using code tags), that way we can see if it's right or not.

    >>It seems like this notation resets the structs pointer to the start again ?
    Nothing you've shown so far will actually change the value to the pointer is holding.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  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