9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex24_imp.c (593B)
   1 /*    Reads an non-negative integer from stdin and prints it
   2     on stdout in hexadecimal 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     unsigned int num;
  13     char buffer[MAX_LEN];
  14 
  15     if(fgets(buffer, MAX_LEN, stdin) == NULL)
  16         return 1;
  17 
  18     /*    Converts the input str to int, if the number
  19         is negative, puts an error message on stderr;    */
  20     if((num = atoi(buffer)) < 0 ) {
  21         fprintf(stderr, ERR_MSG_NEG"\n");
  22         return 1;
  23     }
  24     
  25     /*    prints the integer in hexadecimal base;    */
  26     printf("%X\n", num);
  27     return 0;
  28 }