Can the Parameters of a Function in C be Declared Static?

Question: Can the Formal Parameters to a Function in C be declared Static?

Answer: Let’s first try a static variable to see how it behaves when function containing the variable is called more than once. For example,

/*
 * behaviour_static_var_fun.c -- program shows how static variable 
 * behaves
 */
#include <stdio.h>
void display_static_var(void);  /* declaration */
 
int main(void)
{
    int FLAG = 1;		
 
    while (FLAG) {
        display_static_var();
        printf("User, want to continue, enter 1 else 0,\n");
        scanf("%d", &FLAG);
    }
    printf("Thank you!\n");
    return 0;
}
 
void display_static_var(void)
{
    static int count = 1;/* count is declared static and initialized once */
 
    printf("static integer variable \"count\" is %d\n", count++);
}

Now, we turn to analyse the output of the above program below,

static integer variable "count" is 1
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 2
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 3
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 4
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 5
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 6
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 7
User, want to continue, enter 1 else 0,
0
Thank you!

We observed that static variable ‘count’ is initialized just once and thereon during each successive calls to the function ‘display_static_var()’ it retained its last modified value from previous call to function during the entire lifetime of program.

Let’s turn to see what happens when we declare formal parameters of type static. Consider an example,

advertisement
advertisement
/*
 * static_formal_arg1.c -- program shows if formal parameters can be 
 * declared static
 */
#include <stdio.h>
void sum_ints(static int, static int);    /*function declaration */
 
int main(void)
{
    static int x, y;
    int n;
 
    printf("user, type in two integers...for their sum; (q to quit)\n");
    while (n = scanf("%d %d", &x, &y) == 2) {
        sum_ints(x, y);
        printf("user, type in other two integers...for their sum;"
               " (q to quit)\n");
    }
    printf("Thank you!\n");
    return 0;
}
 
/* formal arguments of type static */
void sum_ints(static int u, static int v)
{
    printf("Sum of two static integers %d and %d is %d\n", u, v, u + v);
}

Check the type of error when above program was compiled, storage class error for static parameters in function declaration and function definition.

static_formal_arg.c:3:1: error: storage class specified for unnamed 
parameter
static_formal_arg.c:3:1: error: storage class specified for unnamed 
parameter
static_formal_arg.c:19:26: error: storage class specified for parameter ‘u’
static_formal_arg.c:19:40: error: storage class specified for parameter ‘v’

This is because of nature of static variable. Static variable in a function is initialized once and retains its last modified value from the previous call to function during the entire execution of the program. When function is called, instructions in the function execute and function exits. However, static variable with its last modified value still exists in the memory. When function is called again static variable isn’t reinitialized instead its last modified value is used. When we attempted to declare formal parameters of type static, and compiled the program, it resulted in storage class error for formal parameters in function definition and for unnamed parameters in function declaration.

Note: Join free Sanfoundry classes at Telegram or Youtube

Recall that ANSI C Standard recommends function declaration for each function in a program. Function prototype tells the compiler that function can appear somewhere in the program. Further, in a function declaration, specified arguments are not created and therefore they can be omitted from the prototype. For example,

    void new_msg(char *msg);

could be equivalently rewritten as

    void new_msg(char *);    /* name is omitted */

Therefore, formal parameters to a function are not declared static because of following two reasons:

advertisement

1. TOTAL MEMORY REQUIREMENT IS LESS if the parameters are declared automatic because these are created as the function is entered and are destroyed as the function exits.

2. RECURSION CAN ONLY BE IMPLEMENTED if variables are declared automatic because these are pushed onto the stack when the function is called. For example:

#include <stdio.h>
long int compute_prod_recur(int);
 
int main(void)
{
    int num;            /* num is an automatic variable */
    long int product;   /* product is also an automatic variable */
 
    printf("user, enter the natural number for which product successively"
           " from 1,2,3,...till the num is to be computed\n");
    scanf("%d", &num);
 
    product = compute_prod_recur(num);
    printf("Product through Recursion for the number is %ld\n", product);
 
    return 0;
}
 
long int compute_prod_recur(int number)  
{
    /* number is an automatic variable */
    if (number == 1)
        return 1;
    else
       return number * compute_prod_recur(--number);       
 
    /*  
     *  each time fun. compute_prod_recur() is called, number,
     *  an automatic variable, is created, initialized with value
     *  passed as an argument when the fun. was called & pushed onto
     *  the stack in LIFO (Last In First Out) manner. 
     */
}

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.