[C ++ primer plus] [learning C ++ from Chapter 2] -- 2. c ++ basic statements

Source: Internet
Author: User

[C ++ primer plus] [learning C ++ from Chapter 2]

2. c ++ basic statements

// Carrot. CPP -- C ++ basic statement # include <iostream> int main (void) {using namespace STD; int B; B = 2; int A = 1; int C; cout <"enter an integer:"; CIN> C; cout <"A is" <A <", B is" <B <", C is "<C <". \ n "; return 0 ;}


2.1 Definition and Declaration

I have never been able to clearly understand these two concepts before. Please clarify them now.


int b;

Defines an integer variable, that is, B is allocated with storage space suitable for integer variables.

At the same time, it is a declaration. In my understanding of the concept of declaration, when I encounter B again, I will tell the compiler that there is a definition of B so that the compiler will know that no error should be reported.

Declarations are not necessarily definitions, such


extern int d;


It is a declaration that tells the compiler that there is a D definition, and extern indicates that this definition may be elsewhere. No space is allocated for D. (About extern)

Conclusion: Define the allocation space and declare that no allocation is made. The declaration only tells the compiler that such a definition exists.


2.2 initialization and assignment

See the following code.

Initialization


// Ta. cpp-test difference Betwwen assignment and initializationint main (void) {static int A = 1; // initialize return 0 ;}

Compile into assembly language

G ++-wall-s ta. cpp-O ta. s


.file"ta.cpp".text.globl main.typemain, @functionmain:.LFB0:.cfi_startproc.cfi_personality 0x0,__gxx_personality_v0pushl%ebp.cfi_def_cfa_offset 8movl%esp, %ebp.cfi_offset 5, -8.cfi_def_cfa_register 5movl$0, %eaxpopl%ebpret.cfi_endproc.LFE0:.sizemain, .-main.data.align 4.type_ZZ4mainE1a, @object.size_ZZ4mainE1a, 4_ZZ4mainE1a:.long1.ident"GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3".section.note.GNU-stack,"",@progbits


Likewise

Value assignment code

// TB. cpp-test difference Betwwen assignment and initializationint main (void) {static int A; A = 1; // value returned 0 ;}


Compiled into assembly language: G ++-wall-s TB. cpp-o tb. s

.file"tb.cpp".text.globl main.typemain, @functionmain:.LFB0:.cfi_startproc.cfi_personality 0x0,__gxx_personality_v0pushl%ebp.cfi_def_cfa_offset 8movl%esp, %ebp.cfi_offset 5, -8.cfi_def_cfa_register 5movl$1, _ZZ4mainE1amovl$0, %eaxpopl%ebpret.cfi_endproc.LFE0:.sizemain, .-main.local_ZZ4mainE1a.comm_ZZ4mainE1a,4,4.ident"GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3".section.note.GNU-stack,"",@progbits


From the compiled assembly code, we can see what is the difference between the two in terms of processing.

If static is removed, the assembly code is the same in my experiment.

Whether to use static, initialization, or assignment in recursive functions is very different.

If your eyes are as hard to use as I do, you can use the emacs file comparison function:

Start the M-x ediff command

Enter ta. s TB. s as prompted

Then we can compare it. A frame in the upper right corner shows several different places, place the cursor there, and use P n to switch between different positions.


2.3 CIN

Like cout, CIN is in the namespace STD for input.

New Features of 2.4 cout

Code

cout << "a is " << a << ", b is " << b << endl;


Concatenates strings and variables for output.

There are two problems

Why is it possible to concatenate: the reason is that cout <"A is" outputs the string "A is" and returns the cout object, which is equivalent

cout << a << ", b is " << b << endl;


And so on.

Why don't you specify the variable type? The reason is that the "polymorphism", the cout Class re-wears the "<" operator, so that it can make different responses based on different types of variables, it is more "intelligent" than the printf function in C language.


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.