+ 1

what is (int) doing in following code?

string s='0123'; int n=(int)s[0]-'0'; cout<<n; what is (int) doing in code above, although code works without the same?

11th May 2020, 5:56 AM
Utkarsh Dhiman
Utkarsh Dhiman - avatar
2 Respuestas
+ 2
Its used for type casting I.e. conversion of a variable from one type to another Your code works with or without explicit type casting since the compiler implicitly converts(promotes) char to int for use in arithmetic expressions But note this is not always the case and sometimes you have to convert a variable from one type to another or else the compiler will complain
11th May 2020, 6:35 AM
Anthony Maina
Anthony Maina - avatar
+ 1
here 'int n' refers to the initiallisationof the variable n and the tnt infront of s[o] is to convertion so that it possess the logic bearing .literally speaking" type casting" in c language i hope u can understand...
3rd Jun 2020, 5:22 PM
Lekhana Pinninti
Lekhana Pinninti - avatar