Calling Undeclared Function in C and C++

In this article, we are going to see what happens when we call an undeclared function in C and C++?
Submitted by Radib Kar, on August 28, 2020

In C:

Below is the example in C,

#include <stdio.h>

// Argument list is not mentioned
void func();

int main()
{
    //it compiles in C, but this is not 
    //something we should do
    func(10);

    return 0;
}

void func(int x)
{
    printf("value: %d\n", x);
}

Output:

value: 10

The above one successfully compiles. But the below one doesn't compile in C.

#include <stdio.h>

// Argument list is not mentioned
void func();

int main()
{
    //it won't compile
    func('a');

    return 0;
}

void func(char x)
{
    printf("value: %c\n", x);
}

Output:

main.c:14:6: error: conflicting types for ‘func’
 void func(char x)
      ^~~~
main.c:15:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
 {
 ^
main.c:4:6: note: previous declaration of ‘func’ was here
 void func();
      ^~~~

In C++:

Below is the example in C++,

#include <bits/stdc++.h>
using namespace std;

// Argument list is not mentioned
void func();

int main()
{
    //it won’t compile
    func(10);

    return 0;
}

void func(int x)
{
    cout << "value: \n" << x << endl;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:10:12: error: too many arguments to function ‘void func()’
     func(10);
            ^
main.cpp:5:6: note: declared here
 void func();
      ^~~~





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.