9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex23_imp.c (549B)
   1 /*    Reads an integer from stdin and prints it
   2     on stdout in octal base; */ 
   3 
   4 #include <stdio.h>
   5 #include <stdlib.h>
   6 
   7 #define MAX_LEN 100
   8 #define ERR_MSG_NEG "Err: the number is negative"
   9 
  10 int main (void)
  11 {
  12     int num;
  13     char buffer[MAX_LEN];
  14 
  15     if(!fgets(buffer, MAX_LEN, stdin)) return 1;
  16 
  17 /*    Converts the input str to int, if the number
  18     is negative, puts an error message on stderr;    */
  19     if((num = atoi(buffer)) < 0 ) {
  20         fprintf(stderr, "%s\n", ERR_MSG_NEG);
  21         return 1;
  22     }
  23     
  24 /*    prints the integer in octal base;    */
  25     printf("%o\n", num);
  26     return 0;
  27 }