Reputation: 11
#define ________ 0x0
#define _______X 0x1
#define ______X_ 0x2
#define ______XX 0x3
#define _____X__ 0x4
I have created macros to store a few sets of characters as integer values in the .h file as shown above so as to make the .h file usable in other files.
If I include this .h file in any other .c file containing char arrays such as ch[] = {________,_______X}
is there anyway for me to print the actual sets of characters i.e "____" for ch[0] and "_______X" for ch[1]. When I try to print it prints the char value for the corresponding integer values i.e 0 and 1. Please let me know what command I can use to print the same.
Upvotes: 1
Views: 179
Reputation: 4433
You are representing numbers in a "binary" notation ( _ stands for 0 and X stands for 1). Since positive integer numbers are stored in memory in their exact binary representation, you can use the bit operators >> << and & to obtain the succesive bits.
Finally, for each bit, you can do something like that:
putchar( bit? 'X': '_');
This sentence asks if your bit is 1 or 0. If it is equal to 1, then a character 'X' is printed, else, '_' is printed.
I left to you the details of writting the (iterative) algorithm to find the subsequent bits of a given number.
Upvotes: 0
Reputation: 4289
A character is, by definition, a single character. Therefore, printing a single character such as 0x2 will never print several characters such as "_ _ _ _ _ _ X _". The way you'd do this is by creating an array of char * (strings in C are char *) rather an array of char. As in,
char **mystrings = {"________", "_______X", "______X_", etc.}
Then, you can use
printf("%s", mystrings[______X_]);
which will print
______X_
Upvotes: 1