Why don't the line numbers in an error message match the problematic line of code?

In many cases, an error or warning is reported for a line that follows the line that contains the error. For example, consider these lines; the first is missing a semi-colon at the end:

There will an expression error produced, but the error will indicate the second line. This is reasonable, as the C language allows statements to be spread over multiple lines, so there is nothing actually wrong with the first line of code. Once the second line is parsed, it then becomes obvious that something is wrong. The error will be flagged at the line at which things first go wrong.

If you are seeing error messages that are out by a number of lines, one thing you can check is that your source files are using the correct "new line" characters. If you are compiling under Windows®, there should be a CR-LF sequence at the end of each line. If, for example, the LF character was stripped somehow from a line, IDEs, like MPLAB® X IDE, would still interpret the remaining CR as a new line but the compiler will not. Since it is legal to have two statements on the one line, seeing a multi-statement line is not flagged as an error. But if there is an error later in the code, the line number count will not be accurate.

To see the characters used by a source file, you will need to use a text editor that can display them or dump the file in binary.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.