C Language Essentials

  • Predefined Macros:

    • <span>__FILE__</span>, <span>__LINE__</span>, <span>__DATE__</span>, <span>__TIME__</span>, and <span>__func__</span> provide context information at compile time, commonly used for debugging.

  • Floating Point Handling:

    • Floating point numbers cannot be compared directly using <span>==</span>, and functions like <span>fabs</span> should be used for approximate comparisons.

    • The storage format of floating point numbers consists of a sign bit, exponent, and mantissa.

  • Data Exchange:

    • Two variables can exchange values using XOR operations.

  • <span>typedef</span> vs <span>#define</span>:

    • <span>typedef</span> is preferred over <span>#define</span> because it can create concise aliases for complex types.

  • Data Type Conversion:

    • Prefer conversion to long data types, and floating point numbers should be prioritized over integers.

  • Natural Atomicity of Data:

    • 8-bit microcontrollers require two operations for 16-bit data operations, necessitating atomic protection.

  • <span>const</span> Keyword:

    • <span>const</span> variables are read-only, and the compiler and processor will protect them from modification.

1. Predefined Macros

The C language provides several predefined macros to offer context information at compile time.

(1) <span>__FILE__</span>

  • Meaning: Represents the name of the source file currently being compiled.

  • Usage: Commonly used for outputting debugging information.

  • Example Code:

printf("File: %s\n", __FILE__);

(2) <span>__DATE__</span>

  • Meaning: Represents the date the source file was compiled.

  • Format: Typically in the format <span>"Mmm dd yyyy"</span>, e.g., <span>"Jul 25 2025"</span>.

  • Example Code:

printf("Date: %s\n", __DATE__);

(3) <span>__TIME__</span>

  • Meaning: Represents the time the source file was compiled.

  • Format: Typically in the format <span>"hh:mm:ss"</span>, e.g., <span>"14:30:00"</span>.

  • Example Code:

printf("Time: %s\n", __TIME__);

(4) <span>__LINE__</span>

  • Meaning: Represents the line number of the current file.

  • Usage: Commonly used for outputting debugging information to help locate code positions.

  • Example Code:

printf("Line: %d\n", __LINE__);

(5) <span>__STDC__</span>

  • Meaning: If the compiler supports the ANSI C standard, this value is 1.

  • Usage: Used to check if the compiler conforms to the ANSI C standard.

  • Example Code:

#if defined(__STDC__)printf("Compiler supports ANSI C standard.\n");#endif

(6) <span>__func__</span>

  • Meaning: Represents the name of the current function.

  • Usage: Commonly used for outputting debugging information.

  • Example Code:

void myFunction() {    printf("Function: %s\n", __func__);}

2. Floating Point Handling

(1) Floating Point Comparison

  • Floating point numbers cannot be compared directly using <span>==</span> due to potential precision issues in their representation.

  • Example Code:

float a = 0.1 + 0.2;float b = 0.3;if (fabs(a - b) < 0.00001) {    printf("a is approximately equal to b\n");}

(2) Floating Point Storage Format

  • The storage format of floating point numbers is: sign bit (1 bit) + exponent (8 bits) + mantissa (23 bits).

  • Smaller floating point numbers have higher precision, while larger floating point numbers have lower precision.

(3) Floating Point Data Conversion

  • Method 1: Direct Conversion:

int iVal = 0x40880000;float *pfVal = (float *)&iVal;printf("Float value: %f\n", *pfVal);

Method 2: Using Union:

typedef union {    unsigned char byte[4];    float Result;} uFloatConvert;uFloatConvert u;u.byte[0] = 0x00;u.byte[1] = 0x00;u.byte[2] = 0x88;u.byte[3] = 0x40;printf("Float value: %f\n", u.Result);

3. Data Exchange

(1) XOR Swap

  • Macro Definition:

#define SWAP0(a, b) \    a = a ^ b; \    b = a ^ b; \    a = a ^ b;

(2) Arithmetic Swap

  • Macro Definition:

#define SWAP1(a, b) b = (a + b) - (a = b);

4. <span>typedef</span> vs <span>#define</span>

(1) <span>typedef</span>

  • Advantages:<span>typedef</span> is preferred over <span>#define</span> because it can create concise aliases for complex types.

  • Example Code:

typedef struct {    int x;    int y;} Point;Point p = {1, 2};

(2) <span>#define</span>

  • Usage:<span>#define</span> is used for macro definitions but cannot create aliases for types.

  • Example Code:

#define dPS struct s *

5. Data Type Conversion

(1) Data Type Conversion Rules

  • Prefer conversion to long data types.

  • Floating point numbers and integers should be converted to floating point numbers first.

  • Unsigned numbers should be converted to unsigned numbers over signed numbers.

  • Example Code:

int a = 10;float b = 20.5;float result = a + b; // Result is a floating point number

(2) Type Casting

  • Method: Use <span>()</span> or <span>UL</span> modifiers.

  • Example Code:

int i = 10;float f = (float)i; // Force conversion to floating point

6. Natural Atomicity of Data

(1) Natural Atomicity of Data

  • 8-bit microcontrollers have 8-bit integers.

  • 16-bit microcontrollers have 16-bit integers.

  • 8-bit microcontrollers require two operations for 16-bit data operations, which need atomic protection during this period.

  • Example Code:

int a = 0x1234;int b = 0x5678;int result = a + b; // 8-bit microcontroller needs two operations

(2) Boolean Type

  • Generally, integers with natural atomicity are used to define boolean types.

  • Example Code:

typedef int bool;

7. <span>const</span> Keyword

(1) <span>const</span> Modified Variables

  • <span>const</span> modified variables are read-only variables, not constants.

  • Pointers can modify the values of local variables modified by <span>const</span>, but cannot modify the values of global variables.

  • Example Code:

const int global_var = 10; // Global variableconst int local_var = 20;  // Local variablevoid func() {    int *ptr = (int *)&local_var;    *ptr = 30; // Modify local variable's value}

(2) Protection of <span>const</span> Variables

  • At compile time, the compiler ensures that <span>const</span> variables cannot be changed.

  • The processor protects <span>const</span> read-only variables from being modified.

  • Example Code:

const int *ptr = &global_var;// *ptr = 30; // Error, cannot modify const variable

8. Usage of Predefined Identifiers

(1) Common Predefined Identifiers

  • <span>__FILE__</span>: Represents the name of the currently compiled source file.

  • <span>__LINE__</span>: Represents the line number of the current file.

  • <span>__DATE__</span>: Represents the date the file was compiled.

  • <span>__TIME__</span>: Represents the time the file was compiled.

  • <span>__func__</span>: Represents the name of the current function.

(2) Example Code

#include <stdio.h>void myFunction() {    printf("File: %s\n", __FILE__);    printf("Line: %d\n", __LINE__);    printf("Date: %s\n", __DATE__);    printf("Time: %s\n", __TIME__);    printf("Function: %s\n", __func__);}int main() {    myFunction();    return 0;}

Output:

File: main.cLine: 10Date: Jul 25 2025Time: 14:30:00Function: myFunction

Leave a Comment