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

  • Home
  • SEARCH
  • 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 476887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:32:07+00:00 2026-05-13T00:32:07+00:00

I know in C you can declare a string and the number of characters

  • 0

I know in C you can declare a string and the number of characters like below,

char mystring[50];

with ’50’ being the number of characters.

However, what is proper procedure if the user is going to be inputting the contents of the string (via scanf(“%s”, mystring);)? Do I leave it as,

char mystring[0];

leaving it as ‘0’ since I have no clue how many characters the user will input?

Or do I do,

char mystring[400];

giving up to 400 characters for the user to input?

  • 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-05-13T00:32:08+00:00Added an answer on May 13, 2026 at 12:32 am

    You’ve hit upon the exact problem with scanf() and %s – what happens when you don’t know how much input there is?

    If you try running char mystring[0];, your program will compile just fine. But you will always segfault. You’re creating an array of size 0, so when you try to place something into that array, you will immediately go out of bounds for your string (since no memory will have been allocated) – which is a segfault.

    So, point 1: you should always allocate a size for your string. I can think of very few circumstances (okay, none) where you would want to say char mystring[0] rather than char *mystring.

    Next, when you use scanf, you never want to use the “%s” specifier – because this will not do any bounds-checking on the size of the string. so even if you have:

    char mystring[512];
    scanf("%s", mystring);
    

    if the user enters more than 511 characters (since the 512th is \0), you will go out of the bounds of your array. The way to remedy this is:

    scanf("%511s", mystring);
    

    This is all to say that C doesn’t have a facility to automatically resize a string if there is more input than you’re expecting. This is the kind of thing you have to do manually.

    One way to deal with this is by using fgets().

    You could say:

    while (fgets(mystring, 512, stdin))
    {
       /* process input */
    }
    

    You may then use sscanf() to parse mystring

    Try the above code, with a string of length 5. After 4 characters have been read, that code loops again to retrieve the rest of the input. “Processing” could include code to re-allocate a string to be a bigger size and then append the newest input from fgets().

    The above code isn’t perfect – it would make your program loop and process any infinite string length, so you might want to have some internal hard limit on that (eg, loop a maximum of 10 times).

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

Sidebar

Related Questions

I know I can declare an array of string in XAML like this: <x:Array
So, In Java, you know how you can declare integers like this: int hex
I know that in PHP you can declare a variable A and then if
How to know the size of a declared variable in GMP??or how can we
I want to know can we have a JPanel with a Layout other than
This is something I know can be done somehow , because I've done it
I need to know can we add image in TextArea through StyleableTextField htmlText because
I have a query that I know can be done using a subselect, but
Sometimes, I'll end up having to catch an exception that I know can never
You can know if the event stack is empty calling the gtk.events_pending() method, 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.