In-depth analysis: named Constants

Source: Internet
Author: User

Content from: DS Plan C Deep Learning Project

Welcome to join and make progress together

Visit this forum

A named constant in C is a variable modified with Const.

This article will go deep into the nature, storage location, whether it can be changed, and how to change the name constant.

First, declare the environment I tested: Linux Enterprise 5.0.

 

Test non-static global variables

The test procedure is as follows:

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> const int g_int_a = 1000; <br/> int main () <br/> {<br/> printf ("g_int_a is % d. /n ", g_int_a); </P> <p> printf (" g_int_a is % d. /n ", g_int_a); </P> <p> return 0; </P> <p >}< br/>

Compile the program, and then run the "size executable program name" command to check its segment size.

[Root @ SS test] # Size Test

Text Data BSS dec hex filename

1229 280 8 1517 5ed Test

Then, change the global variable g_int_a to a non-Const. Compile the variable and run the size command to view it:

[Root @ SS test] # Size Test

Text Data BSS dec hex filename

1225 284 8 1517 5ed Test

It can be found that the size of the test segment (Text Segment) is reduced by four bytes, and the size of the Data Segment (Data Segment) is increased by four bytes.

Conclusion 1: The global variables modified by const are stored in text segments.

Then, test whether the specified constant can be changed. Still the above Code, add the statement in main:

G_int_a = 10;

When compiling, an error message is displayed: "Main. C: 11: Error: assign a value to the read-only variable 'G _ int_a"

Conclusion 2:

The value of a global variable modified by const cannot be changed by the variable name.

Test whether the address can be used to change its value. Add a statement in main

Int * pint = (int *) & g_int_a;

* Pint = 10;

No errors are found during compilation. Then run, the system prompts a segment error, and the program exits: "segment error (core dumped)"

Conclusion 3:

Because the global variable modified by const is saved in a text segment, its value cannot be modified by pointer.

 

Test static global variables

It is consistent with the non-static global variables and conforms to the above three conclusions.

 

Test static local variables

It is consistent with the non-static global variables and conforms to the above three conclusions.

 

Test non-static local variables

Conclusion 1: The local variables modified by const are stored in the stack segment.

Conclusion 2:

The value of a local variable modified by const cannot be changed by the variable name.

Conclusion 3:

The value of a local variable modified by const cannot be modified by pointer.

 

To sum up:

  Non-static global variables Static global variables Non-static local variables Static local variable
Storage location Text Section Text Section Stack segment Text Section
Change its value using the variable name No No No No
Use a pointer to change its value No No Yes No


 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.