C--(14) struct, macro, compile

Source: Internet
Author: User

Knowledge Points:
1. struct structs
2.typedef keywords
3. Definition of macros
4. The difference between a macro and a function
5. File inclusion and multi-file compilation
6. Conditional compilation
===========================
Structural body
Think: If you now want to save a student's information, name, height, age, address, how to save

Char name[64];
float height;
int age;
Char addr[64];


1. What is struct struct
struct refers to a data structure, which is a kind of compound data type in C language.
A collection of many different data types

2. The role of the structural body
Structs can be used to create complex data structures.

3. Definition of structure

1) Define the syntax
struct Structure Body name

Member List
};


4. Struct Member declaration
1) can declare various types of members
2) where the member declaration needs to be noted
Practice:

1. Define a structure to describe the time,
Specifically, the structure has three members, respectively, describe the time and seconds information
1 Constructing a Time type
2 Save the current time to the time variable
3 Displaying time variables

2. Define a structure to describe the information of a book,
Structure members include name, unit price, author, publisher, publication date

5. Declaring struct type variables and initialization//"C programe", 38.8, "rectification", "Tsinghua"
(1) Method one
(2) Method two
6. How members of a struct are accessed
struct variable name. Member Name

Practice:
1. Initialize and output the information for the time structure body
2. Initialize and output the information of the book structure
3. Enter the information for the book from the terminal, and output


7. Structure array
Data type array an array group length

Practice:
1. Establishment of a registration form containing 2 student information, including number, name, gender, address and 3 results C CPP OC

8. struct-Body pointer


9. Structure size and memory alignment
1) Think: why the same body, the size of different situations
2) Reason: Can be divided roughly into 2 points
When using the sizeof operator to calculate the space occupied by a struct, it is not simple to add up the space of all the elements in the struct, which involves the problem of memory byte alignment.
In theory, access to any variable can start at any address,
In fact, however, access to certain types of variables can only be accessed at a specific address,
This requires that each variable be spatially arranged in a certain way, rather than simply sequentially, which is memory alignment.

Reasons for Memory alignment:

1) Certain platforms can only access certain types of data at specific addresses;
2) Increase the speed of data access. For example, some platforms each time the data is read from an even address, for a variable of type int, if it is stored from the even address unit, it can read the variable in only one reading period, but if it is stored from the odd address cell, it needs 2 read cycles to read the variable.
3) Alignment
1> and compilers, typically 32-bit systems are aligned by default in 4-byte fashion
2> the system is aligned with the type of the largest element in the structure

==========================================================================
typedef key

Thinking: The cross-platform nature of the program 6432168
int int Int2
Long Long Long

size_t

Long Longint64


1.typedef function
1) typedef is commonly used to define an alias for an identifier and a keyword, and does not allocate actual memory
2) typedef can enhance the readability of the program
3) typedef can enhance the flexibility of identifiers
2.typedef Nature
typedef nature is an alias of type
3.typedef Syntax Usage
The typedef already has a new type name for the type name;

4. Redefining the structure
5. Redefining types
The difference between 6.typedef and define
1) typedef is an alias for the readability of the program and for the identifier, and
Define is in C language to define constants, common code substitution
2) define can use #ifdef and #ifndef to make logical judgments


==========================================================================
Macro definition
0. What is a macro definition
1) macro definition is one of the three preprocessing functions provided by C, including macro definition, file inclusion, conditional compilation
1. The role of macro definitions
1) Use identifiers to replace some common code

WALK xiaoming---nickname alias
2. Essence of macro definition
1) A placeholder to replace some common code
2) Everything to change to the premise, to do anything before the first to replace
2. BASIC macro Definition
0) #define指令语法
Macro Macros Macro name macro body
1) identifiers
2) uppercase, end without semicolon
3) Use
Example: A macro that defines a constant pi
Practice:
Macro that defines the size of an array
Defining the gravitational Acceleration macro 9.8

3. Macro definitions with Parameters
1) Example: a macro that calculates a two-digit maximum value
Practice:
1. Calculate the sum of two of the macro, two number of the product


Thinking: ADD (*add) results



4. Issues with macro substitution
1) Demonstration of macro substitution function, gcc compilation parameters
2) Think: Can you nest definitions or invoke


5. Differences between macros and functions

1) macros are simply replaced when the compiler is compiling the source code, without any logical detection, that is, simple code copying.
2) The type of the parameter is not considered when the macro is defined.
3) The use of a parameter macro causes a block of code with the same effect to have multiple copies in the destination file, which increases the size of the target file.
4) parameter macros run faster than functions because they do not require a parameter stack/stack operation.
5) The parameter macro should be more careful when defining, extra parentheses.
6) The function only exists in one place in the target file, which saves the program space.
7) function calls will involve the transfer of parameters, the stack/stack operation, the speed is relatively slow.
8) The parameter of the function has the problem of passing the value and passing the address (pointer), the parameter macro does not exist.
==================================
File inclusion and multi-file compilation
1. Thinking: How to achieve multi-person cooperation projects
Interface----> function prototypes. h//head
Implement----> function definitions. c

Cvsgitsvn

2. Recall function definitions and declarations
3. header files and source files
4. #include指令语法!!!!
1) Essence: The header file contains the actual replacement file content
2) Function: Realize the separation of declaration and implementation, further make the program more modular

#include <stdio.h> angle brackets indicate system standard Catalog
Double quotation marks to indicate a custom path
Add, Sub, mut, Div +-
5. Example: the declaration of the implementation of a linked list is detached
Practice:
1. Implement subtraction arithmetic and separate the Declaration and implementation of the function
and write a test function to test the functions

A B C D200100


==========================================================================
Conditional precompilation
Requirements: Users now purchase only certain modules in the system
1. Thinking: How to easily and quickly to meet the needs of users in the process of compiling

2. #ifdef conditional Compilation syntax
#ifdef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif


#ifndef
1) Function: module selection
Example: Determine if a PI macro has been defined
Exercise: Determine if you have a common macro defined above
Thinking:
1. How to safely and quickly annotate your code
2.//and/* * * comments can be nested using

3. #if conditional Compilation syntax
#if an expression
Procedure Section 1
#else
Procedure Section 2
#endif

1) function
Quick and secure Comment code
4. Security annotations
Thinking: What happens if a repeating header file is included
5. Prevent the header file from repeating the inclusion
#ifndef test_h_h Header file name
#define Test_h_h

#include "test.h"
#endif

==========================================================================
Debug macros

Thinking: How to debug code with printf, whether it's easy to move out
1. DEBUG macro Definitions
INFO, WARN, ERROR, DEBUG
2. Debug switch
3. Pre-defined macros
printf ("line =%d, File =%s, DATA =%s, time =%s",
__line__, __file__, __date__, __time__);
__LINE__: Line number
__file__: File name
__DATE__: Date%s
__TIME__: Time
__FUNC__: Name of function

==========================================================================

C--(14) struct, macro, compile

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.