Introduction to using C ++ to define variables

Source: Internet
Author: User

The C ++ programming language is no stranger to the majority of developers. It has a wide range of functions and can help us easily complete various functions. Here we will introduce in detail how to define variables in C ++, hoping to help you solve some problems.

  • C ++ declares the correct method of application
  • How can C ++ inline functions help us?
  • C ++ smart pointer Application Experience
  • What is C ++ hosting?
  • C ++ command line mode compilation and setting skills

There are two main methods to define variables in C ++:

1. Symbolic constant (# define)

2. Constant Value variable (const)

The definition of symbolic constants is actually unrelated to C ++ itself. It is a compiler instruction, or a pre-processing instruction related to a pre-processing instruction and # include, # ifdefine, and so on ). Its usage is as follows:

# Define PI 3.1415926

This syntax defines a symbolic constant called PI. Its value is 3.1415926.

Note: because it is not actually a C ++ syntax, it does not need or can not) end with a semicolon.

How can we use it?

 
 
  1. # Include <iostream>
  2. # Define PI 3.1415926
  3. Using namespace std;
  4. Int main ()
  5. {
  6. Double Area (int radio); // declaration method
  7. Cout <Area (2) <endl;
  8. Return 0;
  9. }
  10. Double Area (int radio) // implementation method, which is a method for calculating the Area based on the radius
  11. {
  12. Return PI * radio;
  13. }

In contrast to a symbolic constant, a constant value variable must be defined by the const keyword. Relatively speaking, const is more modern. It is a keyword of c ++. Its usage is as follows:

 
 
  1. const double PI=3.1415926; 

Let's convert the above Code.

 
 
  1. # Include <iostream>
  2. Using namespace std;
  3. Const double PI = 3.1415926;
  4. Int main ()
  5. {
  6. Double Area (int radio); // declaration method
  7. Cout <Area (2) <endl;
  8. Return 0;
  9. }
  10. Double Area (int radio) // implementation method, which is a method for calculating the Area based on the radius
  11. {
  12. Return PI * radio;
  13. }

To sum up, we recommend using const instead of # define preprocessing commands.

1. const can define data types, improving type security. For example, we can specify that the constant PI is of the double type.

2. Since const is a variable, it is not changed, but it is called a constant value variable), it has an address and wider applicability.

3. Better syntax

Question: Can I use symbols in C?

Symbol definitions can also be used in C #, for example, the following:

 
 
  1. # Define MINI
  2. Using System;
  3. Using System. Collections. Generic;
  4. Using System. Text;
  5. Namespace ConsoleApplication1
  6. {
  7. Class Program
  8. {
  9. Static void Main (string [] args)
  10. {
  11. # If MINI
  12. Console. WriteLine ("You have defined the MINI symbol ");
  13. # Endif
  14. Console. Read ();
  15. }
  16. }
  17. }

In general, C ++ defines variables to change the compilation behavior. Because # define is a compiler instruction

In addition to using the # if condition to compile the statement, we can also use the Conditional command.

 
 
  1. # Define MINI
  2. Using System;
  3. Using System. Collections. Generic;
  4. Using System. Text;
  5. Using System. Diagnostics;
  6. Namespace ConsoleApplication1
  7. {
  8. Class Program
  9. {
  10. Static void Main (string [] args)
  11. {
  12. # If MINI
  13. Console. WriteLine ("You have defined the MINI symbol ");
  14. # Endif
  15. // Call the method based on conditions
  16. SomeMethod ();
  17. Console. Read ();
  18. }
  19. [Conditional ("MINI")] // Conditional is a new Attribute for Conditional compilation,
    In this example, the method is compiled only when the MINI symbol is defined.
    Otherwise, a blank space will be placed in any other call.
  20. Static void SomeMethod ()
  21. {
  22. Console. WriteLine ("You called my method ");
  23. }
  24. }
  25. }

However, conditional can only be used for the void method, which is quite understandable. Because you need to know that it may be replaced, that is, it may not be compiled. If it has a return value and the value needs to be used by other program code, isn't it troublesome.

The preceding section describes how to define variables in C ++.

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.