Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:21:18+00:00 2026-06-01T08:21:18+00:00

I know that %s is a string of characters, but I don’t know how

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:21:19+00:00Added an answer on June 1, 2026 at 8:21 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that .NET String class uses such behaviour, but I can't find any
I know that for an integer, you can use: int value; I tried: string
I know that using Dim currUser As String = Request.ServerVariables(LOGON_USER) returns the Domain\Username, but
I know that I can pass a string as the second parameter to the
I know that in J2ME I can get a byte[] object from a String
Does anyone know if it is possible to create a single connection string that
We know that string is a reference type , so we have string s=God
date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
If I have a sting such as AppNamespace.SomeObject.ClassName and I know that the string
I have this string that has illegal chars that I want to remove but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.