The above code runs perfectly. But if the integer and array initalization part preceeding the while loop which is currently comment, is enabled the compiler gives error. I'm a rookie in C programming any help is appriciated.
It gives an error... and what does the error message say?
Error messages are very useful informations to find out why they are and how to rectify the error.
Put both the commented lines immediately after the '#include' statements so they are outside main().
Then remove the line starting 'char' inside the while() loop.
Your problem is with the scope of the variables, I think in this case you want the seg_code[] array to be global, that means moving it outside of any functions, including main(). When you use it there is no longer any need to use 'char' again as that would try to re-declare it again.
Hi
It gives an error... and what does the error message say?
Error messages are very useful informations to find out why they are and how to rectify the error.
Put both the commented lines immediately after the '#include' statements so they are outside main().
Then remove the line starting 'char' inside the while() loop.
Your problem is with the scope of the variables, I think in this case you want the seg_code[] array to be global, that means moving it outside of any functions, including main(). When you use it there is no longer any need to use 'char' again as that would try to re-declare it again.
You did not declare the size of P1 in the declaration for starters. Unless your compiler defaults
to int for a variable, mine does not.
Then you need to remove the definition of variables seg_code and i inside the while loop,
and uncomment their dupes just prior to the while loop. Reinitializing variables inside a while
loop is a waste of code space if the compiler does not optimize that out, which mine does,
yours may not. Removing them inside while() and instantiating them inside main() makes
them local to main(), if thats your intention. I am not sure how the ANSI standard treats declar-
ations inside while() that is inside of main(), I assume they are treated the same as far as
scope is concerned.
Code:
int i;
char seg_code[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};
while(1)
{
int i;
char seg_code[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};
I concur with Brian, think seg_code should be outside main(), as I would guess its global
in usage for your application. Unless of course its not just a LUT but is dynamic in usage,
created and changed in other code.
A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration. This tutorial guides you on how to use C variable scope.
It may help to specify the size of array by putting 10 between the [ and ]. Some compilers will work it out for themselves, some have to be told and some will just set up a pointer to the array.
The more specific you are, the more predictable operation will be.
I was reading a thread titled "strlen vs sizeof" on CodeGuru, and one of the replies states that "it's anyways [sic] bad practice to initialie [sic] a char array with a string literal." Is this t...
Interesting comments - I think it boils down to:
If you tell it the size it will use it.
If you let it size itself (dynamically) it may take into account the null terminator and be bigger than you anticipated.