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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:47:11+00:00 2026-05-23T16:47:11+00:00

How are string literals compiled in C? As per my understanding, in test1, the

  • 0

How are string literals compiled in C? As per my understanding, in test1, the string “hello” is put in data segment by compiler and in the 2nd line p is assigned that hard-coded virtual address. Is this correct? and that there is no basic difference between how test1 works and how test2 works.

Some code:

#include <stdio.h>

test1();
test2();
test3();

main()
{
    test1();
    test2();
    //test3();
}

test1()
{
    char *p;
    p="hello";
}

test2()
{
    char *p="hello";
}

test3()
{
    char *p;
    strcpy(p,"hello");
}

any reference from C standard will be greatly appreciated, so that I can understand this thing in depth from compiler point of view.

  • 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-23T16:47:12+00:00Added an answer on May 23, 2026 at 4:47 pm

    From the C standard point of view there’s no particular requirement about where the literal string will be placed. About the only requirements about the storage of string literals are in C99 6.4.5/5 “String literals”:

    • “an array of static storage duration and length just sufficient to contain the sequence” , which means that the literal will have a lifetime as long as the program.
    • “It is unspecified whether these arrays are distinct provided their elements have the appropriate value”, which means the various "hello" literals in your example may or may not have the same address. You can’t count on either behavior.
    • “If the program attempts to modify such an array, the behavior is undefined”, which means that you can’t change the string literal. One many platforms this is enforced (if you attempt to do so, the program will crash). On some platforms, the change may appear to work so you can’t count on the bug being readily evident.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A recent question about string literals in .NET caught my eye. I know that
If possible one that supports at least spell checking: C# string literals HTML content
I'm trying to match chunks of JS code and extract string literals that contain
Are there any languages that support easily extending the compiler to support new literals
I have few questions about string literals in C++. char *strPtr = "Hello" ;
I've always understood that the storage of string literals is implementation defined, so the
When logging, you always get entangled in string literals. I solved that nicely for
Are the string literals we use inside functions automatic variables? Or are they allocated
I always try to avoid to return string literals, because I fear they aren't
I am interested in where string literals get allocated/stored. I did find one intriguing

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.