Right Click to Search

Thursday, January 14, 2025

Scanf >> Reading or Accepting String From User in C


Scanf : Reading or Accepting String From User in C

Syntax for Accepting String :
scanf("%s",a);
Live Example :
#include<stdio.h>
void main()
{
char name[20];
printf("\nEnter the Name : ");
scanf("%s",name);
}

Rules / Facts :
  1. Use%s for Accepting String
  2. scanf accepts only String before Space
Must Visit Here !!!
Reading String with spaces using scanf


Tags / Keywords : | , ,

Stumble
Delicious
Technorati
Twitter
Facebook

2 Comments:

Vishal Mishra said...

its &name in scanf....

Priteh Taral said...

No !! No Whatever I have Written is Correct ..

scanf needs a pointer to the variable that will store input. In the case of a string, you need a pointer to an array of characters in memory big enough to store whatever string is read in. When you declare something like char var[100], you make space for 100 chars with name[0] referring to the first char and name[99] referring to the 100th char. The array name by itself evaluates to exactly the same thing as &name[0], which is a pointer to the first character of the sequence, exactly what is needed by scanf. So all you need to do is scanf("%s", name);

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates