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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:12:39+00:00 2026-06-08T17:12:39+00:00

This is the third program in C language. This program is to demonstrate string

  • 0

This is the third program in C language.
This program is to demonstrate string concept.

From study I know that string at its end has a null character ‘\0’ to terminate the string.

I wrote that code :

main()
{
      char  name[8];

      strcpy(name, "Mahmoud");
      printf("The contents of name are %s\n", name);
      getchar();
}

In this code I declare array of type char to save string in it. My name “Mahmoud” is 7 chars and I declare name with size 8 which is 7 for “Mahmoud” and 1 for ‘\0’, and it works correctly.

But in the following code:

main()
{
      char  name[8];

      strcpy(name, "MahmoudEmam");
      printf("The contents of name are %s\n", name);
      getchar();
}

When I display name the output is “MahmoudEmam”, although the size of name is 8.

How does it do ?

  • 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-08T17:12:40+00:00Added an answer on June 8, 2026 at 5:12 pm

    The fact you see the full name displayed is undefined behavior. Basically, you are copying more than 7 characters and a terminating 0 (total of 8 characters) into storage reserved only for 7 characters and a terminating 0.

    It just happens to work, but could as easily cause your program to crash.

    This is why C is considered a lower level programming language, or, as the phrase used to go many years ago, a high level assembly programming language.

    You the programmer have to check the length of the copy operation’s target, using a construct like sizeof(name), and make sure what you are copying does not override that space. You must also keep in mind for
    string termination, enough space + 1 to account for the terminating ‘\0’.

    Don’t forget that using sizeof with a string allocated with malloc will return a value of 4 for a 32-bit pointer, or the pointer length of your hardware. In that case, you’ll have to rely on strlen to get the buffer length, or store what size was used to malloc the string.

    Finally, when passing a string pointer to a function, writing the function to have a buffer length is very helpful. You cannot get the true buffer length. strlen only returns the string length, not the buffer size actually pointed to by the pointer.

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

Sidebar

Related Questions

I have some third-party Javascript that has statements like this: FOO = function() {
I'm invoking third party code method.invoke(null, args); and it has a classloader that loads
Anyone know how to do this without using a third party program? If there
I'm working on a third-party program that aggregates data from a bunch of different,
THIRD EDIT: I now believe that this problem is due to a SOAP version
I have a third party web service that returns this xml <book> <release_date>0000-00-00</release_date> </book>
I have a third party SDK in C# which has class names like this:
EDIT: Purpose of this Website: Its called Utopiapimp.com. It is a third party utility
When creating dlls (Add-ins) for a third party program that loads Native DLLs dynamically,
My company currently runs a third-party simulation program (natural catastrophe risk modeling) that sucks

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.