Microchip Technology SW006021-2 User Manual
How To’s
2012 Microchip Technology Inc.
DS52053B-page 47
Only use a cast in situations where the types used by the compiler are not the types
that you require. For example consider the result of a division assigned to a floating
point variable:
that you require. For example consider the result of a division assigned to a floating
point variable:
int i, j;
float fl;
fl = i/j;
In this case integer division is performed, then the rounded integer result is converted
to a float format. So if i contained 7 and j contained 2, the division will yield 3 and
this will be implicitly converted to a float type (3.0) and then assigned to fl. If you
wanted the division to be performed in a float format, then a cast is necessary:
to a float format. So if i contained 7 and j contained 2, the division will yield 3 and
this will be implicitly converted to a float type (3.0) and then assigned to fl. If you
wanted the division to be performed in a float format, then a cast is necessary:
fl = (float)i/j;
(Casting either i or j will force the compiler to encode a floating-point division). The
result assigned to fl now be 3.5.
result assigned to fl now be 3.5.
An explicit cast may suppress warnings that might otherwise have been produced. This
can also be the source of many problems. The more warnings the compiler produces,
the better chance you have of finding potential bugs in your code.
can also be the source of many problems. The more warnings the compiler produces,
the better chance you have of finding potential bugs in your code.
3.4.1.2
CAN IMPLICIT TYPE CONVERSIONS CHANGE THE EXPECTED
RESULTS OF MY EXPRESSIONS?
RESULTS OF MY EXPRESSIONS?
Yes! The compiler will always use integral promotion and there is no way to disable this,
see Section 5.6.1 “Integral Promotion”. In addition, the types of operands to binary
operators are usually changed so that they have a common type as specified by the C
Standard. Changing the type of an operand can change the value of the final expres-
sion so it is very important that you understand the type C Standard conversion rules
that apply when dealing with binary operators. You can manually change the type of an
operand by casting, see Section 3.4.1.1 “When Should I Cast Expressions?”.
see Section 5.6.1 “Integral Promotion”. In addition, the types of operands to binary
operators are usually changed so that they have a common type as specified by the C
Standard. Changing the type of an operand can change the value of the final expres-
sion so it is very important that you understand the type C Standard conversion rules
that apply when dealing with binary operators. You can manually change the type of an
operand by casting, see Section 3.4.1.1 “When Should I Cast Expressions?”.
3.4.1.3
HOW DO I ENTER NON-ENGLISH CHARACTERS INTO MY PROGRAM?
The ANSI standard and MPLAB XC8 do not support extended characters set in char-
acter and string literals in the source character set. See Section 5.4.6 “Constant
Types and Formats” to see how these characters can be entered using escape
sequences.
acter and string literals in the source character set. See Section 5.4.6 “Constant
Types and Formats” to see how these characters can be entered using escape
sequences.
3.4.1.4
HOW CAN I USE A VARIABLE DEFINED IN ANOTHER SOURCE FILE?
Provided the variable defined in the other source file is not static (see
Section 5.5.2.1.1 “Static Variables”) or auto (see Section 5.5.2.2 “Auto Variable
Allocation and access”), then adding a declaration for that variable in the current file
will allow you to access it. A declaration consists of the keyword extern in addition to
the type and name of the variable as specified in its definition, e.g.
Section 5.5.2.1.1 “Static Variables”) or auto (see Section 5.5.2.2 “Auto Variable
Allocation and access”), then adding a declaration for that variable in the current file
will allow you to access it. A declaration consists of the keyword extern in addition to
the type and name of the variable as specified in its definition, e.g.
extern int systemStatus;
This is part of the C language and your favorite C text will give you more information.
The position of the declaration in the current file determines the scope of the variable,
i.e., if you place the declaration inside a function, it will limit the scope of the variable to
that function; placed outside of a function allows access to the variable in all functions
for the remainder of the current file.
i.e., if you place the declaration inside a function, it will limit the scope of the variable to
that function; placed outside of a function allows access to the variable in all functions
for the remainder of the current file.
Often, declarations are placed in header files and these are then #included into the
C source code, see Section 5.14.2 “Preprocessor Directives”.
C source code, see Section 5.14.2 “Preprocessor Directives”.