Before declaring local variable we must be know that:
1- The scope of local variables will be within the function only.
2- Variables should be declared in the C program before to use.
Declaring the variable means defoliation of variable, means that what is the name of your variable and how much memory space reserved for this variable.
where Initializing means that assigning the value to variable. You can declare and initialize the variable at once.
Now we take an example
#include void test(); int main() { int marks = 345, t_marks = 500; printf("\n Marks = %d \n Total Marks = %d", marks, t_marks); }
if you run this code this will return
Marks=345;
Total Marks=500
Note: \n us used to take control on next line.
Now take an other example with two function one is main and other is test function.
#include void test(); int main() { int marks = 345, t_marks = 500; printf("\n Marks = %d \n Total Marks = %d", marks, t_marks); } void test() { printf("\n Marks = %d \n Total Marks = %d", marks, t_marks); }
If we Run this code it will return Un declaration of marks and t_marks error because / marks and t_marks are local variables of main function and these variables are having scope within this main function only. These are not visible to test function. If you try to access these variable in test function, you will get 'marks' undeclared and 't_marks' undeclared error
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++