Advertisement
RicardasSim

test scanf

Dec 29th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main (int argc, char **argv){
  6.  
  7.     char str[64];
  8.     int c;
  9.     int result;
  10.    
  11.     printf("Enter name: ");
  12.    
  13.     //scanf("%32s", str);
  14.    
  15.     result = scanf("%32[^\n]", str);
  16.    
  17.     if(result!=1||result==EOF)
  18.     {
  19.         printf("error occurred (1)\n");
  20.         return 1;
  21.     }
  22.    
  23.     if((c = getchar()) != '\n'){
  24.         printf("error: input to long.\n");
  25.         return 1;          
  26.     };
  27.    
  28.     printf("Hi %s\n",str);
  29.    
  30.     //while((c = getchar()) != '\n' && c != EOF){};
  31.    
  32.     printf("Enter message: ");
  33.    
  34.     memset(str,0,64);
  35.    
  36.     result = scanf("%32[^\n]", str);
  37.    
  38.     if(result!=1||result==EOF)
  39.     {
  40.         printf("error occurred (2)\n");
  41.         return 1;
  42.     }
  43.    
  44.     if((c = getchar()) != '\n'){
  45.         printf("error: input to long.\n");
  46.         return 1;
  47.     };
  48.    
  49.     printf("You entered: %s\n",str);
  50.  
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement