but i is not declared so what it return why?Code:a[]={0,1,2,3,4}; a[i]=i;
thank you..!!
but i is not declared so what it return why?Code:a[]={0,1,2,3,4}; a[i]=i;
thank you..!!
If i is not declared the program won't compile so it won't return anything. If i is declared but not initialized then it is undefined what value i will contain.
Jim
ideone says
You can see the output for yourselfcc1: warnings being treated as errors
prog.c:1: error: data definition has no type or storage class
prog.c:1: error: type defaults to ‘int’ in declaration of ‘a’
prog.c:2: error: ‘i’ undeclared here (not in a function)
prog.c:2: error: data definition has no type or storage class
prog.c:2: error: type defaults to ‘int’ in declaration of ‘a’
In other words What is the default value of integer in c
Well that's ok,because then the compiler says what's wrong...but if the code looks like this
then the compiler will tell nothing,but i is uninitialized so actually we have a logic error here,which of course is more difficult to detect(in large programs) than the syntactic onesCode:int main(void) { int i; int a[]={0,1,2,3,4}; a[i]=i; return 0; }
Actually my compiler tells me about using uninitialized variables.then the compiler will tell nothing,but i is uninitialized
Jimmain.c||In function ‘main’:|
main.c|4|warning: variable ‘a’ set but not used [-Wunused-but-set-variable]|
main.c|5|warning: ‘i’ is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 errors, 2 warnings ===|
If you are using Linux you are probably using gcc which is what I used. What compiler switches are you using? Are you at least using -Wall -Wextra?
Jim
No flags at all. gcc -o programName fileName.c .
I used what you did (h.c was the file i created and compiler said nothing again.) I am via putty(is this relevant ? )
Code:linux11:/home/users/std10093>pico h.c linux11:/home/users/std10093>gcc -o -Wall -Wextra h h.c gcc: h: No such file or directory linux11:/home/users/std10093>gcc -Wall -Wextra -o h h.c linux11:/home/users/std10093>h Segmentation fault linux11:/home/users/std10093>gcc -o h h.c linux11:/home/users/std10093>h Segmentation fault linux11:/home/users/std10093>
Here is a complete list of the warning options I use:
You cant try add them one at a time, I recommend using at least the -Wall -Wextra -pedantic every time you compile.-Wshadow"
-Winit-self"
-Wredundant-decls"
-Wcast-align"
-Wundef"
-Wfloat-equal"
-Winline"
-Wunreachable-code"
-Wmissing-declarations"
-Wmissing-include-dirs"
-Wswitch-enum"
-Wswitch-default"
-Wmain"
-pedantic-errors"
-pedantic"
-Wextra"
-Wall"
-g"
-std=c11"
Jim