Basic knowledge of C language ---- Union & amp; enumeration & amp; struct, basic knowledge ----

Source: Internet
Author: User

Basic knowledge of C language ---- Union & enumeration & structure, basic knowledge ----
Struct tag (optional) {type 1 identifier 1; Type 2 Identifier 2; ......} (variable definition optional );
Tags available for union {type 1 identifier 1; Type 2 Identifier 2 ;........
} Optional variable definitions. The Union is similar to the struct, but there are key differences in the memory layout. Each member in the consortium is stored sequentially. in the consortium, all members are stored starting from the offset to zero (the positions are overlapping), and at a certain time point, only one member is actually stored in the address change.
Enumeration (it is used to declare a group of named constants. When a variable has several possible values, it can be defined as Enumeration type) enum optional label {content .....} optional variable definition. If a value is assigned to an identifier in the list, the identifier next to the identifier is greater than the value 1. however, for undefined identifiers, the names in enumeration cannot be displayed in the debugger and can be used in code debugging. # A constant defined by the define macro, it is replaced during compilation.
Sample program[Cpp]View plaincopyprint?

  1. # Include <stdio. h>
  2. Struct s
  3. {
  4. Int;
  5. Char c;
  6. } S1;
  7. Enum e
  8. {
  9. A, B = 3
  10. } E1;
  11. Union u
  12. {
  13. Int;
  14. Int B;
  15. Char c;
  16. } U1;
  17. Int main (void)
  18. {
  19. S1.a = 3;
  20. U1.a = 3;
  21. U1. B = 4;
  22. E1 = B;
  23. Printf ("struct: s1.a = % d \ n", s1.a );
  24. Printf ("enum: e1_ B = % d \ n", e1 );
  25. Printf ("union: u1.a. & = % x \ n", & u1.a );
  26. Printf ("union: u1. B. & = % x \ n", & u1. B );
  27. }
Running result
The running results show that all members in the Union share one storage address and only one storage address exists.


Basic knowledge of C Language

Yes. The landlord is very smart. Haha

C language entry question

For the C language, it is recommended that the author of Tsinghua's tan haoqiang be more suitable for beginners. Although the content of the book is not detailed enough, it is specific and easy to understand, however, the disadvantage is that the orchestration is not reasonable. If you choose to read this book, you are advised to read the while do ~ Before the while and for loops, read the function call chapter, which helps you better understand the program... this book is currently used for C language teaching in colleges and universities. It is based on 0 and has no difficulty in self-learning. Primer C is indeed a good book, but it is really not suitable for beginners. If it is all integrated, then you can say that it is a small achievement in C language, this book is suitable for people who have a certain degree of foundation. It is not recommended that you use this book to start learning.
For C ++, it must be determined that money can be written. This person's understanding of C ++ is unmatched by the vast majority of students who write teaching materials, his book is also the C ++ Teaching Book of the national key university. Although the content inside is a bit in-depth, the content in the book is indeed good, A lot of words are brilliant. For C ++ learning, especially for beginners, you must read the books written by this person. There are several editions of his books, preferably the new version. The old version does not matter and there will be no major changes. Like the C language, primer C ++ is also a very high usage book, which is not suitable for beginners and is not recommended for you to start learning.
Currently, software programming majors vary in the course classes of major colleges and universities across the country. Some schools learn C first and C ++ later. Some schools do not learn C, and direct C ++ has their own advantages and disadvantages, in my personal experience, it is recommended that you learn C first. After all, C is conducive to your learning of C ++, although some people say that C learning will help you learn C ++ with a lot of ideas fixed to the wrong position, learning programming is flexible, in addition, C is a very universal language and is suitable for writing large programs. On the basis of your C language, C ++ learning will be very smooth!
I hope you can learn this well and have your own achievements in the future. Also, you should never forget that programming is a hard job. If you have chosen this path, don't regret it. You should make great efforts in this kind of work diligently, otherwise, all your efforts will be in vain.

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.