Splint (programming Tool) - Example

Example

#include int main { char c; while (c != 'x'); { c = getchar; if (c = 'x') return 0; switch (c) { case '\n': case '\r': printf("Newline\n"); default: printf("%c",c); } } return 0; }

Splint's output:

Variable c used before definition Suspected infinite loop. No value used in loop test (c) is modified by test or loop body. Assignment of int to char: c = getchar Test expression for if is assignment expression: c = 'x' Test expression for if not boolean, type char: c = 'x' Fall through case (no preceding break)

Fixed source:

#include int main { char c = (char) 0; // Added an initial assignment definition. while (c != 'x') { c = (char) getchar; // Type-cast to char. if (c == 'x') // Fixed the assignment error to make it a comparison operator. return 0; switch (c) { case '\n': case '\r': printf("Newline\n"); break; // Added break statement to prevent fall-through. default: printf("%c",c); break; //Added break statement to default catch, out of good practice. } } return 0; }

Read more about this topic:  Splint (programming Tool)

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)