Im new to C programming i wrote a simple program that displays A - F.
Im having a problem displaying F to A. could someone help me .
You've shown us the output that is in error, but you have not
really indicated what you want the output to look like.
The leading '#' disappeared somewhere on that line.
#include <stdlib.h>
#include <string.h>
You do not use anything from string.h in the program you show.
int main(void)
{
int row;
char ch;
for (row = 0; row <= 6; row++)
You have 6 characters to deal with, which are going to be at
offsets 0, 1, 2, 3, 4, and 5, but your termination condition is
such that you are also going to execute the loop for the row
offset 6. It is not clear to us whether that is a mistake or
a deliberate outputting of the empty line?
{
for (ch = ('A' + row ); ch < ('A' + CHARS); ch++)
printf("%c", ch);
It would be clearer if you indented that printf()
Your code here assumes that the characters 'A' through 'F' are
contiguous. All that C promises is that '0' through '9' are
contiguous. It would be legal in C for the underlying character
set to run something like AQ*v/ba in which case your first
line of output would be AQ*v/b instead of the ABCDEF that you hoped for.
There really is no good hack that you can use to get around this,
so the easiest way to deal with it is to precreate a string that
has the characters in the order you want them:
char hexes[] = "ABCDEF";
Then your logic becomes something like
for (row=0; row < strlen(hexes); row++) {
for( offset=row; offset < strlen(hexes); offset++ ) putchar(hexes[offset]);
putchar('\n');
}
As for the reverse direction, I will give the hint:
putchar(hexes[strlen(hexes)-offset-1])
printf("\n");
}
system("pause");
You should return a value from main() or use exit(), so that the
operating system does not get confused about whether the program
worked or failed.