Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #define LEN 10
  3. char * getnchar(char * str, int n);
  4. int main(void)
  5. {
  6.     char input[LEN];//prepare the memory block when main() is executing.
  7.     char *chk;
  8.    
  9.     chk = getnchar(input, LEN - 1);
  10.     //this if branch has no sense in terms of usage except the first console input is EOF
  11.     //I hasn't caught any implementation in the corresponding function
  12.     if (chk == NULL)
  13.         puts("Input failed.");
  14.     else
  15.         puts(input);
  16.     puts("Done.\n");
  17.    
  18.     return 0;
  19. }
  20.  
  21. char * getnchar(char * str, int n)
  22. {
  23.     int i;
  24.     int ch;
  25.  
  26.     for (i = 0; i < n; i++)
  27.     {
  28.         ch = getchar();
  29.         if (ch != EOF)
  30.             str[i] = ch;
  31.         else
  32.             break;
  33.     }
  34.     //????????? logic has no sense to me.  
  35.     if (ch == EOF)
  36.         return NULL;
  37.     else
  38.     {
  39.         str[i] = '\0';
  40.         return str;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement