Math Basic question C++

Would this print out a+C+5 ?

 
  cout << int('a')+int('C')+int('5');


1
2
3
4
5
int x=17, y=15;    
cout << x+y/4;         // =20
cout << x%3+4;      //  =6
cout << 2*x+3;       //  =37
cout << x-2*y+3;    //  = -10 


Can someone check if they are correct?
Last edited on
as a string? No.
thats all that is printed
I think its by int but all it said was:

3. cout << int('a)+int('C')+int('5');


Even with just 'a' + 'C', usual arithmetic conversions are applied and the two resultant integers are added.
https://msdn.microsoft.com/en-us/library/3t4w2bkb.aspx

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <type_traits>

int main()
{
    static_assert( std::is_same< decltype( 'a' ), char >::value, "type of 'a' is char" ) ;
    static_assert( std::is_same< decltype( 'a' + 'C' ), int >::value, "type of 'a' + 'C' is int" ) ;
    static_assert( std::is_same< decltype( +'a' ), int >::value, "type of +'a' is int" ) ;
}

http://coliru.stacked-crooked.com/a/3592124273b5e0bc
It only gives one line to answer so I'm thinking that it is supposed to be more simple than that.
what happens to int('5')?

It's printing out the integer value of the character '5'.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <type_traits>

int main()
{
    static_assert( std::is_same< decltype( 'a' ), char >::value, "type of 'a' is char" ) ;
    static_assert( std::is_same< decltype( 'a' + 'C' ), int >::value, "type of 'a' + 'C' is int" ) ;
    static_assert( std::is_same< decltype( +'a' ), int >::value, "type of +'a' is int" ) ;
    
    std::cout << 'a' << ' ' << +'a' << '\n'
              << 'C' << ' ' << +'C' << '\n'
              << "'a' + 'C' " << 'a' + 'C' << '\n' ;
}

clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out
g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
a 97
C 67
'a' + 'C' 164
a 97
C 67
'a' + 'C' 164

http://coliru.stacked-crooked.com/a/85018d905fcf8bfa
Topic archived. No new replies allowed.