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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:30:17+00:00 2026-05-13T18:30:17+00:00

When allocating Strings on the heap (with ‘malloc’), and initializing them with the standard

  • 0

When allocating Strings on the heap (with ‘malloc’),
and initializing them with the standard input (using ‘fgets),
an unnecessary newline appears between them when trying to print them (with ‘printf’ and %s formatting).
for example:

main()
{
    char *heap1;
    char *heap2;

    heap1=malloc(10);
    heap2=malloc(10);
    fgets(heap1,10,stdin);
    fgets(heap2,10,stdin);
    printf("%s%s",heap1,heap2);
}

with input “hi1\nhi2” produces “hi1\nhi2”.
compiled using gcc 4.3.4 for Debian.

  • 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-13T18:30:17+00:00Added an answer on May 13, 2026 at 6:30 pm

    fgets also reads the ‘\n’ (newline) character. You should remove it if you don’t want it to print like that.

    heap1[strlen(heap1) - 1] = '\0';
    

    after you read the contents of heap1.

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

Sidebar

Related Questions

In order to help avoid unnecessary heap allocations of strings inside a class, I
When allocating a new BSTR with SysAllocString via a wchar_t* on the heap, should
Is it necessary to check for nullptr after allocating an object using gcnew?
I am allocating memory for array of pointers to structure through malloc and want
When I run the below mentioned code using NetBeans, the allocated heap size graph
I am dynamically allocating a struct which has a different struct as a member:
I'm curious if allocating a buffer on the stack is required to have correct
I am allocating memory for an object and if a particular statement following the
I' having a problem allocating a structure in a function. Here is the code(I'm
Background: I'm allocating my own machine contexts and stacks with the getcontext(3) / makecontext(3)

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.