I know that %s is a string of characters, but I don\'t know how to use it. Can anyone provide me a very basic example of how its used and how its different from char ?
%s will get all the values until it gets NULL i.e. '\0'.
char str1[] = "This is the end\0";
printf("%s",str1);
will give
This is the end
char str2[] = "this is\0 the end\0";
printf("%s",str2);
will give
this is
void myfunc(void)
{
char* text = "Hello World";
char aLetter = 'C';
printf("%s\n", text);
printf("%c\n", aLetter);
}
Here goes:
char str[] = "This is the end";
char input[100];
printf("%s\n", str);
printf("%c\n", *str);
scanf("%99s", input);
%s is the representation of an array of char
char string[10] // here is a array of chars, they max length is 10;
char character; // just a char 1 letter/from the ascii map
character = 'a'; // assign 'a' to character
printf("character %c ",a); //we will display 'a' to stout
so string is an array of char we can assign multiple character per space of memory
string[0]='h';
string[1]='e';
string[2]='l';
string[3]='l';
string[4]='o';
string[5]=(char) 0;//asigning the last element of the 'word' a mark so the string ends
this assignation can be done at initialization like char word="this is a word" // the word array of chars got this string now and is statically defined
toy can also assign values to the array of chars assigning it with functions like strcpy;
strcpy(string,"hello" );
this do the same as the example and automatically add the (char) 0 at the end
so if you print it with %S printf("my string %s",string);
and how string is a array we can just display part of it
// the array one char
printf("first letter of wrd %s is :%c ",string,string[1] );
For both *printf
and *scanf
, %s
expects the corresponding argument to be of type char *
, and for scanf
, it had better point to a writable buffer (i.e., not a string literal).
char *str_constant = "I point to a string literal";
char str_buf[] = "I am an array of char initialized with a string literal";
printf("string literal = %s\n", "I am a string literal");
printf("str_constant = %s\n", str_constant);
printf("str_buf = %s\n", str_buf);
scanf("%55s", str_buf);
Using %s
in scanf
without an explcit field width opens the same buffer overflow exploit that gets
did; namely, if there are more characters in the input stream than the target buffer is sized to hold, scanf
will happily write those extra characters to memory outside the buffer, potentially clobbering something important. Unfortunately, unlike in printf
, you can't supply the field with as a run time argument:
printf("%*s\n", field_width, string);
One option is to build the format string dynamically:
char fmt[10];
sprintf(fmt, "%%%lus", (unsigned long) (sizeof str_buf) - 1);
...
scanf(fmt, target_buffer); // fmt = "%55s"
EDIT
Using scanf
with the %s
conversion specifier will stop scanning at the first whitespace character; for example, if your input stream looks like
"This is a test"
then scanf("%55s", str_buf)
will read and assign "This"
to str_buf
. Note that the field with specifier doesn't make a difference in this case.