The static array will exist for the life of the program and is therefore accessible after the function returns. My compiler accepted it and execution produced the expected result when the printf statement was placed in main.
Probably unrelated to your real question but the format string passed to printf should also have a \n after the %s to insure the output is visible. > -----Original Message----- > From: IBM Mainframe Discussion List [mailto:[email protected]] On > Behalf Of Frank Swarbrick > Sent: Monday, June 19, 2017 2:26 PM > To: [email protected] > Subject: Simple (?) C question > > I know there are at least a few C developers here, so I was wondering if you could answer a > question. Is the following valid C? (I'm not asking if one should actually do it; only if its > valid at all.) > char *get_static_string(void) { > static char str[81] = "This is a statically allocated C string"; > return str; > } > > > printf("%s", get_static_string()); > > > I don't have a C compiler available at work else I'd try it myself. ---------------------------------------------------------------------- For IBM-MAIN subscribe / signoff / archive access instructions, send email to [email protected] with the message: INFO IBM-MAIN
