How to avoid Compile Error while defining Variables Last Updated : 05 Apr, 2019 Comments Improve Suggest changes 2 Likes Like Report 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: C #include <stdio.h> int main() { printf("%d", x); return 0; } Compile 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: C #include <stdio.h> int main() { int x; printf("%d", x); return 0; } Output: 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: C #include <stdio.h> int main() { { int x = 5; } printf("%d", x); return 0; } Compile 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 How 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: C #include <stdio.h> int main() { { int x = 5; printf("%d", x); } return 0; } Output: 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: C #include <stdio.h> int main() { char* x; int i = x; printf("%d", x); return 0; } Warning: prog.c: In function 'main': prog.c:7:13: warning: initialization makes integer from pointer without a cast [-Wint-conversion] int i = x; ^ Create Quiz Comment Y YashKhandelwal8 Follow 2 Improve Y YashKhandelwal8 Follow 2 Improve Article Tags : C Language Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like