#pragma指令的使用

Source: Internet
Author: User
Tags pack
Preface

#pragma指令为我们提供了让编译器执行某些特殊操作提供了一种方法. This instruction is useful for very large programs or programs that require special features of a particular compiler. #pragma指令的一般形式为: #pragma para, para as parameters. The command can be simple or complex.

#pragma命令中出现的命令集在不同的编译器上是不一样的, you must consult the document of the compiler you are using to understand what commands are available, and the capabilities of those commands. pragma common parameter explanation

1. #pragma message ("Messages")

As for the role of this directive, you can look through the demo below.
#include <iostream>
using  namespace std;
#define STR
int main ()
{
    #ifdef str
        #pragma message ("STR" has been defined. ")
    #endif//STR
}

the results of the operation are shown below:

We found that the parameter information of the message was printed at compile time. Unlike the previously learned printf () and cout implementation printing, message print messages appear at compile time and do not appear in the final running results of the program. In this way, you can see if a macro has been defined. Sometimes you do not want to have information that is not relevant to the results in your running results, and you can use the #pragma command to select the message parameter to achieve the printout of the information.

2. #pragma once
If you add this instruction to the beginning of the header file, it is guaranteed that the header file is compiled only once.

3. #pragma hdrstop
This directive indicates that the compilation header file is so far and that no further compilation is required.

4. #pragma pack ()

#include <iostream>
using  namespace std;

int main ()
{
#pragma pack (2)

    struct student1
    {
        char name[20];
        Char  num[10];
        int score;
        char sex;
    } STU1;

    cout << sizeof (STU1) << Endl;

#pragma pack ()

    struct Student2
    {
        char name[20];
        Char  num[10];
        int score;
        char sex;
    } STU2;

    cout << sizeof (STU2) << Endl;
    System ("pause");
}

The results of the operation are as follows:

Using pseudo-directive #pragma pack (n), the C compiler is aligned according to n bytes.
Use the pseudo Directive #pragma pack () to cancel the custom byte alignment.

#include <iostream>
using  namespace std;

int main ()
{
#pragma pack (push)
#pragma pack (2)

    struct student1
    {
        char name[20];
        Char  num[10];
        int score;
        char sex;
    } STU1;
    cout << sizeof (STU1) << Endl;

#pragma pack (pop)

    struct student2
    {
        char name[20];
        Char  num[10];
        int score;
        char sex;
    } STU2;
    cout << sizeof (STU2) << Endl;
    System ("pause");
}

Execute the code above, and the execution results are exactly the same.

#pragma pack (push) is to save the current default byte alignment, and #pragma pack (POP) to restore the default byte alignment.

Note: The structure is defined between the #pragma pack (push) and the #pragma pack (pop), and the structure is defined between the #pragma pack (n) and the #pragma pack (), and the effect is the same.

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.