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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:30:04+00:00 2026-06-01T05:30:04+00:00

Disclaimer: This question is strictly academic. The example I’m about to give is probably

  • 0

Disclaimer: This question is strictly academic. The example I’m about to give is probably bad style.

Suppose in C I write a subroutine of this form:

char *foo(int x)
{
    static char bar[9];

    if(x == 0)
        strcpy(bar, "zero");
    else
        strcpy(bar, "not zero"),

    return bar;
}

Then, elsewhere, I use foo as follows:

printf("%i is %s\n", 5, foo(5));

My mental model of pointers and static variables predicts that, in practice, the output of this printf will be

5 is not zero

…but is this actually required by the C standard, or am I in nasal demon territory?

To make things even worse, what about something like

strcpy(foo(5), "five");

My mental model says this should “work” unless it’s explicitly illegal, though it’s somewhat pointless since it doesn’t affect the output of foo. But again, is this actually defined by the standard?

  • 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-01T05:30:06+00:00Added an answer on June 1, 2026 at 5:30 am

    What you’ve written is OK; there are no nasal demons awaiting you. Even the strcpy() example is ‘OK’ because you are not trampling beyond the bounds of the array. That ‘OK’ is in quotes because it is not a good idea, but as written, there is no out of bounds memory access and so no undefined behaviour. The static data in the function is there throughout the program’s lifetime, containing the last value that was written to it.

    There could be problems if you try:

    printf("%i is %s but %i is %s\n", 5, foo(5), 0, foo(0));
    

    You will get the wrong answer for one of the two numbers, but it is not defined which will be the wrong answer.

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

Sidebar

Related Questions

Disclaimer This is not strictly a programming question, but most programmers soon or later
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer When I wrote this question I was wrong about the behavior of the
DISCLAIMER: This is not a real-world example. It is just a theoretical question of
Disclaimer: I tried to search for similar question, however this returned about every C++
Disclaimer: This question is not about fixing visual studio So, I've used VSS for
Disclaimer: This is a follow-on question from my other question about NServiceBus which was
Disclaimer This is not a question about whether we should be escaping for database
Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer: This is for a homework assignment, but the question is not regarding the

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.