标签:
#ifdef __cplusplus
extern "C" {
#endif
/* body of header */
#ifdef __cplusplus
}
#endif
structured_data_display.h structured_data_display.c
typedef UINT8 user_data_struct_type
#define USER_DATA_MAX_ERROR_CODES 15
enum user_data_struct_type
{
USER_DATA_ERROR_INVALID_DATA,
USER_DATA_ERROR_FAILURE
};
handle_error check_for_error /* NOT errorCheck */
g_default_interface
void start_engine(engine_type* myengine,
UINT8 engine_id,
const UINT32* protected_data);
| 名字 | 标记 | 示例 |
|---|---|---|
| static | s_ | static UINT8 s_some_number |
| static | s_ | static UINT8 s_some_number |
| constant | c_ | const UINT8 c_some_number |
| cp_ | const user_data_type* cp_user_data |
#ifndef FOO_H
#define FOO_H
/* The rest of the file*/
#endif
// NOT ALLOWED #include <../foo/include/foo.h>
/* -------------------------------------------------------------------------------
Copyright (C) 2011, Nollec Wireless CO. LTD. All Rights Reserved
Revision History:
Bug/Feature ID Author Modification Date Description
------------------ ------------------- ------------------ ---------------------
BugID/FeatureID developer name YYYY/MM/DD brief discription
----------------------------------------------------------------------------------*/
(跟编辑器有关,可设置)
void func()
{
if (something bad)
{
...
if (another thing bad)
{
}
}
switch (testValue)
{
case VALUE_1:
/* Body for case 1. */
break;
case VALUE_2:
/* Body for case 2. */
break;
default:
/* Body for default. */
break;
}
x = 100*10+1; //NOT ALLOWEDnumber = 100*10+1;
UINT8 i, j; /* NOT RECOMMENDED */ UINT8 i = INIT_VALUE; /* GOOD */ UINT8 j = INIT_VALUE; /* GOOD */
/* INCORRECT*/
if (cpCurrentState == CP_L3_STATE_TYPE_D1)
{
cpCurrentState++;
}
/*CORRECT*/
if (cp_currentState == CP_L3_STATE_TYPE_D1)
{
cp_currentState = CP_L3_STATE_TYPE_D2;
}
(Does not meet requirements) #define ONE 1 #define TWO 2 (Meets requirements) #define NUM_LOOPS_FOR_AD_READ 4 #define NUM_SAMPLES_TAKEN 8
#define DEBUG 4
/* if undefined, disables debugging */
/* set to 1 - 4 for desired debugging level */
/* This usage of DEBUG in the for loop control statement is
* allowed since the statement is fully enclosed within the
* conditionally compiled section of code.
*/
#ifdef DEBUG
for (i = 0; i < DEBUG; i++)
{
printf("i = %d\n", i);
}
#endif
Example 31 - Example (Incorrect Usage)
#define DEBUG 4
#ifdef DEBUG /* usage here is fine */
/* do something special */
#endif
/* the code statement below is outside the segment controlled by
* the #ifdef and therefore should NOT use DEBUG.
*/
for (i = 0; ((i < 5) && DEBUG); i++)
{
printf("i = %d\n", i);
}
? INT8: This type stores 8-bit signed numeric values. ? UINT8: This type stores 8-bit unsigned numeric values. ? INT16: This type stores 16-bit signed numeric values. ? UINT16: This type stores 16-bit unsigned numeric values. ? INT32: This type stores 32-bit signed numeric values. ? UINT32: This type stores 32-bit unsigned numeric values. ? BOOLEAN: Ideally, this type is an enumerated type that names the boolean values TRUE (1) and FALSE (0). However, some compilers may not store this efficiently. An alternative is typedef UINT8 BOOLEAN; That simply defines BOOLEAN as an 8-bit unsigned value which is distinct from UINT8. In this case, the constants TRUE and FALSE will have to be defined constants.
(Correct Usage)
void functionName
(
UINT8 x[], /* array with "size" elements */
UINT8 size /* number of elements in x */
);
(Incorrect Usage)
void functionName (UINT8 x[10]);
(Correct): #define A_CONSTANT (ANOTHER_CONSTANT + 1) a = ACONSTANT * 2; (Incorrect): #define A_CONSTANT ANOTHER_CONSTANT + 1 a = ACONSTANT * 2;
// NOT RECOMMENDED, imagine you forget one of the “=” signs
If (PHONE == IS_RESET)
{
}
// GOOD, the following statement eliminates the possible errors explained above
// and easier to follow the value that you are looking for
If (IS_RESET == PHONE)
{
}
// NOT RECOMMENDED
if (something)
if (something else)
doThis();
else
while (input)
doThat();
// GOOD
if (something)
{
if (something else)
{
doThis();
}
else
{
while (input)
{
doThat();
}
}
}
Dangerous memory management
error_code_type* myfunction(void)
{
error_code_type* p_temp = malloc (sizeof(error_code_type));
return p_temp;
/* p_temp is never de-allocated and the user of myFunc cannot de-allocate*/
/* because a temporary copy of that instance is returned.*/
/* Calling user code shall take of the memory deallocating which will create*/
/* complexity and confusion*/
}
UINT32* p_number = NULL;if (NULL == p_number){}
#ifdef _HC11_ #define DIRECT _direct_ram #else /* not _HC11_ */ #define DIRECT #endif /* nSize located in direct RAM in embedded, normal RAM in UNIX */ DIRECT UINT8 nSize;
INT8 signed_value; UINT16 long_value; UINT8 short_value; /* Loss here is in accuracy going from signed to unsigned */ signed_value= (UINT8) short_value; /* Loss here is in size, going from a 16 bit value to an 8 bit */ signed_value= (INT8) long_value;
/*INCORRECT*/ *(masterList + 10) = 0; /*CORRECT*/ masterList[10] = 0;
标签:
原文地址:http://blog.csdn.net/tianxuhong/article/details/44486949