Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read strings from the console ? 2

Status
Not open for further replies.

fixerz

Programmer
Feb 23, 2014
1
i need to read the input string entered on the console to be stored .
 
STRINGS
Strings are a collection of characters. Strings are placed between "quotes”. by default , a null character is stored at the end of every string that tells that compiler that it is the end of string. Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char datatype.
char s[5];

In C, string can be initialized in different number of ways.
char s[]="abcd";
OR,
char s[5]="abcd";

We can also read the string as an input from the console.
char s[20];
// we are initializing an array of name "s" and datatype is character and size of array is 20.
scanf("%s",s);
// %s is used to tell the scan function that a String type is to read as input.
We also have another method to read string input from the user through the console.
we use two functions viz., gets() and puts()
gets() and puts() function handle string, both these functions are defined in "stdio.h" header file.

for further reference pls visit
 
Would be better to use fgets instead of gets. gets has no limit on the string size so it is possible to corrupt the heap/stack/data if the string goes out of bounds.
 
do you mean get user input?
if so use scanf

example
C:
#include <stdio.h>

int main()
{
char input;
scanf("%s", input);//the %s depends if you want to get the input as a int then do %d etc
printf("You Entered: %s", input);
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top