fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6. int inputAsingleChar,i=0,j=4;
  7. char *name = malloc(j);
  8. if (name == NULL){
  9. printf("No memory");
  10. exit(1);
  11. }
  12. printf("Your name: \n");
  13. while((inputAsingleChar = getchar()) != '\n' && inputAsingleChar != EOF)
  14. {
  15. if(i==j){
  16. j+=4;
  17. char * tmp = realloc(name, j);
  18. if(tmp== NULL){printf("Couldn't realloc but the 'name' array is still valid");}
  19. else{name = tmp;}
  20. }
  21. name[i++] = inputAsingleChar ;
  22. }
  23. name[i] = '\0';
  24. printf("Name: %s \n", name);
  25. free(name);
  26. return 0;
  27. }
Success #stdin #stdout 0s 10320KB
stdin
THIS IS SPARTAAAAAAAAAAAAAAAAAAAAAAAA
stdout
Your name: 
Name: THIS IS SPARTAAAAAAAAAAAAAAAAAAAAAAAA