Note when declaring local variables and global variables.

Source: Internet
Author: User
Tags acos

Note when declaring local variables and global variables.

/* ===================================================== ============================================================ Name: testVariable. c Author: lf Version: Copyright: Your copyright notice Description: considerations when declaring local variables and global variables. global variables can only be assigned constant values, however, local variables can be assigned values by constants, expressions, and functions. why? The program needs to use appropriate values to initialize global variables during execution. Therefore, the initial values must be stored in the executable files generated by compilation. Therefore, the initial values must be calculated during compilation. therefore, if you initialize the global variable: double pi2 = acos (-1.0); you must call the acos function when the program is running to calculate the corresponding result. here, we mainly need to differentiate the two phases during compilation and runtime. to simplify the C language of the compiler, the global variables can only be defined using constant expressions. otherwise, the error "initializer element is not a constant expression" is returned. If the global variable is defined: int minute = 480; int hour = minute/60. this is because although it is possible to calculate the hour value during compilation, minute/60 is not a constant expression and does not meet the syntax specification. ========================================================== =============================================================== */# include <stdio. h> # include <stdlib. h> # include <math. h> double pi1 = 3.14159; // the error message is as follows: // initializer element is not a constant expression // double pi2 = acos (-1.0 ); // int hour = minute/60 is incorrect, and the prompt is as follows: // initializer element is not a constant expression // int minute = 480; // int hour = minute/60; int main (void) {printf ("pi1 = % f \ n", pi1); // printf ("pi2 = % f \ n", pi2 ); double pi3 = acos (-1.0); printf ("pi3 = % f \ n", pi3); // printf ("minute = % d \ n", minute ); // printf ("hour = % d \ n", hour); return EXIT_SUCCESS ;}

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.