Post

Read a non-negative number and write it reversed

This is the simplest solution: #include <stdio.h> int main(void) {         int n;         printf("Enter a non-negative number\n");         scanf("%d", &n);         if(n < 0) {                 printf("*** Error: the number is negative!\n");                 return 1;         }         do {                 printf("%d", n % 10);                 n /= 10; // Equivalent to "n = n / 10"         }         while(n > 0);         return 0; } Error messages by convention should be written to standard error, so the statement  printf("*** Error: the number is negative!\n"); should be replaced by fprintf(stderr, "*** Error: the number is negative!\...

Read a number and write whether it is negative or positive or zero

This is the simplest solution: #include <stdio.h> int main(void) { int n; printf("Enter a number\n"); scanf("%d", &n); if(n > 0) printf("Positive\n"); else if(n It works fine if I enter correct numbers, but if I enter text instead of a number, it says it's a positive number. This is because scanf() doesn't do a "strict" check, so we have to add it ourselves! Later, we'll see how to add more stringent checks to scanf()'s reading.

The HelloWorld C program

The HelloWorld program is a long-standing tradition of all programming languages. #include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; }