Search code examples
cgccfgets

Fgets while loop, compiler warning about a statement with no effect


What can I do to get rid of the compiler warning about a statement with no effect? I'm using fgets to read the output of a bash command, but I don't need to or want to do anything with it apart from outputting it to my buffer.

while (fgets(pid, sizeof(pid), fp) != NULL) {
    1; //Statement with no effect :(
}

Solution

  • Why do you have the 1; line? You can have empty while loops

    while (fgets(pid, sizeof(pid), fp) != NULL) {
    }
    

    Or

    while (fgets(pid, sizeof(pid), fp) != NULL); // Note semi-colon
    

    It's probably best to avoid the second option as it's easier to misunderstand what's going on.