11

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 it's different from char?


All the examples given below use arrays which hasn't been taught yet, so I'm assuming I can't use %s yet either.

E_net4
  • 27,810
  • 13
  • 101
  • 139
nofe
  • 408
  • 2
  • 5
  • 16
  • possible duplicate of [How do I print a non-null-terminated string using printf?](http://stackoverflow.com/questions/2137779/how-do-i-print-a-non-null-terminated-string-using-printf) –  Apr 02 '12 at 17:27
  • 1
    @nofe `%c` is for a single character while `%s` is for a series of characters null-terminated. What specifically you didn't understand ? – Mahesh Apr 02 '12 at 17:29

6 Answers6

9

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
4

Here goes:

char str[] = "This is the end";
char input[100];

printf("%s\n", str);
printf("%c\n", *str);

scanf("%99s", input);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
2

%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

Froyo
  • 17,947
  • 8
  • 45
  • 73
1
void myfunc(void)
{
    char* text = "Hello World";
    char  aLetter = 'C';

    printf("%s\n", text);
    printf("%c\n", aLetter);
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

%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]  );
Freaktor
  • 700
  • 1
  • 10
  • 21
-2
#include <stdio.h>
int main() {
    printf("%s", "HELLO WORLD! ");
    return 0;
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 10 '22 at 00:25
  • 2
    This adds nothing new or additional than answers posted ten years ago. – Blastfurnace Nov 10 '22 at 00:43