Solution for Exercise 6-8

ex0608

#include <stdio.h>

int main()
{
    int shadrach, meshach, abednego;

    shadrach = 701;
    meshach = 709;
    abednego = 719;
    printf("Shadrach is %d\nMeshach is %d\nAbednego is %d\n",shadrach,meshach,abednego);
    return(0);
}

Notes

* Above you see the same code as shown in the book, although I didn't wrap the text.

* Here is the same code, with the \ character used to split the long printf() statement:

#include <stdio.h>

int main()
{
    int shadrach, meshach, abednego;

    shadrach = 701;
    meshach = 709;
    abednego = 719;
    printf("Shadrach is %d\nMeshach is %d\nAbednego is %d\n",\
        shadrach,meshach,abednego);
    return(0);
}

* The \ (backslash) and the blanks that begin Line 11 are ignored by the compiler. They exist outside of double quotes, so they're considered white space.

* You can also fashion the printf() statement like this:

#include <stdio.h>

int main()
{
    int shadrach, meshach, abednego;

    shadrach = 701;
    meshach = 709;
    abednego = 719;
    printf("Shadrach is %d\nMeshach is %d\nAbednego is %d\n",
        shadrach,
        meshach,
        abednego);
    return(0);
}

* The construction above doesn't need a backslash. Again, the new line and spaces after the comma (at Line 10) are ignored by the compiler; they're white space. I've also set each variable on a line by itself. For a complex printf() statement, such formatting is blessed relief — and quite readable.

* Remember to close the parentheses and end the printf() statement with a semicolon, especially when it's split across multiple lines.

* And, of course, you could have used three printf() functions on three statements to do the same thing. As is usually the case, you'll find multiple solutions to programming exercises in C.