C programming errors

In this article, we are going to learn some of the C programming language errors and their causes, solutions.
Submitted by Sudarshan Paul, on June 25, 2018 [Last updated : March 13, 2023]

Making errors in C programming is the most elementary step towards developing your grip in the subject. You must know the proper debugging tips and tricks to identify the error. As a beginner in C you might have encountered many of them and with the basic knowledge you can obviously find the errors and finish the compilation of your program successfully. Here we will mention some common errors that beginners in C language commit and ways to tackle them.

There are mainly three types of C programming errors, which generally a developer phases doing writing code in C language.

Types of C Language Errors

  1. Compiler Time Error
  2. Run Time Error
  3. Warnings

1) Compile time error

The errors occurring during the compilation of a program leading to failure in the compilation are termed as compile time error. They are generally caused when the programmer does not follow the programming rules.

2) Run time error

These errors generally occur due to the incorrect logic used while programming. They are difficult to detect as compared to compile time errors. The programmer must check the code from the beginning in order to check where the logic is wrong.

3) Warnings

The warnings are generated by the compiler when the compiler detects some abnormality in the coding way or pattern. The program might occasionally crash or misbehave if warnings are neglected but that rarely occurs. In C warnings can be neglected but compiler errors and runtime errors should be corrected to obtain the desired output.

Some Common C Language Errors and Their Causes

1) Undeclared Variables

int main()
{
	if(x>5)
		printf("x is greater than 5\n");
}

The variable x used, is undeclared in the function. Hence compiler is not aware of any such variable.

2) Uninitialized variables

int lc;
while(lc<100)
{
	lc++;
}

The loop counter (lc) used with the while loop is not initialized. It is compulsory to initialize the loop counter before using while loop. Hence the compiler will not enter the loop.

3) Using assignment operator (=) to check equality

char x='Y';

while(x='Y')
{
	printf("x is equal to y\n");
}

Here, in the while loop condition x is assigned to y with a single equal to sign which is wrong. In order to check equality == sign should be used.

4) Incorrect use of semicolon

int main()
{
	int a = 0 //Semicolon Undefined
	while(i < 5);
	{
		printf("Hello World");
		i++;
	}
	return 0;
}

In the above cod snippet, the while loop has been terminated with a semicolon which is undesirable. Loop statements are never terminated with a semicolon. Therefore the loop body doesn't get executed.

5) Missing operator in scanf function

#include<stdio.h>

int main()
{
	int num1, num2;
	printf("\nEnter the first number:\t");
	scanf("%d", num1); 
	return 0;
}

In the above case, & ampersand operator is not used in the scanf function. The compiler will successfully compile the program with no errors. But during runtime the user will not be able to give the input to the program. This is a very common mistake that beginners make.

C Language Tutorial »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.