why func() and func(void) are different [duplicate] - c

This question already has answers here:
in c: func(void) vs. func() [duplicate]
(2 answers)
Closed 9 years ago.
I have a function, can write in 2 ways.
void function(void) {
// operations....
}
and
void function() {
// operations......
}
Both functions are of same prototype. Why we have to mention void as argument at function definition?

No, both have different prototypes.
Compile the below programs you will understand.
void function1(void)
{
printf("In function1\n");
}
void function2()
{
printf("In function2\n");
}
int main()
{
function1();
function2(100); //Won't produce any error
return 0;
}
Program 2:
#include <stdio.h>
void function1(void)
{
printf("In function1\n");
}
void function2()
{
printf("In function2\n");
}
int main()
{
function1(100); //produces an error
function2();
return 0;
}

The correct way to say "no parameters" in C and C++ is
void function(void);
But when we write
void function();
It means a little different way in C and C++! It means "could take any number of parameters of unknown types", and in C++ it means the same as function(void).

Related

Difference between int main(void) and int main() [duplicate]

This question already has answers here:
Difference between int main() and int main(void)?
(10 answers)
Closed 9 months ago.
#include <stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
I was reading article on geeksforgeek the difference between int main(void) and int main() but I am confused. How main function taking arguement?
int main(void) { }
Here, you are telling your compiler explicitly that your main() function is not going to take any arguments. This prototype of main() function is standard.
int my_func(void)
{
return 100;
}
// the above function can be called as:
// 1. my_func(); --> good
// 2. my_func(21); --> error
// 3. my_func("STACKOVERFLOW"); --> error
int main() { }
Here, you are telling your compiler implicitly that your main() function is not going to take any arguments. But, there's a plot twist, in C leaving the function without any arguments (type function() { }) means you can put any value/variable in it while calling that function. This prototype should be avoided.
int my_func()
{
return 100;
}
// the above function can be called as:
// 1. my_func(); --> no error
// 2. my_func(21); --> no error
// 3. my_func("STACKOVERFLOW"); --> no error

Why can't I pass void and a parameter to the function in C?

#include <stdio.h>
void fun();
int main(void) {
fun(fun());
return 0;
}
void fun()
{
printf("function is called");
}
The return type of function fun is void. So the below statement should be valid right!
fun(fun())
But the compiler raises compilation error as error: argument type 'void' is incomplete. Cannot understand what the error means?
I think you want something like this:
#include <stdio.h>
int fun(int arg); // function takes one argument
int main(void) {
fun(fun(1)); // use one argument when calling fun
return 0;
}
int fun(int arg)
{
printf("function is called\n");
return 0;
}

Why the function calls not getting executed? [duplicate]

This question already has answers here:
In C, calling a function from main [closed]
(6 answers)
Closed 4 years ago.
#include<stdio.h>
void check_alphabets(char array_string[],int n){
char alphabet_array[n];
int i,j,val;
int alphabet_array_counter=0;
for(i=0;i<n;i++){
val=array_string[i];
if((val>=65 && val<=90) || (val>=97 && val<=122)){
alphabet_array[alphabet_array_counter]=array_string[i];
alphabet_array_counter++;
}
}
for(j=0;j<n;j++){
printf("%c",alphabet_array[j]);
}
}
void check_number(char array_string[],int n){
char number_array[n];
int i,j,val;
int number_array_counter=0;
for(i=0;i<n;i++){
val=array_string[i];
if(val>=30 && val<=39){
number_array[number_array_counter]=array_string[i];
number_array_counter++;
}
}
for(j=0;j<n;j++){
printf("%c",number_array[j]);
}
}
void check_character(char array_string[],int n){
char character_array[n];
int i,j,val;
int character_array_counter=0;
for(i=0;i<n;i++){
val=array_string[i];
if((val>=32 && val<=47) || (val>=58 && val<=64) || (val>=91 && val<=96) || (val>=123 && val<=127)){
character_array[character_array_counter]=array_string[i];
character_array_counter++;
}
}
for(j=0;j<n;j++){
printf("%c",character_array[j]);
}
}
void main(){
int size=100;
char array_string[size];
printf("Enter string? ");
scanf("%s",array_string);
int i=0,count=0;
while(array_string[i]!='\0'){
count++;
i++;
}
printf("size is %d",count);
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
}
I have the above piece of code . I don't know what is wrong with it .No matter what I modify The functions are not getting executed.
I am compiling using gcc in ubuntu 18.
But whenever I try to compile it throws a warning which is
In function main
warning:parameter names(without types) in function declaration void check_alphabets(array_string,count)
this warning shows with all the calls from main.
I googled but couldn't find any solution.
These are not function calls:
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
These are declarations. The return type in front of the function name tells us this is a declaration. When you call a function, you don't need to say what the return type is:
check_alphabets(array_string,count);
check_number(array_string,count);
check_character(array_string,count);
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
these lines are not function calls. YOu mean
check_alphabets(array_string,count);
check_number(array_string,count);
check_character(array_string,count);
The warning you are getting is because the compiler thinks you are trying to declare a function, but your syntax is an invalid function declaration (argument names without types)
First, you have to learn how to use Markdown to format your code properly. Secondly, you're not calling your functions in main, you are only declaring them.
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
must become
check_alphabets(array_string,count);
check_number(array_string,count);
check_character(array_string,count);

Execute any function before main [duplicate]

This question already has answers here:
Executing code before main()
(5 answers)
Closed 9 years ago.
I want to execute user define function before main().
Is it possible to execute a function before main() in c?
sum(int a, int b) { return (a+b); }
g_sum = sum(1, 5);
main(){
sum(5, 6);
printf("%d", g_sum);
}
Is it possible to execute a function before main()
Yes it is possible if you are using gcc and g++ compilers then it can be done by using __attribute__((constructor))
Example:
#include <stdio.h>
void beforeMain (void) __attribute__((constructor));
void beforeMain (void)
{
printf ("\nThis is before main\n");
}
int main ()
{
printf ("\nThis is my main \n");
return 0;
}

function declaration inside main and also outside main in C

The first program compiled properly.second gave error saying too few argument for foo...
is global declaration ignored by the compiler in both programs?
first program:
#include<stdio.h>
void foo();
int main()
{
void foo(int);
foo(1);
return 0;
}
void foo(int i)
{
printf("2 ");
}
void f()
{
foo(1);
}
second program:
void foo();
int main()
{
void foo(int);
foo();
return 0;
}
void foo()
{
printf("2 ");
}
void f()
{
foo();
}
The inner declaration hides declarations at the global scope. The second program fails because the declaration void foo(int); hides the global declaration void foo();; so when you say foo within main you are referring to the one taking an int as argument.
I see that you investigate the specific behavior of inner declaration but why not:
#include<stdio.h>
void foo(int);
int main()
{
foo(1);
return 0;
}
void foo(int i)
{
printf("2 ");
}
void f()
{
foo(1);
}
or even:
#include<stdio.h>
void foo(int i)
{
printf("2 ");
}
void f()
{
foo(1);
}
int main()
{
foo(1);
return 0;
}

Resources