INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

how to read strings from the console ?

how to read strings from the console ?

(OP)
i need to read the input string entered on the console to be stored .

RE: how to read strings from the console ?

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 http://www.fixoncloud.com/Home/LoginValidate/OnePr...

RE: how to read strings from the console ?

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.

RE: how to read strings from the console ?

do you mean get user input?
if so use scanf

example

CODE --> 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;
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close