Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

How to set a default value to function parameters in C

1 Answer

0 votes
// In C, you can't directly set default values for function parameters. 
// However, you can achieve similar behavior by using wrapper functions.

#include <stdio.h>

void print(char *name, int times) {
    for (int i = 0; i < times; i++) {
        printf("The: %s.\n", name);
    }
}

// Wrapper function with a default value
void print_with_default(char *str) {
    print(str, 1);  // Default value for 'times' is 1
}

int main() {
    print_with_default("C Programming");  
    
    print("abc", 3);  

    return 0;
}


/*
run:

The: C Programming.
The: abc.
The: abc.
The: abc.

*/

 



answered Jan 28, 2025 by avibootz

Related questions

2 answers 86 views
1 answer 90 views
1 answer 93 views
1 answer 75 views
1 answer 93 views
2 answers 90 views
2 answers 91 views
...