Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C, typing arguments after declaring??

  • 07-06-2011 12:02pm
    #1
    Registered Users, Registered Users 2 Posts: 307 ✭✭


    Hi folks

    I have recently seen functions (C) that type their vars AFTER the function argument string...

    instead of what I normally would do:
    void function (int x, register const char * c)
    {/*code*/}
    

    its written:
    void function (x,c) 
    int x; 
    register char * c; 
    {/*code*/}
    

    Is either of these more correct? Or more efficient? Why have two ways of doing the same thing?
    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    To support legacy stuff. These days the compiler will automatically do this stuff for you. Only really needed if you are writing very low level embedded C on some obscure platform.


  • Registered Users, Registered Users 2 Posts: 307 ✭✭wolf99


    Thanks srsly78
    Turns out at the end o d day it prob will end up running in an embedded env. but I haven't had problems using the first way in embedded env. before...
    Thanks anyhoo, will keep on as I am so.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    You probably use GCC right? If so don't worry, it will automatically make various optimisations like "register" and so on. By obscure I meant something GCC wasn't available/optimised for, and I can't actually think of anything what meets that description these days!

    Trying to "beat" the compiler is not worth it these days.


  • Registered Users, Registered Users 2 Posts: 307 ✭✭wolf99


    TBH I havent clue what the compiler itself is, am using DevC++ env though.
    But, I routinely use others like keil uvision developing for microcontrollers usually does need memory location qualifiers as they can optimise stuff I dont want optimised or vice-versa depending on the compiler and chip and application

    eg using xdata for 8051 external memory and so on

    so from habit I normally through them in there anyway so I know exactly where everything is.

    The functions im using the register qual. for are small string handling functions and so could deal with large length strings (64bytes [large on a micro!]) so Id rather force optimization anywhere I can :)

    though in this case, after more thought, it probably doesnt make a diff, as you say.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Ok, that stuff is outside my area tbh. I do suspect your IDE is using gcc under the hood tho.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 307 ✭✭wolf99


    Just checked their site (Dev C++), its a Mingw port of GCC, and I have no idea what that means ;)

    uVision of course uses dif compilers for dif chips, C51 for 8051 and so on


  • Registered Users, Registered Users 2 Posts: 1,340 ✭✭✭carveone


    The former is standard ANSI C function usage. The latter is the style used in the original Kernigan and Richie "The C Programming Language" book, before ANSI came along and standardised things. As far as I know, ANSI C90 obsoleted this style anyway.

    You should definately use the standard ANSI C usage, mainly because of prototyping. If you use the K&R usage, and a function declaration for "function" is not in scope when you call the function, x and c will undergo default argument promotion (as in variadic arguments like printf). This can be highly problematic and the compiler is not obliged to check anything for you in this case. Use the ANSI C usages and save yourself some grief.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    GCC is opensource, they compiled it to work with mingw is all that means.

    Here is the thing, thousands and thousands of man-years of effort have gone into compiler R&D over the years. What we have today is so much more advanced than in the past. I usually do desktop development, with a bit of mobile stuff on the side. I trust the compiler to produce optimal code, I don't try to second guess it. The only situation where GCC wouldn't be optimised is on some wierd obscure piece of hardware (possibly like you described, interfacing with exotic memory controller or whatever).


  • Registered Users, Registered Users 2 Posts: 307 ✭✭wolf99


    wow, thanks for all the follow on info folks, good to know this stuff! I usually trying to ignore discussions I see on forums that start talking about C90, C99 yada yada and so on but this has given me a bit of a handle on what its about... especially as I started using K&R's book a short while ago for reference when I forget things!
    Thanks again folks :)


Advertisement