Jump to content

Proper C Etiquette

tengarp

So I'm trying to teach myself C (to be able to program an Arduino) through various websites and I was just wondering if there is any proper coding etiquette involved with it. I was taught python in a class and there i learned to keep my code nice and organized, and to comment a lot so that the code s easier to read, but is there anything else I should know? Such as should i put " return 0 " at the end of every print statement or not? Any tips you could give me would be very much appreciated! Also if you have any website recommendations/youtube videos i should watch to learn C that would be nice too. Thanks!

 

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

Link to post
Share on other sites

You know there is no real magic bullet for proper C Etiquette...although admittedly there are some rules that should be follow (but doesn't affect how the code runs).

The first is always return the main function...Personally I don't care that the specification allows for leaving the line out...it just makes sense to put in the return statement.

 

Having gone through many different processors who requires different code, there are many different mentalities behind writing and commenting in code.

 

In general this is the rules I follow:

Commenting:

  • Put a comment at the top of each function, just as a quick description.  If it returns something, say what it returns...in the event of a pointer make a note whether the pointer needs to be freed.
  • Comments inside a function should be used only to describe what is happening, if not obvious, or if you need to make note of something (e.g. if(((a<<b)>>a)+i++!=3534) isn't very obvious and shouldn't be done, but if you do comment)

Coding:

  • Try making everything readable.  Nothing is worse than going back to your code to find variables named a b c d e f... when they could have been named index, maximum, minimum...etc.
  • Try avoiding insanely long lines of code, you can break it down so that they are on different lines (and it won't affect the processing)
  • This one is only my pet peeves, nested if statements...yes they can't always be avoided, but if you have code with 6 tabs to the current line there is something wrong :P  This is just a personal opinion though, built through a few processors who disliked them...with that said I have seen people who did this
    int main() {    ...    if(i < 5) {         ... //50 lines of code         if(z > 5) {             ...//50 more lines of code         }         else {             return 2;         }    }    else {        return 1;     }   return 0;}//The more reasonable approach (in my mind)int main() {    ...    if(i >= 5)        return 1;    ...//50 lines of code    if(z <= 5)        return 1;    ..//50 lines of code    return 0;}
  • For myself, I enjoy ) { but, there are many people who prefer ) \n { which is alright

Macros (#define):

  • This is a tricky one, but don't touch function macros until you have learned the language fully (there are way to many errors)
  • In general though macros can be good when you need to re-write multiple lines of code that repeat itself, expect with a few name changes
  • If you plan to work in a group, don't use function macros
  • When learning, use macros for constants like #define PI 3.1414... or for header guards.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

You know there is no real magic bullet for proper C Etiquette...although admittedly there are some rules that should be follow (but doesn't affect how the code runs).

The first is always return the main function...Personally I don't care that the specification allows for leaving the line out...it just makes sense to put in the return statement.

 

Having gone through many different processors who requires different code, there are many different mentalities behind writing and commenting in code.

 

In general this is the rules I follow:

Commenting:

  • Put a comment at the top of each function, just as a quick description.  If it returns something, say what it returns...in the event of a pointer make a note whether the pointer needs to be freed.
  • Comments inside a function should be used only to describe what is happening, if not obvious, or if you need to make note of something (e.g. if(((a<< B)>>a)+i++!=3534) isn't very obvious and shouldn't be done, but if you do comment)

Coding:

  • Try making everything readable.  Nothing is worse than going back to your code to find variables named a b c d e f... when they could have been named index, maximum, minimum...etc.
  • Try avoiding insanely long lines of code, you can break it down so that they are on different lines (and it won't affect the processing)
  • This one is only my pet peeves, nested if statements...yes they can't always be avoided, but if you have code with 6 tabs to the current line there is something wrong :P  This is just a personal opinion though, built through a few processors who disliked them...with that said I have seen people who did this
    int main() {    ...    if(i < 5) {         ... //50 lines of code         if(z > 5) {             ...//50 more lines of code         }         else {             return 2;         }    }    else {        return 1;     }   return 0;}//The more reasonable approach (in my mind)int main() {    ...    if(i >= 5)        return 1;    ...//50 lines of code    if(z <= 5)        return 1;    ..//50 lines of code    return 0;}
  • For myself, I enjoy ) { but, there are many people who prefer ) \n { which is alright

Macros (#define):

  • This is a tricky one, but don't touch function macros until you have learned the language fully (there are way to many errors)
  • In general though macros can be good when you need to re-write multiple lines of code that repeat itself, expect with a few name changes
  • If you plan to work in a group, don't use function macros
  • When learning, use macros for constants like #define PI 3.1414... or for header guards.

 

Okay, thankyou for your response! :D

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

Link to post
Share on other sites

Learn pointers and learn them well. I'm still confused with them. Bad teacher is bad  :(

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
Share on other sites

Link to post
Share on other sites

My teacher just let us watch this: https://www.youtube.com/watch?v=6pmWojisM_E

Now I perfectly understand how pointers work :D

Oh my lord I actually watched this lol. I get confused when I should use * or & :P

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
Share on other sites

Link to post
Share on other sites

Oh my lord I actually watched this lol. I get confused when I should use * or & :P

To me the ampersand looks a bit like a capital A:

& A

Therefore & stands for &ddress. Principle of exclusion then gets me to the

meaning of *. Haven't confused the two since I came up with this. :)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

My teacher just let us watch this: https://www.youtube.com/watch?v=6pmWojisM_E

Now I perfectly understand how pointers work :D

haha, that is such an odd video...effective but odd.

The unfortunate thing is they didn't get into functors/function pointers...got to tell you, it scared me when I first saw something like this "double (*funcp)(double, double (*funcptwo)(double))"

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

haha, that is such an odd video...effective but odd.

The unfortunate thing is they didn't get into functors/function pointers...got to tell you, it scared me when I first saw something like this "double (*funcp)(double, double (*funcptwo)(double))"

The potential power they have is pretty mighty as far as I'm aware (without being

an uber-C guru myself TBH), but the potential for creating undecipherable unholy

messes seems almost equally great. :lol:

Then again, pointers seem to have that capability in general, regardless of how

awesome (at least IMHO) the concept itself is. ;)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×