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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:06:35+00:00 2026-06-10T01:06:35+00:00

I have recently learnt that its possible to change values of constants in c

  • 0

I have recently learnt that its possible to change values of constants in c using a pointer but its not possible for string literals. Possibly the explanation lies in the fact that constants and other strings are allocated space in modifiable region in space whereas string literals gets in non-modifiable region in space (possibly code segment). I have written a program that display addresses for these variables. Outputs are shown as well.

#include <stdio.h>
int x=0;
int y=0;

int main(int argc, char *argv[])
{
    const int a =5;
    const int b;
    const int c =10;
    const char *string = "simar";   //its a literal, gets space in code segment
    char *string2 = "hello";
    char string3[] = "bye"; // its array, so gets space in data segment
    const char string4[] = "guess";
    const int *pt;
    int *pt2;

    printf("\nx:%u\ny:%u Note that values are increasing\na:%u\nb:%u\nc:%u Note that values are dec, so they are on stack\nstring:%u\nstring2:%u Note that address range is different so they are in code segment\nstring3:%u Note it seems on stack as well\nstring4:%u\n",&x,&y,&a,&b,&c,string,string2,string3,string4); 
}

enter image description here

Please explain where exactly these variables get space??
Where do globals get space, where do constants get and where does string literals get??

  • 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-10T01:06:37+00:00Added an answer on June 10, 2026 at 1:06 am

    “Possible” is over-stating the case.

    You can write code that attempts to modify a const object (for example by casting its address to a pointer-to-non-const type). You can also write code that attempts to modify a string literal.

    In both cases your code has undefined behavior, meaning that the standard doesn’t care what happens. The implementation can do what it likes, and what happens is usually an accidental side-effect of something else that does matter. You cannot rely on the behavior. In short, that code is wrong. This is true both of objects defined as const, and of string literals.

    It may be that on a particular implementation, the effect is to change the object or the literal. It may be that on another implementation, you get an access error and the program crashes. It may be that on a third implementation, you get one behavior sometimes and the other at other times. It may be that something entirely different happens.

    It’s implementation-specific where variables get their space, but in a typical implementation:

    • x and y are in a modifiable data segment
    • a is on the stack. If it weren’t for the fact that you take its address, then the variable storage could be optimized away entirely, and the value 5 used as an immediate value in any CPU instructions that the compiler emits for code that uses a.
    • b I think is an error — uninitialized const object. Maybe it’s allowed, but the compiler probably ought to warn.
    • c is on the stack, same as a.
    • the literals “simar” etc are all either in a code segment, a read-only data segment, or a modifiable data segment if the implementation doesn’t bother with rodata.
    • string3 and string4 are arrays on the stack. Each is initialized by copying the contents of a string literal.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started to learn Objective-C and write my tests using OCUnit that
My app is ready for submission now but I have recently learnt about Licensing.
Recently scalaz caught my eye. It looks very interesting, but I have not found
I am using zepto library for my mobile web site. I have recently learnt
I have recently began using C# to invoke powershell scripts, with some success :)
I have a question that may have been answered over 9000 times before but
I have been using php for a while but haven't taken the time to
So I've recently started learning Scala and have been using graphs as sort of
Recently I have started working on a program that will monitor the packets of
REST resources from database tables I have recently learnt things about REST (many of

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.