Declare and initialize variables in C,

Source: Internet
Author: User

Declare and initialize variables in C,

C # variable Initialization is another example of C # emphasizing security. To put it simply, the C # compiler needs to initialize the variable with an initial value before referencing the variable in the operation. Most modern compilers mark non-initialization as warning, but the C # compiler treats it as an error.

1. in C #, the variable declaration format is:

Data type variable name;

2. The variable assignment format is:

  Variable name = data;

3.In general, values are given after the Declaration, or the initial value is assigned when the variable is declared. However, sometimes in the development and design of a program, you often forget to assign the initial value (that is, initialize), which will lead to unexpected errors in the program design.

Explanation: When we declare a variable, the memory will be allocated (for example, int I; 4 bytes will be allocated on the stack ), that is, the memory will be allocated during variable declaration to save the corresponding value of this type. As for the declaration of a variable, it will be used without assigning values. In this case, the compiler reports an error. The error message is: a variable without assigning values is used. One concept in. NET should be familiar to everyone, that is, type security. What is type security? This question is the best example. If you note that. in the. NET compiler, when using unassigned variables, the compiler will prompt that this is an error. In other languages, the compiler prompts a warning about the use of unassigned variables.

4. C # uses two methods to ensure type security:

1) a variable is a field in a class or structure. If no value is displayed, the value of the variable is the default value of the corresponding type when it is created by default.

2) The local variables of the method must be displayed in the Code for initialization before they can be used in the statement. Initialization is not performed when a variable is declared. The Compiler checks all possible paths. If it detects that a local variable uses its value before initialization, the compiler produces an error.

1 public static void Main()2 {3     int a;4     Console.WriteLine(a);5     return;6 }

The above code reports in the compiler: Use of unassigned local variable 'd (using the unassigned local variable "").

Although this protection mechanism is annoying, we should initialize the variables while declaring them to avoid unnecessary troubles.

 

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.