Is this declaration correct?

int main()
{ signed x = 9;
cout<< x;
}

we have not specified the data type. Then why is it not showing any error ?

Hey Chahat,
yes it is a valid declaration 1h. βœ“
Signed means you are declaring and number which can be negative as well as positive.As your variable is positive it is showing you that value.

signed values can take positive as well as negative numbers whereas unsigned take only positive
consider these examples
signed a=-9; cout<<a; //output=-9
unsigned a=-9; cout<<a; //output= 4294967287

thanks
but my confusion is that signed is a data modifier and not a data type
so how do we know if we are referring here to a char or an int ?

hey chahat, you cannot use only signed or unsigned when you deal with characters. you have mention them as signed char or unsigned char. If you don’t do that it will be treated as int only and give its ASCII value. consider the below example.

signed char var=β€˜a’; cout<<var; //output=a
signed var=β€˜a’; cout<<var; //output= 97

conclusion: if it is only signed or unsigned than it is modifier for integer and if there is char after signed or unsigned than it for char.

1 Like

Hey chahat, if your get your answer please marks this doubt as resolved and rate me on my profile.

okay thanks :slight_smile: