Hi,
Can someone explain why this won't compile? At times I can instantiate an int and at times I cannot.. The code compiles absolutely fine if I remove this line. It's not a syntax error nor is it that testNum has been instantiated elsewhere..
Instantiating an int - won't compile..
Instantiating an int - won't compile..
- Attachments
-
- cap1.PNG (64.82 KiB) Viewed 2596 times
- darko.ilijevski
- Posts: 581
- Joined: 21 Mar 2017 16:57
Re: Instantiating an int - won't compile..
Hello,
When coding in C, you have to follow a certain code structure:
If you need to define functions used in the main() function, you need to defie them in the 'global declarations', so the functions are visible in the main function. So you should have a layout like this one:
Also it's possible to change the layout a bit, if you invoke the functions in the global parameters section of your code:
Try following this layout for your code and you won't have troubles with the issue you have. What you did is declaration of a new variable after the statements for your function. Try defining it in the appropriate part of your function (local variables), following the template I gave you in the above listing.
Best regards
When coding in C, you have to follow a certain code structure:
Code: Select all
#pre - processor directives
global declarations ;
[type] main()
{
local variables to function main ;
statements associated with function main;
}
Code: Select all
#pre - processor directives
global declarations ;
[type] f1()
{
local variables to function f1 ;
statements associated with function f1 ;
}
[type] f2()
{
local variables to function f2 ;
statements associated with function f2 ;
}
[type] main()
{
local variables to function main ;
statements associated with function main (you may now include f1() and f2() in your main()) ;
}
Code: Select all
pre - processor directives
global declarations ;
[type] f1() ;
[type] f2() ;
[type] main()
{
local variables to function main ;
statements associated with function main (you may now include f1() and f2() in your main()) ;
}
[type] f1()
{
local variables to function f1 ;
statements associated with function f1 ;
}
[type] f2()
{
local variables to function f2 ;
statements associated with function f2 ;
}
Best regards
BR,
Darko
Darko
Re: Instantiating an int - won't compile..
That works, I forgot about the various C89/C99/ANSI C rules! (XC16 Doesn't seem to care!)
Thanks!
Thanks!
- darko.ilijevski
- Posts: 581
- Joined: 21 Mar 2017 16:57
Re: Instantiating an int - won't compile..
I am glad that I could be of help.
Best regards
Best regards
BR,
Darko
Darko