Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to store my name or word of any length.

What I have tried:

C
char name[20];
for(i=0;i<=10;i++)
scanf("%c",&name[i]);
Posted
Updated 28-Jul-23 4:14am
v2

1 solution

You cannot store name or word 'of any length' (we have finite-storage machines).
Anyway, your best bet is fgets[^]:
C
#include <stdio.h>
#include <string.h>


int main()
{
  enum
  {
    SIZE = 256,
  };

  char name[SIZE];

  for(;;)
  {
    printf("please enter your name ('quit') to terminate: ");
    fgets(name, sizeof(name), stdin);
    if ( strncmp(name, "quit", 4) == 0)
      break;
    printf("hi %s", name);
  }

  printf("goodbye\n");

  return 0;
}
 
Share this answer
 
Comments
Andre Oosthuizen 28-Jul-23 10:15am    
+5
CPallini 28-Jul-23 10:18am    
Thank you.
R.P. Dutta 30-Jul-23 13:12pm    
Thanks Sir/maam
CPallini 30-Jul-23 14:52pm    
You are welcome!
-- Sir Carlo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900