I'm trying to print the following pattern:
@@@@@@@@
@@@@@@@
@@@@@@
@@@@@
However, I am getting this instead:
@ABCDEFG
ABCDEFG
BCDEFG
CDEFG
I'm not sure what I am doing wrong. I would appreciate any feedback or direction.
#include <stdio.h>
#define ROWS 4
#define CHARS 8
int main(void)
{
for(int row = 0; row < ROWS; row++)
{
for(char ch = ('@' + row); ch < ('@' + CHARS); ch++)
printf("%c", ch);
printf("\n");
}
return 0;
}
The +
operator doesn't work the way you think it does. @
is converted to it's ASCII value (64) then you add row
. When row
is 2
, you are saying: print the character that coresponds the number (64 + 2) which is A
.
I would change the inside loop to something like this:
for(int ch = row; ch < CHARS; ch++) {
printf("%c", '@');
}
printf("\n");
Why are you complicating second for
with that character. It can be simply
for(int col = row; col < CHARS; col++)
printf("%c", '@');
将 ch 更改为 '@' printf("%c", '@');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.