Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Integer literal as parameters of function declaration in cpp

I’m almost familiar with c and c++ programming. Today I was searching about function declaration when I suddenly came across a strange syntax in c++ language.
I wrote below code:

#include <iostream>
using namespace std;
int foo('3');
int bar(3);
int main(){
    
}

I’ve never seen defining the literals as function parameters! So I expected to get compile error when I compile this program:

$ g++ file.cpp

But it compiled without any problem!
So I’m curious to know what’s the meaning and usage of int foo('3'); and int bar(3); lines?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It means int foo = '3'; and int bar = 3; respectively.

But it’s not exactly equivalent, e.g. with classes = doesn’t permit explicit constructors.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading