-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
  • 355
  • 3
  • 7
  • 3
    Is `' '` a typo? Did you mean to use a multi character constant? – cigien Nov 29 '20 at 15:01
  • Related: [https://stackoverflow.com/questions/3960954/multicharacter-literal-in-c-and-c](https://stackoverflow.com/questions/3960954/multicharacter-literal-in-c-and-c) – drescherjm Nov 29 '20 at 15:03
  • Change `cout<< gvalue<<' ';` to `cout<< gvalue<<' ';` or `cout<< gvalue<<" ";` for the value that you wanted. – drescherjm Nov 29 '20 at 15:38

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
  • 50,461
  • 4
  • 56
  • 85
  • 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