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

error compiler with symbol "_"

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



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

error compiler with symbol "_"
PostPosted: Sat Apr 18, 2020 10:46 am     Reply with quote

Hi, I try make portable a code from mplabx, but i get error with the simbol "_"
why?


for example
Code:
uint8_t* AddStringToBuf(uint8_t *_buf, char *_string)
{
  uint16_t _length = strlen(_string);
  _buf[0] = _length >> 8;
  _buf[1] = _length & 0xFF;
  _buf+=2;
  strncpy((char *)_buf, _string, _length);
  return _buf + _length;
}



this operation strlen(_string) make error if i write the varible with _
So some variable i shoud be declare without "_"

Code:
uint8_t* AddStringToBuf(uint8_t *_buf,  char *string)

  uint16_t _length = strlen(string);         
  _buf[0] = _length >> 8;                     
  _buf[1] = _length & 0xFF;
  _buf+=2;                                     
  strncpy((char *)_buf, string, _length);     
  return _buf + _length;                           
}   


The code work fine... but to end i have rename the variables.
maybe its stupid, but i want know why happen this.


Thanks
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 18, 2020 11:02 am     Reply with quote

might be a 'C' syntax that variables MUST begin with a LETTER.....

CCS C mostly follows K&R C, though I've never been formally taught any C....

the pros here WILL know....

To me using an 'underscroe' character is bad...too hard to see on a screen using my 67 year old eyeballs....
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Sat Apr 18, 2020 11:23 am     Reply with quote

C has restrictions on the use of _.

Quote:

predefined macro names shall begin with a leading underscore followed by an uppercase letter or a second underscore.”


By avoiding leading underscores in your identifiers, you avoid the possibility of your identifier conflicting with a predefined symbol defined by the compiler implementation. Depending on the context, conflicts like this can be difficult to diagnose, so completely avoiding the possibility of a conflict is a good practice.

Coding standards and/or style guides within an organization will often prohibit leading underscores (to avoid the conflicts discussed above), and sometimes establish a naming convention for various types of identifiers, including whether or not to use underscores to separate words.

Because CCS by default does not use case, you have to avoid _ as the
leading character.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 18, 2020 12:10 pm     Reply with quote

temtronic, remember that K&R pdf I showed you how to get the other day ?
It's in there. Search for the word: underscore
See section: 2.1 Variable Names
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Sat Apr 18, 2020 12:15 pm     Reply with quote

I like this answers...

Thanks you!
jeremiah



Joined: 20 Jul 2010
Posts: 1355

View user's profile Send private message

PostPosted: Sat Apr 18, 2020 1:07 pm     Reply with quote

7.1.3 of the standard also has:

Quote:

— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.


So as Ttelmah alluded to, using the _ at the beginning of an identifier (macro, variable, function, etc.) is technically "undefined", as defined in the standard:

Quote:

If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 18, 2020 3:13 pm     Reply with quote

heck, I downloaded it, got to read about 3 pages, then everyone wanted THEIR stuff done....I did run the plow through the 1/2 ac garden once, hauled a 70' black walnut tree home in pieces, lots of pieces.Only 3 pieces left, easch about 3200# and at top of steep hill....sigh.
full 16 hr days are getting 'challenging', at 67 I get tired for some silly reason.
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