+ 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?
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
+ 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...