[Courses] Chapter 4 Exercise 4-2 SPOILER

Wendy Galovich wendy at scottishmusician.com
Sun Feb 17 16:47:29 EST 2002


Hi... since this list has been fairly quiet the last few days, I thought I'd 
ask how everyone is doing, and how far most of us have progressed in the 
book. At this point I can say I'm solidly through Chapter 4, though I did 
come across one question in doing the exercises at the end of the chapter; it 
has to do with the character variable type. 

In a couple of the exercises, I saw two different ways of getting the end 
result and since they didn't specify which to use, I did both. Exercise 4-2 
was one of those - I first completed it simply printing the characters I 
wanted:

#include <stdio.h>

/* Exercise 4-2: Simple program to print a block E using asterisks, where */
/* the E has a height of 7 characters and a width of 5 characters.        */

int main()
{
    printf("\n");
    printf("*****\n");
    printf("*\n");
    printf("*\n");
    printf("***\n");
    printf("*\n");
    printf("*\n");
    printf("*****\n");
    printf("\n");
    return (0);
}

Then I tried to use a character variable to get the same result, and that was 
where I hit a problem. Ultimately I landed on the following, which worked: 

#include <stdio.h>

/* Exercise 4-2: Program to print a block E using asterisks, where the E */
/* has a height of 7 characters and a width of 5 characters.             */

char a;	/* the character '*' */

int main()
{
    a = '*';

    printf("\n");
    printf("%c%c%c%c%c\n", a,a,a,a,a);
    printf("%c\n", a);
    printf("%c\n", a);
    printf("%c%c%c\n", a,a,a);
    printf("%c\n", a);
    printf("%c\n", a);
    printf("%c%c%c%c%c\n", a,a,a,a,a);
    printf("\n");
    return (0);
}



However it took some sleuthing to get there, because I started out trying to 
define the variable differently:


#include <stdio.h>

/* Exercise 4-2: Program to print a block E using asterisks, where the E */
/* has a height of 7 characters and a width of 5 characters.             */

char *;	/* the character '*' */

int main()
{
    * = '*';

    printf("\n");
    printf("%c%c%c%c%c\n", *,*,*,*,*);
    printf("%c\n", *);
    printf("%c\n", *);
    printf("%c%c%c\n", *,*,*);
    printf("%c\n", *);
    printf("%c\n", *);
    printf("%c%c%c%c%c\n", *,*,*,*,*);
    printf("\n");
    return (0);
}

I didn't see anything in the book - at least not in Chapter 4 - saying that * 
couldn't be used as a character variable in that way, but obviously it can't. 
So, my question is this: does anyone have a list of the keyboard characters 
which are "legal" to use as char variables? Or maybe more importantly, which 
are not? 

Thanks, 
Wendy



More information about the Courses mailing list