Instantiating an int - won't compile..

General discussion on mikroC PRO for FT90x.
Post Reply
Author
Message
hrfraz
Posts: 9
Joined: 04 Aug 2017 18:07

Instantiating an int - won't compile..

#1 Post by hrfraz » 24 Aug 2017 19:45

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..
Attachments
cap1.PNG
cap1.PNG (64.82 KiB) Viewed 2586 times

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: Instantiating an int - won't compile..

#2 Post by darko.ilijevski » 27 Aug 2017 00:54

Hello,

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;
}
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:

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()) ;
}
Also it's possible to change the layout a bit, if you invoke the functions in the global parameters section of your code:

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 ;
}
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
BR,
Darko

hrfraz
Posts: 9
Joined: 04 Aug 2017 18:07

Re: Instantiating an int - won't compile..

#3 Post by hrfraz » 28 Aug 2017 16:04

That works, I forgot about the various C89/C99/ANSI C rules! (XC16 Doesn't seem to care!)
Thanks!

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: Instantiating an int - won't compile..

#4 Post by darko.ilijevski » 29 Aug 2017 18:32

I am glad that I could be of help.
Best regards
BR,
Darko

Post Reply

Return to “mikroC PRO for FT90x General”