C in about # and # #的简易使用

Source: Internet
Author: User

#运算符用于在预编译时, convert the macro argument to a string

eg.

#include <stdio.h>
#define CONVERT (f) (#f)

void HelloWorld ()
{
printf ("Hi,tom Welcome to You! ");
}

int main ()
{
printf ("%s\n", CONVERT (Hello world!));
printf ("%s\n", CONVERT (100));
printf ("%s\n", CONVERT (while));
printf ("%s\n", CONVERT (return));
printf ("%s\n", CONVERT (HelloWorld));
return 0;
}

Output:

printf ("%s\n", CONVERT (HelloWorld)); This sentence does not execute the contents of the function, there is no output: printf ("Hi,tom Welcome to You! ");

The convert macro simply outputs the name of the function.

# #运算符则用于在预编译时, adhesion two symbols

In general, we define the structure as follows:

#include <stdio.h>

typedef struct _STUDENT
{
int id;
char* name;
}student;

int main ()
{
Student S1;
s1.id=1;
S1.name= "TTH";
printf ("id:%d\n", s1.id);
printf ("name:%s\n", s1.name);

Student *p1;
p1=&s1;
printf ("p1-id:%d\n", p1->id);
printf ("p1-name:%s\n", p1->name);

return 0;
}

When we have the # #运算符符后, we can define the structure as follows:

#include <stdio.h>
#define STUDENT (type) typedef struct _# #type type;\
struct _# #type
STUDENT (STUDENT)
{
int id;
Char *name;
};


int main ()
{
Student S1;
s1.id=1;
S1.name= "TTH";

printf ("id:%d", s1.id);
printf ("name:%s", s1.name);
return 0;
}

Output:

C in about # and # #的简易使用

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.