How to avoid Compile Error while defining Variables
Variables: A variable is the name given to a memory location. It is the basic unit of storage in a program.
- The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
- All the variables must be declared before use.
How to declare variables?
We can declare variables in common languages (like C, C++, Java etc) as follows:

where: datatype: Type of data that can be stored in this variable. variable_name: Name given to the variable. value: It is the initial value stored in the variable.
How to avoid errors while creating variables?
- The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn’t been declared yet, an “undeclared identifier” compile-error will occur.
Example:
#include <stdio.h>intmain(){printf("%d", x);return0;}chevron_rightfilter_noneCompile Errors:
prog.c: In function 'main': prog.c:5:18: error: 'x' undeclared (first use in this function) printf("%d", x); ^ prog.c:5:18: note: each undeclared identifier is reported only once for each function it appears in - No initial value is given to the variable: This error commonly occurs when the variable is declared, but not initialized. It means that the variable is created but no value is given to it. Hence it will take the default value then. But in C language, this might lead to error as this variable can have a garbage value (or 0) as its default value. In other languages, 0 will be its default value.
Example:
#include <stdio.h>intmain(){intx;printf("%d", x);return0;}chevron_rightfilter_noneOutput:
0
- Using variable out of its Scope: Scope of a variable is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can be determined at compile time and independent of the function call stack.
Example:
#include <stdio.h>intmain(){{intx = 5;}printf("%d", x);return0;}chevron_rightfilter_noneCompile Errors:
prog.c: In function 'main': prog.c:5:18: error: 'x' undeclared (first use in this function) printf("%d", x); ^ prog.c:5:18: note: each undeclared identifier is reported only once for each function it appears inHow to Correct the above code: Declare the variable x before using it in the outer scope. Or you can use the already defined variable x in its own scope
Example:
#include <stdio.h>intmain(){{intx = 5;printf("%d", x);}return0;}chevron_rightfilter_noneOutput:
5
- Creating a variable with an incorrect type of value: This arises due to the fact that values are implicitly or explicitly converted into another type. Sometimes this can lead to Warnings or errors.
Example:
#include <stdio.h>intmain(){char* x;inti = x;printf("%d", x);return0;}chevron_rightfilter_noneWarning:
prog.c: In function 'main': prog.c:7:13: warning: initialization makes integer from pointer without a cast [-Wint-conversion] int i = x; ^
Recommended Posts:
- Defining, Creating and Dropping a MongoDB collection
- How to avoid NullPointerException in Java using Optional class?
- Difference between Compile Time Errors and Runtime Errors
- How to avoid dropdown menu to close menu items on clicking inside ?
- How to test a URL for 404 error in PHP?
- JavaScript | Error name Property
- jQuery | error() Method
- JavaScript | Error message Property
- Unreachable Code Error in Java
- How to solve “Submit is not a function” error in JavaScript ?
- How to remove error Call to undefined function curl_init()?
- Where does PHP store the error log? (php5, apache, fastcgi, cpanel)
- CSS | Variables
- C# | Variables
- Python Variables
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



