Variable declaration in C Language

Source: Internet
Author: User

Variable declaration in C Language

Int I, j, k = 0; // defines three integer variables: char c; // defines an integer c. Of course, multiple variables in a declaration statement can be separated and written, the Declaration above is equivalent to int I = 0; int j = 0; int k = 0; char c; the declaration of any variable can be limited by the const qualifier. The specified value cannot be modified, for example, # include <stdio. h> int main () {const int num = 20; num = num + 2; printf ("% d", num); return 0;} error: l-value specifies const object # include <stdio. h> int main () {volatile const int num = 20; int * p = & num; * p = 10; printf ("% d", num); return 0 ;}

 

Result: 10 volatile prevents the compiler from optimizing the code and reminds the compiler that the variables defined after it may change at any time. Therefore, each time the compiled program needs to store or read the variable, data is read directly from the variable address. If there is no volatile keyword, the compiler may optimize reading and storage, and may temporarily use the value in the register. If this variable is updated by another program, it will be inconsistent.

Related Article

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.