+ All Categories
Home > Documents > Errors in C Program

Errors in C Program

Date post: 03-Jun-2018
Category:
Upload: ghanashyamkj
View: 215 times
Download: 0 times
Share this document with a friend
27
Errors in C program
Transcript
Page 1: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 1/27

Errors in C program

Page 2: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 2/27

Introduction

Errors

The process of detecting and correcting

the errors in the program is known as debugging.

There is a program called debugger that

takes the object program as input and execute it

and it helps in eliminating the errors that occurs

in the source program.

Page 3: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 3/27

Generally Errors can be Classified as follows 

Page 4: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 4/27

Syntax error: -

This type of errors are the result of violation ofthe rules of the programming. It is easy to debugthese errors since computer display errors specifyingthe line number.

Compiler Trappable Errors 

• Missing of semicolon “;” . • Missing of closing brace “}”. 

• Mistyping Upper Lower Case .

Missing invited comma.• Variable not declared.

• Using a function or assignment inside a macro.

• Type mismatch in expressions.

Page 5: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 5/27

Missing semicolon;

Every statement

must end with a semi

colon. A missing

semicolon is easily

trapped by the

compiler.

If we forget to use

semicolon in the

program we get theerror message as “

Syntax error or

Statement missing” 

Page 6: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 6/27

Misuse of semicolon:- 

Semicolon is also

called as statementterminator Another

common mistake is

to put a semicolon ina wrong place which

may occur logical and

syntax error. Which

gives error message

as “Declaration

terminated

incorrectly” 

Page 7: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 7/27

Missing of closing brace }

This error is harder tospot and may cause awhole host of irrelevantand incorrect errors afterthe missing brace. Thiserror will generate amessage like

“Unexpected end of fileor Compound statementis missing”

One way to avoid this

by Counting the bracescarefully or by writingboth opening and closingbrace at first and fill inthe statementsafterwards.

Page 8: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 8/27

Mistyping Upper/Lower Case

• C distinguishes between small and capital

letters and it is case sensitive.

• While writing a program by mistake if we type

a data type or any reserve words like “int,

float” e.t.c.., in upper case it will show error as

“Undefined symbol”.

• Thus a program fails at the linking stage

because it has found an undefined symbol.

Page 9: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 9/27

Page 10: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 10/27

Missing invited comma:-

CASE 1:-

If a quote is missedout of a statement

containing a control

string then the compiler

will usually signal thiswith a message like

“String too long

orUnterminated string or

character constants”.

Page 11: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 11/27

CASE 2:-

Every string must be enclosed in a double quotes

while a single character constant in single quotes.If we miss them out, the string (or the character)

will be interpreted as a variable name

Eg:- if(response==YES) /* YES is a string*/

Grade=A; /* A is a character constant*/

Hence YES and A are treated as variable andtherefore a message “undefined names” may ocur 

Page 12: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 12/27

Undeclared Variable:- 

• C requires every variable

to be declared at first

• If variable is used which

has not first been

declared, or that a

variable is used outside ofthe main program.

• Than the compiler will

show a error message like“Undefined symbol”.

Page 13: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 13/27

Using a function or assignment inside a macro 

If for example abs(x) is a macro and not a

function then the following are incorrect:

abs(function());

abs(x = function());

Only a single variable can be substituted into a

macro. This error might generate something

like “value required". 

Page 14: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 14/27

Type mismatch in expressions

There is a rule in C that all maths operations

have to be performed with long variables.These are

• int & long int .

• double & long float.

The result is also a long type. If the userforgets this and tries to use short Cautomatically converts it into long form. The

result cannot therefore be assigned to a shorttype afterwards or the compiler will complainthat there is a type mismatch

Page 15: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 15/27

Logical Errors:-

These errors occurs during coding process.

When the programmer codes his problem, he musttake care of correct operation to be performed. The

program will execute but produces some unwanted

result.

Hence it very difficult to debug such errors,

because computer do not display them. We can

eliminate this error by tracing it and running for

simple data. some of the logical errors are• Confusion of = and ==.

• Confusing C++ and ++C.

• Misuse of arithmetic operators.

Page 16: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 16/27

  Confusing C++ and ++C 

In many cases these two forms are

identical. However, if they are hidden insideanother statement e.g.

array[C++] = 0;

then there is a subtle difference. ++C causes Cto be incremented by 1 before the assignment

takes place whereas C++ causes C to be

incremented by 1 after the assignment hastaken place. So if you find that a program is

out of step by 1, this could be the cause.

Page 17: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 17/27

Confusion of = and ==

A statement such as:

if(a = 0)

{

} is valid in C, but notice that = is the assignment operator

and not the equality operator ==. It is legal to put an assignment

inside the if statement (or any other function) and the value of

the assignment is the value being assigned! So writing theabove would always give the result zero (which is `FALSE' in C)

so the contents of the braces {} would never be executed. To

compare a to zero the correct syntax is:

if(a == 0){

}

Page 18: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 18/27

Runtime errors:-

these errors occur when we attempt to

run the ambiguous instructions. For example,

an infinite loop in a program sequence which

causes no output. These also occur due to

device errors, improper sequencing of

construct, errors in system software, incorrect

data input e.t.c.., the computer will print runtime error message.

Page 19: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 19/27

Some of run time errors are:-

• Divide by zero.

• Infinite looping.

• Missing ‘&’ in scanf. 

• The number of actual and formal parameter

does not match.

• The conversion string in scanf/printf is wrong.

• Array out of bounds

• Mathematical error.

Page 20: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 20/27

Missing “&” in scanf  

This error can often be trapped by a compiler,

but not in all cases. The arguments of the scanfstatement must be pointers or addresses of

variables, not the contents of the variables

themselves. Thus the

following is wrong: And should read as:

int i; int i;

char ch; char ch;

scanf("%c %d",ch,i); scanf("%c %d", &ch, &i);

Note:- The ‘&’ is not always needed if the identifier is a

pointer or a string type.

Page 21: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 21/27

Page 22: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 22/27

The conversion string in scanf/printf is wrong 

Incorrect I/O is can be the result of poorly

matched conversion strings in I/O statements.

These are wrong:

float x; double x;

scanf("%d",&x); scanf("%f",&x);

should perhaps be

float x; double x;scanf("%f",&x); scanf("%lf",&x);

Page 23: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 23/27

Page 24: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 24/27

  Arrays out of bounds 

C does not check the

limits of arrays. If an arrayis sized

data_type array[5];

and the you allow theprogram to write to

array[6] or more, C will

not complain. However

the computer might! Inthe worst case this could

cause the program to

crash.

Page 25: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 25/27

Page 26: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 26/27

  Locating a problem 

Complex bugs can be difficult to locate.

Here are some tips for fault finding:• Try to use local variables, in preference to global ones

for local duties. Never rely on global variables for

passing messages between functions.

• Check variable declarations and missing parameters.

• Check that a program has not run out of private

memory.

• Try retyping the program, or using a filter which strips

out any illegal characters which might have found

their way into a program

Page 27: Errors in C Program

8/12/2019 Errors in C Program

http://slidepdf.com/reader/full/errors-in-c-program 27/27

Ghanashyam K J


Recommended