A few thoughts
Your code is a loop inside a loop. For 2D arrays you can consider doing it all in just one loop. For small arrays ie: 1-5 it really doesn't matter. For larger arrays your algorithm will be faster if you calculate i,j as a function of the array.
See: Map a 2D array onto a 1D array
Also you could name your variables 'a' 'i',j something like row,col or something more meaningful depending on the code's indented purpose.
The other part that confuses me a bit. Assuming your array is 5x5 and also assuming your end goal is to ensure every '1' always has 8
neighbors of zero, once the first random number generate produces a '1' and forces the 8 neighbors to zero as many as 4 neighbors no longer need to be updated.
(i,j+1)/(i+1,j)/(i+1,j+1)/(i-1,j). As noted by @Weather Vane you are overwriting your work this maybe where you get that 'redundant' feeling.
#define MAX_ARRAY 5
int in_bounds(int row,int col)
{
if (row >= MAX_ARRAY) return 0;
if (col >= MAX_ARRAY) return 0;
return 1;
}
void print_matrix (int matrix[MAX_ARRAY][MAX_ARRAY],int size)
{
for (int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
fprintf(stdout,"%d",matrix[i][j]);
// fprintf(stdout,"%d,%d ",i,j);
}
fprintf(stdout,"\n");
}
}
int main()
{
//for stack overflow: https://stackoverflow.com/questions/65398722/is-there-a-cleaner-way-to-write-this-c-code/65399364#65399364
srand(time(0));
int matrix[MAX_ARRAY][MAX_ARRAY];
memset(matrix, -1, sizeof(int) * MAX_ARRAY * MAX_ARRAY);
for (int counter=0;counter<MAX_ARRAY * MAX_ARRAY;counter++)
{
int col=counter / MAX_ARRAY;
int row=counter % MAX_ARRAY;
if (matrix[row][col] == -1)
{
matrix[row][col] = rand() %2;
if (matrix[row][col] == 1)
{
if (in_bounds(row,col+1)) matrix[row][col+1] = 0;
if (in_bounds(row+1,col)) matrix[row+1][col] = 0;
if (in_bounds(row+1,col+1)) matrix[row+1][col+1] = 0;
if (in_bounds(row-1,col)) matrix[row-1][col] = 0;
if (in_bounds(col,row-1)) matrix[col][row-1] = 0;
if (in_bounds(row-1,col-1)) matrix[row-1][col-1] = 0;
if (in_bounds(row-1,col+1)) matrix[row-1][col+1] = 0;
if (in_bounds(row+1,col-1)) matrix[row+1][col-1] = 0;
}
}
}
print_matrix(matrix,MAX_ARRAY);
}