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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:09:06+00:00 2026-06-17T19:09:06+00:00

A simple program below with malloc and scanf with %s to get a string

  • 0

A simple program below with malloc and scanf with %s to get a string as below, gives me an output I cannot comprehend. While I have ‘malloced’ only 5 bytes, my input string has exceeded the above size but no segmentation fault.
Is scanf overiding malloc allocation?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  char * name;
  int SZSTRING;

    printf("Enter size of name :");  
    scanf("%d", &SZSTRING);
    name = (char*) malloc ((SZSTRING + 1) * sizeof(char));

    printf("Enter name :");
    scanf("%s", name);
    printf("len of 'name' : %d\n",strlen(name));

  printf("name final: \"%s\"\n",name);
  free(name);

return 0;
}

Output:

OptiPlex-380:~/gsa/compile$ gcc -o try try.c 
OptiPlex-380:~/gsa/compile$ ./try 
Enter size of name :4
Enter name :qwertyui
len of 'name' : 8
name final: "qwertyui"

I noticed one more thing here: with

    //scanf("%s", name);

output shows

len of 'name'= 0

and ‘malloced’ locations were actually memset to NULL. But its calloc and not malloc which initialises the allocated bytes to 0 as per man-page???

  • 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-17T19:09:07+00:00Added an answer on June 17, 2026 at 7:09 pm

    It may seem to “work” but that’s just because you got lucky. When I run your code on one compiler it “works” on a different one it crashes because of a heap corruption. Your best bet if you want to use scanf() is to allow scanf() to allocate the memory for you:

    scanf("%ms", &name); // compiled with -std=c99 this will allocate the correct amount
                        // of memory for you. You can use "%as" if you're using -std=c89 
    

    Also keep in mind that scanf() has a return value (which tells you number of input items successfully matched and assigned) and it’s important to check that to know if it worked.

    While we’re at good practices, you shouldn’t typecast the return value of malloc()

    Another alternative, not using scanf(), is to use fgets() instead:

    fgets( name, SZSTRING, stdin);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates
I have the below simple program using boost threads, what would be the changes
I have a simple question: Why the program below does not write the whole
I get a seg fault for the simple program below. It seems to be
I have a very simple Java program (see below). The GridLayout has 20 rows
I'm practising with C, writing simple programs. The little program below should just get
The code pasted below is a simple JSF program, with an idea of having
Below I have written a sample program that I have written to learn about
below i have a code that runs in most of my simple programs ..
I have a simple program. I want to set the path and some properties

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.