×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

Example 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

Example

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();
      ^~~~

Example 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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.