CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

The correct form of coding in ccs

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GiG



Joined: 03 Sep 2021
Posts: 39

View user's profile Send private message

The correct form of coding in ccs
PostPosted: Tue Nov 23, 2021 12:36 am     Reply with quote

Hi
I had a question about the correct coding format
The format I use for coding is This
Code:
#device
#fuses
#etc

void1
{
function1
}
void2
{
function2
}
void3
{
function3
}
void4
{
function4
}
.
.
.


main()
{
setup1
setup2
while(1)
{
void1
void2
void3
void4
voids.....
}
}


I wanted to see the correct way and which method has less error? And there is a better template for coding
Is there any technical problem with coding in this format?
What is the appropriate template you suggest and why?


The next question is about defining variables in arguments!
In terms of coding, is it better to define variables at the beginning of the program?
Or be defined in the relevant argument?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Nov 23, 2021 2:32 am     Reply with quote

Basically standard C.

The function lines you have at the top, are function 'prototypes'. If they
actually contain code, then they are function definitions. In C, you must
have either a prototype or a definition before you access a function. If you
have your definitions before the main, you do not need to have prototypes
at all. So it is common to have a .h file containing the prototypes, load
this before the main, and then have the actual definition afterwards.
The advantage of this is then that the main becomes close to the top
of the program, rather that low down after the definitions. More
important, when you have many thousands of definitions, and some of
these are large....

Now on variables, ones declared 'at the beginning of the program(assuming
this is outside of any function), become 'global' variables. Accessible
for all functions, and holding their values when functions are exited.
Ones declared at the start of a function, are local variables to that function.
They don't actually 'exist' when the function exits (unless declared as
'static'). Any values in them can/will be overwritten when you are out
of the function. The memory they use can/will be re-used in other functions.
Advantage of these is they can't get written to by any other code, and
they save RAM. Downside is they need to be initialised each time the
function is called.
Generally only declare things as global, when they need to be accessed
from multiple locations. Keep these as rare as you can.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group