Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <errno.h> //perror()
  2. #include <stdio.h> //perror()
  3. #include <stdlib.h> //exit(), atoi()
  4. #include <unistd.h> //read(), write()
  5. #define BUFF_SIZE 4096
  6.  
  7. int main(int argc, char const *argv[]) {
  8. int first = 0;
  9. int second = 0;
  10. int sum = 0;
  11. int n = 0;
  12. char buffer[BUFF_SIZE];
  13.  
  14. printf("Inserisci il primo addendo (-1 per uscire):\n");
  15. while (((n = read(STDIN_FILENO, buffer, BUFF_SIZE)) > 0) &&
  16. ((first = atoi(buffer)) != -1)) {
  17. printf("Inserisci il secondo addendo:\n");
  18. if (read(STDIN_FILENO, buffer, BUFF_SIZE) == -1) {
  19. perror(argv[0]);
  20. exit(EXIT_FAILURE);
  21. }
  22.  
  23. second = atoi(buffer);
  24. sum = first + second;
  25.  
  26. printf("La somma e:\n");
  27.  
  28. int size_byte = sprintf(buffer, "%d", sum);
  29. if (write(STDOUT_FILENO, buffer, size_byte) == -1) {
  30. perror(argv[0]);
  31. exit(EXIT_FAILURE);
  32. }
  33.  
  34. printf("\n\nInserisci il primo addendo (diverso da -1):\n");
  35. }
  36.  
  37. if (n == -1) {
  38. perror(argv[0]);
  39. exit(EXIT_FAILURE);
  40. }
  41. exit(EXIT_SUCCESS);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement