I sometimes hear people say how they can distinguish between good and bad coders of a certain programming language or even programming in general simply by looking at the code they write.
I'm relearning C, and I was just wondering whether my program follows best practices in terms of my commenting, the way I set it up, and generally the way I've incorporated the for
and while
loops along with macros (whether it be better to use a goto
statement here, or a while
loop instead of a for
loop there, etc).
#include <stdio.h>
#define SIZE (sizeof(words[0])/sizeof(words[0][1]))
#define ROWS 10
#define COLUMNS 20
int main()
{
//The maxmimum letters in a words is 20 and the max words is 10
char words[ROWS][COLUMNS]={0},character;
int count_1=0,count_2=0,i,j;
printf("Enter something: ");
character=getch();
putch(character);
while(character!='\r')
{
words[count_1][count_2]=character;
//Allows the next index to contain the value of the next inputted character
count_2++;
if(character==' ')
{
//index of inputted characters reset to 0
count_2=0;
//Allows the next row in array to be changed as one row will already satisfy a word
count_1++;
}
character=getch();
putch(character);
}
printf("\n");
//Starts from the last word
for(i=count_1;i>=0;i--)
{
for(j=0;j<SIZE;j++)
{
if(words[i][j]!=0)
printf("%c",words[i][j]);
}
}
}
I know the next bit is a bit opinionated, but does my code give an idea of where I'm at with logic and programming as a whole, or is the first part just some kind of myth (i.e. you can't judge a person by such a small piece of code)?
Note: I deliberately avoided the use of functions and strings. I got this task from C Programming: A Modern Approach, and I'm only attempting to solve a programming task simply by using only the features I've learned thus far in the book.
'\r'
. Note that'\r'
is difficult to type via keyboard on some machines (on Windows, pressing the Enter key gives'\n'
). \$\endgroup\$\n
instead, and it didn't reverse my input sentence. \$\endgroup\$abc def ghi .
it echoes that and then prints.ghi def abc
which is what I'd expect from inspection. \$\endgroup\$