0% found this document useful (0 votes)
2K views6 pages

SYNTAX AND LOGICAL ERRORS IN COMPILATION PDF

This document discusses common programming errors in C, specifically syntax errors and logical errors. It provides examples of each type of error, such as missing semicolons, incorrect syntax, logical flaws in conditional statements, and division by zero. It also covers how C handles errors through return values, errno, and exit statuses to indicate success or failure.

Uploaded by

Abhir999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views6 pages

SYNTAX AND LOGICAL ERRORS IN COMPILATION PDF

This document discusses common programming errors in C, specifically syntax errors and logical errors. It provides examples of each type of error, such as missing semicolons, incorrect syntax, logical flaws in conditional statements, and division by zero. It also covers how C handles errors through return values, errno, and exit statuses to indicate success or failure.

Uploaded by

Abhir999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SYNTAX AND LOGICAL ERRORS IN

COMPILATION

Programming Errors in C

DEFINATIONS:

1. Errors are the problems or the faults that occur in the program, which makes the behavior
of the program abnormal, and experienced developers can also make these faults.
Programming errors are also known as the bugs or faults, and the process of removing
these bugs is known as debugging.

These errors are detected either during the time of compilation or execution. Thus, the
errors must be removed from the program for the successful execution of the program.

2. Error is an illegal operation performed by the user which results in abnormal working
of_the_program.
Programming errors often remain undetected until the program is compiled or executed.
Some of the errors inhibit the program from getting compiled or executed. Thus errors
should be removed before compiling and executing.

The most common errors can be broadly classified as follows.

Syntax errors and logical errors

Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are
known as syntax errors. This compiler error indicates something that must be fixed before the
code can be compiled. All these errors are detected by compiler and thus are known as
compile-time_errors.
Most frequent syntax errors are:
• Missing Parenthesis (})
• Printing the value of variable without declaring it
• Missing semicolon like this:
// C program to illustrate
// syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;

printf("%d", (x, y)) // semicolon missed


}

Error:

error: expected ';' before '}' token

Syntax of a basic construct is written wrong. For example : while loop

// C program to illustrate
// syntax error
#include<stdio.h>
int main(void)
{
// while() cannot contain "." as an argument.
while(.)
{
printf("hello");
}
return 0;
}
Error:

Error:
error:expected expression before '.' token
while(.)
Logical Errors : On compilation and execution of a program, desired output is not obtained
when certain input values are given. These types of errors which provide incorrect output but
appears to be error free are called logical errors. These are one of the most common errors
done_by_beginners_of_programming.
These errors solely depend on the logical thinking of the programmer and are easy to detect if
we follow the line of execution and determine why the program takes that path of execution.

// C program to illustrate
// logical error
int main()
{
int i = 0;

// logical error : a semicolon after loop


for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
getchar();
return 0;
}
No output
As such, C programming does not provide direct support for error handling but being a system
programming language, it provides you access at lower level in the form of return values. Most
of the C or even Unix function calls return -1 or NULL in case of any error and set an error
code errno. It is set as a global variable and indicates an error occurred during any function call.
You can find various error codes defined in <error.h> header file.

So a C programmer can check the returned values and can take appropriate action depending on
the return value. It is a good practice, to set errno to 0 at the time of initializing a program. A
value of 0 indicates that there is no error in the program.

errno, perror(). and strerror()


The C programming language provides perror() and strerror() functions which can be used to
display the text message associated with errno.

• The perror() function displays the string you pass to it, followed by a colon, a space, and
then the textual representation of the current errno value.
• The strerror() function, which returns a pointer to the textual representation of the
current errno value.

Let’s try to simulate an error condition and try to open a file which does not exist. Here I’m
using both the functions to show the usage, but you can use one or more ways of printing your
errors. Second important point to note is that you should use stderr file stream to output all the
errors.

#include <stdio.h>
#include <errno.h>
#include <string.h>

extern int errno ;

int main () {

FILE * pf;
int errnum;
pf = fopen ("[Link]", "rb");

if (pf == NULL) {

errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
} else {

fclose (pf);
}

return 0;
}
When the above code is compiled and executed, it produces the following result –

Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory

Divide by Zero Errors


It is a common problem that at the time of dividing any number, programmers do not check if a
divisor is zero and finally it creates a runtime error.

The code below fixes this by checking if the divisor is zero before dividing

#include <stdio.h>
#include <stdlib.h>

main() {

int dividend = 20;


int divisor = 0;
int quotient;

if( divisor == 0){


fprintf(stderr, "Division by zero! Exiting...\n");
exit(-1);
}

quotient = dividend / divisor;


fprintf(stderr, "Value of quotient : %d\n", quotient );

exit(0);

When the above code is compiled and executed, it produces the following result −

Division by zero! Exiting...

Program Exit Status


It is a common practice to exit with a value of EXIT_SUCCESS in case of program coming out
after a successful operation. Here, EXIT_SUCCESS is a macro and it is defined as 0.

If you have an error condition in your program and you are coming out then you should exit with
a status EXIT_FAILURE which is defined as -1. So let’s write above program as follows

#include <stdio.h>
#include <stdlib.h>

main() {
int dividend = 20;
int divisor = 5;
int quotient;

if( divisor == 0) {
fprintf(stderr, "Division by zero! Exiting...\n");
exit(EXIT_FAILURE);
}

quotient = dividend / divisor;


fprintf(stderr, "Value of quotient : %d\n", quotient );

exit(EXIT_SUCCESS);
}

When the above code is compiled and executed, it produces the following result −

Value of quotient : 4

You might also like