I've written code to make a right pyramid out a character.
However, when I execute the program, the last two lines of the pyramid have garbage characters placed after them even when it exceeds the size of the array.
The code is here:
#include <stdio.h>
#include <string.h>
#define ROW 5
int main(void) {
char array[ROW];
int x = 0;
int row = 0;
for (row = 0; row < ROW; row++) {
array[x] = 'a';
if (x < ROW) {
printf("%s\n", dolla);
}
x++;
}
getchar();
}
Where are the garbage characters coming from? It's only on lines after the third.
The problem in your code is that you have not terminated your string with
\0
(null) character. Here's a workout for your code:I'm no specialist, but I've read the following in many typical C books:
int
arrays in C are initialized to0
, whilechar
arrays are initialized to garbage.And yeah, forgot to mention, it's no
dolla
, it'sarray
.