TheWebs
TheWebs

Reputation: 12923

Trying to print ascii representation in C

So I am trying to dynamically generate a box, based on some ascii characters I have defined in a box_ascii.h. I am just tying to test my logic and when I get into my for loops I get an error:

$ make create_dynamic_box
cc     create_dynamic_box.c   -o create_dynamic_box
create_dynamic_box.c:26:30: error: expected expression
                printf("%c", BOX_TOP_LEFT_CORNER);
                             ^
./box_ascii.h:6:41: note: expanded from macro 'BOX_TOP_LEFT_CORNER'
#define BOX_TOP_LEFT_CORNER             = "\214" // ╓

My google research on the error usually mean I need something like int b = a, which basically tells me that something doesn't have a type or has the wrong type(?)

Any ways the code is:

box_ascii.h

#ifndef __box_ascii_h__
#define __box_ascii_h__

// Might not be exact Ascii Characters but they come from:
// http://www.asciitable.com/
#define BOX_TOP_LEFT_CORNER             = "\214" // ╓
#define BOX_TOP_RIGHT_CORNER            = "\187" // ╖
#define BOX_BOTTOM_LEFT_CORNER          = "\200" // ╚
#define BOX_BOTTOM_RIGHT_CORNER         = "\188" // ╛
#define BOX_SIDE                        = "\186" // ║
#define BOX_TOP_BOTTOM                  = "\205" // ═

#endif

create_dynamic_box.c

#include <stdio.h>
#include <stdlib.h>
#include "box_ascii.h"

void print_border(int width, int height);

int main(int argc, char *argv[]) {
    if(argc < 3) {
        printf("Mustenter width and height.");
        return -1;
    }


    print_border(atoi(argv[1]), atoi(argv[2]));

    return 0;
}

void print_border(int width, int height) {
    int row = 0;
    int col = 0;

    for (row = 0; row < width; row ++) {
        for (col = 0; col < height; col++) {
            if (row == 0 && col == 0) {
                printf("%c", BOX_TOP_LEFT_CORNER); // error thrown here.
            }
        }
    }
}

Whats going on? is it because I am using %c ??

Upvotes: 0

Views: 458

Answers (2)

Peter
Peter

Reputation: 36597

The error messages come about because macros do text substitution - they are not named values.

So

#define BOX_TOP_LEFT_CORNER             = "\214"
printf("%c", BOX_TOP_LEFT_CORNER);

will be seen by the compiler as

printf("%c", = "\214");

That has two problems. Firstly, the = is misplaced. Second, %c causes printf() to expect a single character, whereas "\214" is an array of two characters ('\214' and '\0').

So, the = sign needs to be removed from the macro.

If you want to use the %c format, change the macro definition to use single quote characters (')

#define BOX_TOP_LEFT_CORNER           '\214' 

If you want the macros to be multi-character strings, then use the %s format.

Either way, don't supply a string where a single character is expected, or vice versa.

Also: The characters like \214 are extended ASCII (which is not well defined) not ASCII.

Upvotes: 2

Craig S. Anderson
Craig S. Anderson

Reputation: 7374

Your BOX_* macros define strings, not characters. They should be:

#define BOX_TOP_LEFT_CORNER '\214' //

and so forth for the rest of the macros.

In C, `\xyz' defines a character whose octal code is xyz.

Reference here.

Upvotes: 0

Related Questions