-2
#include<iostream>

using namespace std;
    
int gvalue=10;


void extra(){
cout<< gvalue<<'  ';
}


int main()
{
    extra();
    {
        int gvalue=20;
        cout<<gvalue<<' ';
        cout<<gvalue<<' ';
    }
}

The output which I got was: 10822420 20

I cannot get what is the error? & what does the below section of code mean & work?

extra();
{
    int gvalue=20;
    cout<<gvalue<<' ';
    cout<<gvalue<<' ';
}

Thanks in advance..!! Ignore the bad English.

Kush Singla
  • 335
  • 3
  • 7

1 Answers1

4

' ' (note that there are two spaces between apostrophes) is a multi-character literal. Its value is implementation-defined; apparently on your implementation it's 8224 (which happens to be 32 * 256 + 32, in case you are wondering where this number came from; 32 is the ASCII code of the space ' ').

Igor Tandetnik
  • 48,636
  • 4
  • 54
  • 79
  • Thanks, I got it. – Kush Singla Nov 29 '20 at 15:11
  • One more thing I want to ask? What does it mean to have parenthesis after I called the function? Like how does it differ if I just paste the code without parenthesis – Kush Singla Nov 29 '20 at 15:13
  • Can you explain what line of code you are talking about. You can't remove the parenthesis here: `extra();` that would totally change the meaning and make the code do nothing. – drescherjm Nov 29 '20 at 15:16
  • 2
    The braces are unrelated to the function call. It's just a block aka a [compound statement](https://en.cppreference.com/w/cpp/language/statements#Compound_statements). It's redundant here - braces can be removed without changing the meaning of the program. – Igor Tandetnik Nov 29 '20 at 15:19