A Beginner’s Guide to Microcontroller Programming — Understanding the Structure of C Code Files Using main.c/.h as an Example

This article serves as preparatory content for beginners in microcontroller development, explaining the structure of C language code files using main.c/.h as an example, to help newcomers quickly understand C language for microcontroller programming.

There are only two types of C language code files: source files with a .c suffix and header files with a .h suffix. In actual development, product functionality is generally developed in a modular way, so each module typically consists of a pair of .c/.h files. This article explains the structure of these two files to facilitate a quick understanding of C code development.

01

main.h Header File

1. Content of the Header File:

It includes macro definitions, variable declarations, structure data type definitions, function prototypes, etc.;

2. Format of the Header File:

The general format of a header file is as follows:

#ifndef _MAIN_H // This is a precompiled protection condition to avoid multiple inclusions of the header file#define _MAIN_H// Macro definition#define TEST  0U// Variable declarations for external module references// For example, extern int var_a;// Custom data types to be used by external modules// For example, struct my_struct{  int a;  int b;  int c;}; // Function prototypes to be called externally// For example, int main(void)#endif

To avoid multiple inclusions of the header file, a precompiled statement needs to be added at the very beginning of the header file:

#ifndef _MAIN_H// The meaning of if not define _MAIN_H#define _MAIN_H...#endif

This segment informs the compiler during precompilation that if the header file is included and the symbol “_MAIN_H” is not defined, then define “_MAIN_H”, ending the precompiled code block with “#endif”. When the header file is included again, since “_MAIN_H” has already been defined, the condition “#ifndef _MAIN_H” will not hold true, and thus the internal declarations will not be added to the file that includes this header file. The symbol “_MAIN_H” should be unique, and it is generally named after the module file name.

3. Mechanism of the Header File:

During precompilation, the compiler adds the content of the header file to the file that includes it, allowing the compiler to know the function/variable/macro definition/data type declarations that the source file calls, and find their actual definitions. This is the C language rule for calling functions across files;

If you do not want to include all declarations from the header file, there is a way to partially include them: use the keyword “extern” in the source file where the external function or variable is declared;

For example, in a C code source file:

// This is a C source code file// Use the extern keyword for external function declarationextern void func_outside(void);// The same applies to variablesextern int var_outside;int func2(void){  // Then you can call this function within the function body defined in this file  func_outside();  var_outside = 0;}

4. How to Use Header Files:

When you need to reference the functions/variables/data type definitions declared in the header file, simply include the header file in the corresponding source code or header file, using the following method:

// This is a main.c source code file// main.h is the header file in the current project, included with 

Leave a Comment