Considerations for local variables and global variables when declaring

Source: Internet
Author: User
Tags acos

/* ============================================================================ name:testvariable.c Author:                                        LF Version:Copyright:Your Copyright Notice Description: Considerations for local variables and global variables when declaring                                        Global variables can only be assigned constants, but local variables can be assigned constants and expressions, as well as functions.                                        What is this for?                                        The program needs to initialize the global variables with the appropriate values at the beginning of execution, so the initial values must be saved in the compiled executable, so the initial values need to be computed at compile time. So if you initialize the global variable this way: Double Pi2=acos (-1.0); You need to call the ACOS function while the program is running to calculate the corresponding                                        The result.                                        The main point here is to distinguish between the two phases of compile-time and run-time.                                        To simplify compiler C language provisions: Global variables can only be defined with constant expressions.  Otherwise error: initializer element is not a constant expression if you define a global variable: int                minute=480;                                        int hour=minute/60; will also report the sameError. This is because although it is possible to calculate the value of hour at compile time, minute/60 is not a constant expression and does not satisfy the syntax specification. ============================================================================ * * #include <stdio.h> #include <stdlib.h> #include <math.h>double pi1=3.14159;//error, you are prompted as follows://initializer element is not a constant Expression//double Pi2=acos ( -1.0);//int hour=minute/60 is wrong, prompt 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);d ouble Pi3=acos ( -1.0);p rintf ("pi3=%f\n", Pi3),//printf ("minute=%d\n", Minute),//printf ("hour=%d\n", hour); return exit_success;}

Considerations for local variables and global variables when declaring

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.