Thread: printf waits until main returns to display

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    16
    yep.

    Well, not anymore because I am using the command you gave me.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Wow. What if you put a newline in there:
    Code:
    printf("Enter an index: \n");
    Which I know you don't want to do that in this case, but it could be your buffer isn't flushed until a newline (or the buffer is full) which wouldn't be unusual.

    It really is about the OS, by the way. But if you are used to Java, the Java VM/interpreter probably includes a mechanism to make it's own behavior uniform, taking the OS's behaviour into account. As you point out, C is not so fancy.
    Last edited by MK27; 01-28-2010 at 01:26 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    16
    duly noted about card arrays.

    Are you sure that strlen() does not require any non-standard libraries?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, <string.h> is a standard library.

    Also, sizeof will lie about the length of strings like

    Code:
    char s[100] = "Hello";
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anon View Post
    Also, sizeof will lie about the length of strings like

    Code:
    char s[100] = "Hello";
    Incorrect! The string is 5 characters long, which is what strlen() will report.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    strlen should be in string.h

    Edit:
    what the hell? Before I posted this then bright's question about strlen() was the last reply and now when I click submit 2 more replies pop up.. forum bug?
    Last edited by _Mike; 01-28-2010 at 04:02 PM.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    16
    It was my impression that sizeof didn't lie so much as it just includes the null terminator at the end of a C-style char array

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brightmatter View Post
    It was my impression that sizeof didn't lie so much as it just includes the null terminator at the end of a C-style char array
    Try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void) {
    	char *sp = malloc(12);
    
    	strcpy(sp,"hello world");
    	printf("%d %d\n",sizeof(sp),strlen(sp));
            free(sp);
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    char s[100] = "Hello";
    sizeof(s) == 100
    strlen(s) == 5

    char* s = "This is a long string!";
    sizeof(s) == 4
    strlen(s) == 22

    Using sizeof for string length is not reliable..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  4. whats wrong with this?
    By petedee in forum C Programming
    Replies: 32
    Last Post: 01-06-2004, 10:28 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM