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.
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.
#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;
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.