Results 1 to 3 of 3

Thread: printf()

  1. #1
    Join Date
    May 2013
    Beans
    27

    printf()

    hi all,
    when i use this code without loop, printf perform correctly but when use it in loop , it does not work, what can i do?!
    example:
    this code work correctly:
    Code:
    int countRead=read(0,buffer,100);
            printf("%d",countRead);
    but when use this , doesn't print any thing:
    Code:
    while(1)
        {
            int countRead=read(0,buffer,100);
            printf("%d",countRead);
        }
    and warning say:

    incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

    why say that?!
    please help me.
    Last edited by ferizhandi; May 14th, 2013 at 09:18 PM.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: printf()

    You need to include stdio.h for printf:

    Code:
    #include <stdio.h>
    If I guess what your whole program is and omit this include I get the same warning whether it's in a loop or not. Perhaps you could post the full version of your program and the commands you are using to compile it?
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,257
    Distro
    Kubuntu

    Re: printf()

    Quote Originally Posted by ferizhandi View Post
    hi all,
    when i use this code without loop, printf perform correctly but when use it in loop , it does not work, what can i do?!
    example:
    this code work correctly:
    Code:
    int countRead=read(0,buffer,100);
            printf("%d",countRead);
    but when use this , doesn't print any thing:
    Code:
    while(1)
        {
            int countRead=read(0,buffer,100);
            printf("%d",countRead);
        }
    and warning say:

    incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

    why say that?!
    please help me.
    As rsenior indicated, you need to include stdio.h to clean up the warning message that is generated when you compile your code.

    The other thing you need to realize is that stdout is not flushed after a printf() is called unless either a) you print a newline character, b) the stdout buffer is full, or c) the program exits (gracefully).

    In your first attempt, where you only called read() and printf() once, your program exited gracefully, and hence you saw your output. When these statements are inserted into the while-loop, you will not see output until you fill the stdout buffer. You can force the flushing of the stdout buffer by using fflush(stdout); however for your program, I recommend that you print a newline character.

    P.S. stdout will buffer output, however stderr does not. This can present an advantage should you want to print a string with no newline. For example:
    Code:
    ...
    char buffer[100];
    
    fprintf(stderr, "Enter a string: ");
    int rtn = read(fileno(stdin), buffer, sizeof(buffer) - 1);
    ...

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •