#include
int main(void)
{
int i,j,k;
char st;
printf(\"enter string\\n\");
scanf(\"%s\", st);
char st
is a single character. Judging by the rest of your code, you probably intended to declare an array of characters:
char st[80];
As others have mentioned you want to create an array:
Change char st;
to char st[10];
or whatever size array you want.
With the above change st
is an array that has 10 individual elements that can hold a single char value.
You have a type mis-match.
scanf
is not type safe, You need to provide proper type. scanf
enables you to get data from the input and you need to tell it what is the type of the data you want it to read. You ask it to read a string by specifying %s
by provide it with a character variable.
You need an array:
#define MAX_LENGTH 256
char st[MAX_LENGTH];
As @Jerry rightly points out, You can just simple avoid all the hassle by using:
getline() instead of using scanf
char st is a character i.e it will store only one character try it like this
main()
char st [100];
scanf("%s",st);
just change these three lines and it will work it will except only single word strings
here you have declared the function as an int that is int main() keep it as main() and it will work
st is type of char &st is type of char * Take care of the difference. BTW, only one char cannot be used to store a string. Use char[] array.
Use char *st;
or an array like char st[50];
.
When you complete the usage of a char pointer, you should deallocate the memory used by the pointer. This can be done using free(st);
function.
EDIT : As you are printing string and if you are using pointer, you can do:
printf("the entered string is %s\n",st);
printf("the entered string is %s\n",*st); // This will work in both cases, if you use char *st or char st[50]