Common C Programming Errors
Common C Programming Errors
Introduction
Incomputing,C is a general purpose
programming languageinitially developed
byDennis Ritchiebetween 1969 and 1973
atAT&T Bell Labs.
C is animperative(procedural) language. It was
designed to be compiled using a relatively
straightforwardcompiler, to provide low-level
access to memory and to require minimalruntime support. C was therefore useful for many
applications that had formerly been coded in
assembly language, such as insystem
programming. Like mostimperative
languagesin theALGOLtradition, C has
facilities forstructured programmingand
allowslexical variable scopeandrecursion.
C is one of the most widely used programming
languages of all time,and C compilers are
available for the majority of available computer
architectures andoperating systems.
Syntax Errors
The set of rules (grammatical rules) of aprogramming language for
writing statements of thecomputer programis known as syntax ofthe
language. The program statements are written strictly according to these
rules.
Syntax error occur when syntax of aprogramming languageare not
followed in writing thesource code. The compiler detects these errors at
compiling time ofsource code. The compiler reports a propererror
message about the error.
The compiler does not compile a program that contain syntax errors.
Thesyntaxerrors are easy to detect and remove.
In C program, there can be many causes of syntax errors.
Someexamplesare given below:Missing semicolon (;) at the end of statement.
Missing any of delimiters e.g.{or}
Incorrect spelling of any keyword.
Using variable without declaration etc.
prints out:
Two
Three
prints out:
Two
Using = instead of ==
C's=operator is used exclusively for assignment and returns the value
assigned. The==operator is used exclusively for comparison and returns an
integer value (0 forfalse, not 0 fortrue). Because of these return values, the C
compiler often does not flag an error when=is used when one really wanted
an==. For example:
int x = 5;
if ( x = 6 ) printf("x equals 6\n");
This code prints outx equals 6! The assignment inside theifsetsxto 6
and returns the value 6 to theif. Since 6 is not 0, this is interpreted
astrue.
One way to have the compiler find this type of error is to put any
constants (or any r-value expressions) on the left side. Then if an=is
used, it will be an error:
if ( 6 = x)
Size of arrays
Arrays in C always start at index 0. This
means that an array of 10 integers defined
as:
int a[10];
has valid indices from 0 to 9not10! It is
very common for beginners go one too far
in an array. This can lead to unpredictable
behavior of the program.
Integer division
Unlike Pascal, C uses the / operator for both real and integer division. It
is important to understand how C determines which it will do. If both
operands are of an integral type, integer division is used, else real
division is used. For example:
double half = 1/2;
This code setshalfto 0 not 0.5, because 1 and 2 are integer constants.
To fix this, we could change at least one of them to a real constant.
double half = 1.0/2;
If both operands are integer variables and real division is desired,
casting one of the variables todouble(orfloat) would do the trick.
int x = 5, y = 2;
double d = ((double) x)/y;
Loop errors
In C, a loop repeats the very next statement after the
loop statement. The code:
int x = 5;
while( x > 0 );
x--;
is an infinite loop. The semicolon after
thewhiledefines the statement to repeat as the null
statement (which does nothing). Removing the
semicolon will make the loop works as expected.
Another common loop error is to iterate one too many
times or one too few.
char
There are two solutions to this problem. We could use an array or dynamically
allocate an array.
#include <string.h>
int main() {
char st[20]; /* defines an char array */
strcpy(st, "abc"); /* st points to char array */
return 0;
}
or
#include <string.h>
#include <stdlib.h>
int main() {
char *st = malloc(20); /* st points to allocated array*/
strcpy(st, "abc"); /* st points to char array */
free(st); /* don't forget to deallocate when done! */
return 0;
}
Actually, the first solution is much preferred for what this code does. Why?
Dynamical allocation should only be used when it is required. It is slower and
more error prone than just defining a normal array.
String Errors
Confusing character and string constants
C considers character and string constants as very different things.
Character constants are enclosed insingle quotes but string
constants are enclosed indouble quotes. String constants act as a
pointer to the actually string. Consider the following code:
char ch = 'A'; /* correct */
char ch = "A"; /* error */
The second line assigns the character variablechto the address of a
string constant. This should generate a compiler error. The same
should happen if a string pointer is assigned to a character constant:
const char * st = "A"; /* correct */
const char * st = 'A'; /* error */
st1[] = "abc";
st2[] = "abc";
st1 == st2 ) printf("Yes");
printf("No");