What is the default storage class type for function arguments?  See my claims in the question body

Posted on 2004-10-07
Last Modified: 2013-11-15
In KR and many autors of C programming, it is saying all about storage class type for various variables depending upon their storage specifier or by the default storage type if no storage specifier is mentioned.  In a local variable declaration, ie; variables declared inside a function are given an auto stoarge class type by default if no storage class specifer is prefixed at the variable declaration.  It is saying that function arguments are of local scope and so it is also by default auto. In, KR it is saying that the only allowable storage specifier for function argument is register only and by default it is auto.  But if we give the storage type specifier auto prefixed to a function argument it will throw a compile error saying that auto is not allowed here.  If by default it can take auto, then why can't I put auto explictly here only?  We can give only register storage class specifier only there.  That means, function arguments are register type by default.  Then also a question comes?  It is saying that address of a register variable is not allowed in C. Indeed, if the register type variable cannot be stored in a register it will be reduced to auto type storage class.  Then also we cannot get the address of that variable using an & operator.  But here in function argument, eventhough if we specify those as register types, we can get the addess of that variables without any compile error or runtime error.  It seems contradictory.  

For Eg:-

void main()
   int a=10,b=20,c,fun(int,int);

int fun(register int x,/*auto */ int y)
  int z,*ptrtox,*ptrtoy,*ptrtor,*ptrtoarray;
  register int r=50,array[3]={9,8,7};
   ptrtox = &x;/*Why this is allowed? */
   ptrtoy = &y;
   /*ptrtor = &r;*//*Enabling this line will throw compile error*/
   ptrtoarray = array;/*Why this is allowed, arrays are of contigious allocation and they cannot be stored in a register, then why register type is allowed here? */
   /* Then why it is restricted to put auto before the variable y in the function argument */
   z=*ptrtox + *ptrtoy;
   printf("First array element is %d\n",*ptrtoarray);
   printf("First function argument of register type is %d\n",*ptrtox);
   /*printf("Value in the register variable r is %d",*ptrtor);*/

Function arguments and local variables inside to the function are stored on the stack only according to the C memory model and that is of the auto type by default if no storage type specifier defined.  Try to print the address of each variables(including variables in function arguments) for the above program for clarification.  

It may really a confusion and challege to the experts I think.  Seeking for a favourable answer that is logical in the sense.

Thomas Mathew.
( C is the backbone to Software Technology that makes it a Sea ).
Question by:thomasmathewk
LVL 22

Accepted Solution

grg99 earned 100 total points
ID: 12247706
C is a mighty fuzzy language.  Here's on explanation, from a code-generator's point of view:

The code generator cant in general assign registers to parameters, for many reasons:

The function may be separately compiled and there's no easy way for the compiler to pass the register name between the compile sessions.

The hardware may have it's own parameter passing protocol, for instance in a RISC machine there are often special register frames, on a VAX there are special parameter blocks.  

Putting a parameter in a register makes it difficult for a function to call itself, either firectly or indirectly.

it can't or shouldnt make parameters "static", as that conflicts with being able to pass new values in, or have different values for the same parameter in recusrsive calls.

Also I think somewhere some standard says the compiler is free to ignore "register".  If it's a two-pass compiler it may be seeing the "&Register" being used and then in the next pass it can ignore the "register" attribute.   Alternatively it's possible for a really smart compiler to tremporarily store the register somewhere, take the address of THAT, then after its use reload the register from memory (which won't work if that address gets stashed away somewhere).

I'm not too surprised you can't say "auto" in a parameter declaration, that 's the only reasonable attribute fro a parameter.  Neither "register" or "static" make any sense here.


Expert Comment

ID: 12253659
Well, what I have read so far, function parameters are generally stored on stack. When a function is called, first the parameters are put on the stack and then the assmbly CALL is made for that function. Inside the function stack, the parameters are read and then processed. I read the return values are kept in the stack if possible but I have not read anywhere that parameters are passed through stacks.
LVL 11

Expert Comment

ID: 12254412
>>When a function is called, first the parameters are put on the stack and then the assmbly CALL is made for that function.

>>... but I have not read anywhere that parameters are passed through stacks.

doesnt the above two contradict ?
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

LVL 12

Expert Comment

ID: 12260422
Hi pankajtiwary,
> Well, what I have read so far, function parameters are generally stored on stack
Not necessarily. Up to a certain number of arguments, SPARC passes them via registers.

Almost all architectures return via a register. C allows only a single return value, so that fits nicely.

IMHO, the "register" storage class is obsolete. A good optimizer knows which variables offer most gain in a register.

BTW: There are architectures which mirror registers in memory, so they have an "address" - the 6510 is an example (6502 in general?)



Expert Comment

ID: 12269358
Ok, Here is my first question to origional poster.

What kind of compiler are you using?  Is it C99 standard?

Declaring register variable and decaring function inline are just request to compiler and not command. This is difference between inline function and macros.

Now, when you say c=fun(a,b);
comiler simply pushes these two parameters on stack and call the fun. I also suggest you try putting function declaration and/or changing the position of function fun before the calee.

Now when the control gets into the function it is upto the loader( I am not sure at what phase it decides to use register variable) to decide depending upon free CPU registers, it will  really allocate register variable for your variable. I believe, if you are running on x86 platform there is little chance that it will get free CPU register and your request getting  satisfied.

And about passing a variable through register is only happens by default in some RISC processors''s RTOS compilers and not in x86 desktop machines.

So in order to compete, new x86 compiler came up with inline function. If you make above function inline and see whether it works. I positively feel, it will not. DO use force inline and not simple inline.

Passing value through CPU register has some pros and cons. Advantange is, it will be fast and disadvantage is, you can not have recusive function. And some code does not work and needs to be ported by implementing its own stack.

Hope this helps....


Author Comment

ID: 12368259
Sorry for the delay in responding to the comments.  As per your question, the compiler that I am using is the GNU Compiler Collections (gcc ver 3.xx) bundled with RedHat 8.0 or 9.0 and I assume this to be compliant with C99 standards, since C99 specifications are an open source effort.  
Then still also my sceptics are not yet fully cleared as I haven't got a vivid and reasonable answer to the question I posted.  Why my core quest to this post is about the default storage type of function parameters?  Is it register or auto?  The main thing that induce me to start a post on this topic is because when I put a storage specifier as auto infrom a function parameter declaration, I am getting a compiler error saying that auto is not allowed here.  And from books I read that the only allowed stoarge specifier here is register.  So by default is it register?  I too also agree that static and global are absurd and illogical to here.  I do believe and assume that the default stoarge type here may be auto only.  Then why cann't I put a specifier here as auto infront of it?  So please tell me the default storage type of the function parameters and I am expecting a logical answer to this question solely.  Thanks and regards to all who made a glance and comment to my post yet and in the future too.

Thomas Mathew.
(C gave the jump-start to Computer Software Technology that advances to today's state, which was yesterday's dream).

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question