Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include <stdio.h>
int main()
{
  char name[20];  
  printf("Enter Your Name : "); 
  scanf("%s",&name char[20]);  
  printf("Your name is : %s", name);  
 }


What I have tried:

plese tell me fast this is an important project
Posted
Updated 18-Mar-22 6:03am
Comments
Richard MacCutchan 18-Mar-22 7:50am    
Be aware also that scanf will only input characters up to the first whitespace character. So you can enter "Advik" but not "Advik Gupta".

Try scanf("%s", &name[0]). You already know that name is an array of 20 char. What scanf wants is the address where it should put the string that it reads from the console, namely the start of the name array.

C library function - scanf()[^]
C library function - printf()[^]
 
Share this answer
 
v3
Try this example from TutorialsPoint:
#include <stdio.h>

int main () {
   char str1[20], str2[30];

   printf("Enter name: ");
   scanf("%19s", str1);

   printf("Enter your website name: ");
   scanf("%29s", str2);

   printf("Entered Name: %s\n", str1);
   printf("Entered Website:%s", str2);
   
   return(0);
}

Although it does not seem to work in the TutorialsPoint live editor: Online C Compiler[^]

[EDIT] it seems you have to type the input first in the STDIN tab.
 
Share this answer
 
v2
To add to what Greg has said, with an array, you don't need to use the "address of" operator at all as the C specification says that the name of an array is a pointer to the first element. So this:
C
scanf("%s", &name[0]);
is the same as this:
C
scanf("%s", name);
And this:
C
scanf("%s", &name);
is too as the address of the array variable is the same as the address of the first character in it. Looks odd? Yes - but C is a very old language!

Try this and you'll see what I mean:
C
char name[20] = "OriginalGriff";
printf("%s\n", name);
printf("%s\n", &name[0]);
printf("%s\n", &(name[0]));
printf("%s\n", &name);
They all print the same thing, so use the clearest one - the first! (And the last will rightly give you a warning on some compilers!)

In fact you can print the pointer values, and you'll see they are all the same:
C
printf("%p\n", name);
printf("%p\n", &name[0]);
printf("%p\n", &(name[0]));
printf("%p\n", &name);
 
Share this answer
 
v2
Comments
Greg Utas 18-Mar-22 9:00am    
Just writing name here is certainly more idiomatic and what you'd usually see.
#include <stdio.h>
int main()
{
  char name[20];  
  printf("Enter Your Name : "); 
  scanf("%s",&name[20]);  
  printf("Your name is : %s", name);  
return 0;

 }</stdio.h>
 
Share this answer
 
Comments
jeron1 18-Mar-22 10:34am    
scanf("%s",&name[20]);

You sure about that?
Member 15553638 18-Mar-22 11:59am    
No 😮
In scanf statement remove [20]

scanf("%s",&name);
C++
#include <stdio.h>
int main()
{
  char name[20];  // Here, 20 is the size of array name
  printf("Enter Your Name : "); 
  scanf("%s",&name char[20]);  // here 20 is a position in array name
                               // Position 20 is 1 after the end of array
  printf("Your name is : %s", name);  
 }
 
Share this answer
 

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