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 6738101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:19:38+00:00 2026-05-26T11:19:38+00:00

I’m new to C. I need to know how to ask for the user

  • 0

I’m new to C. I need to know how to ask for the user input, which could be an arbitrary number of words, and then put the characters into an array.

I know this shouldn’t be too hard to answer, but Google is just confusing me.

Thanks in advance

  • 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-26T11:19:38+00:00Added an answer on May 26, 2026 at 11:19 am

    If I understand your problem correctly, what you need is to read user input of some unknown size, thus store this information into a char array, right? If this is the case, one possible solution would be to have an char array with which should be allocated to a default fixed size, this dynamically reallocate it’s size on the fly.

    Once looping over the characters of the input, while verifying that you haven’t hit an EOF, you should append the char to the array. The trick is then to check whether the array is large enough to hold the characters from the user’s input. If not, you have to reallocate the array’s size.

    A sample implementation of a solution could look somewhat like this:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned int len_max = 128;
        unsigned int current_size = 0;
    
        char *input = malloc(len_max);
        current_size = len_max;
    
        printf("\nEnter input:");
    
        if(input != NULL) {
            int c = EOF;
            unsigned int i = 0;
    
            // Accept user input until hit EOF.
            while (( c = getchar() ) != EOF) {
                input[i++] = (char)c;
    
                // If reached maximize size, realloc size.
                if (i == current_size) {
                    current_size = i + len_max;
                    input = realloc(input, current_size);
                }
            }
    
            // Terminate the char array.
            input[i] = '\0';
    
            printf("\nLong String value:%s \n\n",input);
    
            // Free the char array pointer. 
            free(input);
        }
    
        return 0;
    }
    

    Performance wise, I am not all to sure this may be the best solution, but I hope this may aid you on solving your problem.

    Kind regards
    ~ E.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.