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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:40:05+00:00 2026-05-27T05:40:05+00:00

In K&R section 5.8, there is a function month_name() which uses an internal static

  • 0

In K&R section 5.8, there is a function month_name() which uses an internal static char pointer array, and makes a point to mention that this is an “ideal application for an internal static array.” The function is below:

/* month_name: return name of n-th month */
char *month_name(int n)
{
    static char *name[] = {
        "Illegal month",
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
    };
    return (n < 1 || n > 12) ? name[0] : name[n];
}

My understanding is that static when applied to internal variable means that the variable hangs around after function returns, and still exists for the next time the function is entered. So the only theory I could come up for why static is used here, is that maybe if static wasn’t used, the char *name[] would be re-initialized every single time the function was entered, which would be inefficient. I did try the function with the static keyword removed, and it still seemed to work fine. So I wanted to test my theory that maybe the array was being created more than once. I created this test, which may be totally worthless, but it’s the best I could come up with:

int main(void)
{
    printf("%s\n", month_name(1)); // print string
    printf("%d\n", month_name(1)); // print address of string
    printf("%s\n", month_name(1)); // print string
    printf("%d\n", month_name(1)); // print address of string
}

My theory was that if the string array was being duplicated inefficiently, the address would change for the second function call… but it remained the same. So I even went a step further and tried to initialize another character array just to shift the allocation a bit (again, this may be totally worthless based on my limited knowledge of how allocation works):

int main(void)
{
    printf("%s\n", month_name(1)); // print string
    printf("%d\n", month_name(1)); // print address of string
    char dummy[] = "dummy";
    printf("%s\n", month_name(1)); // print string
    printf("%d\n", month_name(1)); // print address of string
}

But, the address of “January” is still the same for both function calls, even with the char *name[] in the month_name function NOT being declared as static.

So… couple questions.

1) What is the point of it being static? Why is it an ideal application for internal static array?

2) Is there any merit to the logic behind my test above?

Thanks!

  • 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-27T05:40:05+00:00Added an answer on May 27, 2026 at 5:40 am

    If it’s not static it’s on the stack. Since the function is returning a char * pointer to it, that’s kind of important and it needs to be done that way.

    If it’s not static, outside of the function you have a pointer to a stack variable that is no longer valid. Your “test” isn’t showing this because the memory hasn’t yet been reused. Make no mistake – it will be at some point.

    Edit: Jim brings up a good point in the comments below that I didn’t think about initially. Since these are string literals, they are already in read-only memory. I think that using static here simply keeps from re-initializing the char *name[] array each time. The initial answer above does apply to other data types, but in this case I don’t think it does.

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

Sidebar

Related Questions

In K&R Section 5.10, in their sample implementation of a grep -like function, there
When I Jump to section 1 & 2 everything works fine, and the browsers
In section 10.4, The Definitive ANTLR reference tells you to override mismatch() & recoverFromMismatchedSet()
In K&R ANSI C book, section A.7.4.5 (Unary Minus Operator) it is stated: ...
I'm reading through K&R and came to the small section on register variables, and
I'm working on something where there are 2 links which triggers some Ajax. However
I am appending data output into this div section. Within this <div> section, there
If a javascript function is declared anonymously is there any way to override it
UPDATE: I added this to my unload function: $(window).resize(function(){ var pageheight = $(window).height(); $('section').css('height',
In this C++ tutorial , in the section titled Standard exceptions, there is this

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.