Article count:1385 Read by:1972833

Featured Content
Account Entry

A coquettish C language operation

Latest update time:2021-04-25
    Reads:

A friend posted a picture similar to the one below in our embedded communication group,

Suddenly there was a commotion,
I'll attach the source code again.

main.c is as follows:

#include <stdio.h>
#include <string.h>

static char city_name[][20] = { 
 #include "city.h" 
 };
 
int main(void) {
        printf("%s", city_name[0]);
}

city.h is as follows:

"北京",
"上海",
"广州"

The most interesting part of this code is that it uses the #include operation in an array in C language. This is the first time I have seen it in my life.

However, a friend expressed dissatisfaction and immediately compiled it on his computer. Unfortunately, the compilation failed.

He also posted his code

What's the problem? Seeing this, you can pause for 10 seconds and think about the reason.

Later, the classmate found the problem on his own.

The problem is that #include "city.h" must be on its own line.

So the question comes again, why does #include have to be on a separate line? For this reason, I took out the dusty "C and Pointers" book and looked through it, but I couldn't find the explanation.

Finally found someone's answer on Google:

# must be the first non-space character in a line, that is to say, there can be spaces before #, but there cannot be other characters.

Let’s put this issue to rest for now.

Later, the president classmate in the group raised another question,

He felt that the writing of city.h did not comply with the C rules. At first glance, it seemed to be the case. General header files were not written in this way. However, in fact, no one stipulated how the header file should be written. As long as the c file after it was included Just comply with the grammatical rules. In addition, the files included in #include are not necessarily files with the suffix .h, but can be files with any suffix format.

There are also advantages to using the above method, which is convenient and intuitive to update the array content.

In addition, if you compile with gcc, gcc can help you expand macro definitions and header files using the -E parameter.

Finally, the president said with emotion: After graduation, I will find a job and be proficient in C language; after two years, I will master C language; after five years, I will understand C language.

I looked up my resume from 2013 when I was looking for a job:

He is right, the first thing I wrote is to be proficient in C language programming...

Complete text.

Welcome to follow us and grow with everyone!

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号