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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:39:41+00:00 2026-05-26T22:39:41+00:00

I want to read input from user using C program. I don’t want to

  • 0

I want to read input from user using C program. I don’t want to use array like,

char names[50];

because if the user gives string of length 10, then the remaining spaces are wasted.

If I use character pointer like,

char *names;

then I need to allocate memory for that in such a way of,

names = (char *)malloc(20 * sizeof(char));

In this case also, there is a possibility of memory wastage.

So, what I need is to dynamically allocate memory for a string which is of exactly same as the length of the string.

Lets assume,

If the user input is "stackoverflow", then the memory allocated should be of 14 (i.e. Length of the string = 13 and 1 additional space for ‘\0’).

How could I achieve this?

  • 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-26T22:39:41+00:00Added an answer on May 26, 2026 at 10:39 pm

    Read one character at a time (using getc(stdin)) and grow the string (realloc) as you go.

    Here’s a function I wrote some time ago. Note it’s intended only for text input.

    char *getln()
    {
        char *line = NULL, *tmp = NULL;
        size_t size = 0, index = 0;
        int ch = EOF;
    
        while (ch) {
            ch = getc(stdin);
    
            /* Check if we need to stop. */
            if (ch == EOF || ch == '\n')
                ch = 0;
    
            /* Check if we need to expand. */
            if (size <= index) {
                size += CHUNK;
                tmp = realloc(line, size);
                if (!tmp) {
                    free(line);
                    line = NULL;
                    break;
                }
                line = tmp;
            }
    
            /* Actually store the thing. */
            line[index++] = ch;
        }
    
        return line;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to read 2 values from user input using lisp. I want to
What I want my program to do is to read an input text from
I'm using the following code to try to read an input from user and
I want to read the file path from html input type=file (the entry selected
I want to read UTF-8 input in Perl, no matter if it comes from
What I want to do is the following: read in multiple line input from
I want to read and write from serial using events/interrupts. Currently, I have it
Okay, so ultimately I want a program where the user can input simple data
I'm making a program that takes names from the user seperated by commas. The
The user should input some file names in the command line and the program

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.