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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:21:40+00:00 2026-05-25T17:21:40+00:00

When we run this piece of code, it works normally and prints string constant

  • 0

When we run this piece of code, it works normally and prints string constant on the screen:

char *someFun(){
    char *temp = "string constant";
    return temp;
}
int main(){
    puts(someFun());
}

But when we run the following similar code, it won’t work and print some garbage on screen:

char *someFun1(){
    char temp[ ] = "string";
    return temp;
}
int main(){
    puts(someFun1());
}

What is the reason behind it? Essentially, both functions do similar things (i.e. return a “string”), but still they behave differently. Why is that?

  • 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-25T17:21:41+00:00Added an answer on May 25, 2026 at 5:21 pm
    char *temp = "string constant";
    

    string constant literal resides on read only segment. It gets deallocated at program termination. So, you can have a reference pointing to it.

    char temp[ ] = "string";
    

    string is copied to temp which resides on stack. As the function returns, unwinding of stack begins which de-allocates the variables in the function scope. But you are returning a reference to it which no longer exists on stack and hence you are getting garbage. But sometimes you may still get the correct result but you should not rely on it.

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

Sidebar

Related Questions

I have this simple piece of code in c++: int main(void) { string text
when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
I have written this piece of code public class Test{ public static void main(String[]
I am trying to understand how this piece of self-replicating code works (found here
So I found this piece of code and it obviously works (as it has
When I run this code the selected item is not visible. I've already tried
when I run this JS in FF or Safari it works just right but
So I run this Windows Server 2008 security update and this code block is
When I run this code, About half-way through the concatenation loop, $xml becomes null
When I run this code on my computer with the help of Google App

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.