This program in essence is extremely simple, however, it can be written in many different ways. I'm a beginner at C and I've written it in 5 different variations (mostly differing in the use of getchar
over scanf
, while
over do while
loops..).
Apart from readability and/or maintainability, is there any difference between these 5 code blocks (such as perhaps performance or efficiency, if we disregard the use of getchar
over scanf
)?
unsigned char d;
while (1) while(scanf("%c", &d), d != '\n') { printf("%d\n", d * d); }
do { scanf(" %c", &d); printf("%d\n", d * d); } while (1);
while (1) while((d = getchar()) != '\n') { //could also be written as while(d = getchar(), d != '\n') printf("%d\n", d * d); }
while ((d = getchar())) { if(d == '\n') continue; printf("%d\n", d * d); }
do { d = getchar(); if(d == '\n') continue; printf("%d\n", d * d); } while (1);