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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:24:25+00:00 2026-06-10T02:24:25+00:00

Consider following case: #include<stdio.h> int main() { char A[5]; scanf(%s,A); printf(%s,A); } My question

  • 0

Consider following case:

#include<stdio.h>
int main()
{
    char A[5];
    scanf("%s",A);
    printf("%s",A);
}

My question is if char A[5] contains only two characters. Say “ab”, then A[0]='a', A[1]='b' and A[2]='\0'.
But if the input is say, “abcde” then where is '\0' in that case. Will A[5] contain '\0'?
If yes, why?
sizeof(A) will always return 5 as answer. Then when the array is full, is there an extra byte reserved for '\0' which sizeof() doesn’t count?

  • 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-10T02:24:27+00:00Added an answer on June 10, 2026 at 2:24 am

    If you type more than four characters then the extra characters and the null terminator will be written outside the end of the array, overwriting memory not belonging to the array. This is a buffer overflow.

    C does not prevent you from clobbering memory you don’t own. This results in undefined behavior. Your program could do anything—it could crash, it could silently trash other variables and cause confusing behavior, it could be harmless, or anything else. Notice that there’s no guarantee that your program will either work reliably or crash reliably. You can’t even depend on it crashing immediately.

    This is a great example of why scanf("%s") is dangerous and should never be used. It doesn’t know about the size of your array which means there is no way to use it safely. Instead, avoid scanf and use something safer, like fgets():

    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (‘\0’) is stored after the last character in the buffer.

    Example:

    if (fgets(A, sizeof A, stdin) == NULL) {
        /* error reading input */
    }
    

    Annoyingly, fgets() will leave a trailing newline character (‘\n’) at the end of the array. So you may also want code to remove it.

    size_t length = strlen(A);
    if (A[length - 1] == '\n') {
        A[length - 1] = '\0';
    }
    

    Ugh. A simple (but broken) scanf("%s") has turned into a 7 line monstrosity. And that’s the second lesson of the day: C is not good at I/O and string handling. It can be done, and it can be done safely, but C will kick and scream the whole time.

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

Sidebar

Related Questions

Consider the following 2 code snippets: Case 1: #include <iostream> int main() { int
Consider the following test case: #define _GNU_SOURCE #include <stdio.h> #include <stdarg.h> void test(char **outa,
Consider the following test case: public class Main { static int a = 0;
Consider the following programs: // http://ideone.com/4I0dT #include <limits> #include <iostream> int main() { int
Please consider the following simple use case: public class Foo { public virtual int
Consider the following C#: // C# .net switch(x) { case 1: for(int i =
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
Consider the following case: void Set(const std::function<void(int)> &fn); void Set(const std::function<void(int, int)> &fn); Now
Consider the following code: #include <algorithm> #include <iostream> #include <vector> struct A { int

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.