Why the answer is -47 ?
I have written a simple code beacuse i have two strings, one is hard coded and the sum of whose adjacent membrs is to be stored in the other string.but the answer of first two indexes of string 1 is different as following:
#include#include #include int main() { char string1[]="011100011"; char string2[9]; cout< This code gives a symile face instead of 1.
while the following code gives 1, why?
#include#include #include int main() { char string1[]="011100011"; char string2[9]; cout< The problem can be solved by 2nd code but what is the true logic behind, can u explain........???
The third code gives -47....
#include#include #include int main() { char string1[]="011100011"; char string2[9]; cout< ???
Replies
-
KenJackson
This is one of my pet peeves with cout and one of the reasons I prefer to stick with the excellent printf family of functions.Vicky OneThis code gives a symile face instead of 1.
The examples don't take into account that characters '0' and '1' have binary (ASCII) values of 48 and 49. So after the math ('0' - '0' + '1' - '0'), variable string2[0] contains a binary 1, not character '1'.
The problem with cout is that it blindly obeys the type. Even though you wanted it to print an integer value, it was given a character type so it tried to print a character type. A binary one is often displayed as a smiley face--that's not ASCII but #-Link-Snipped-#.
One way to handle this is to cast the character as an int:
cout << (int)string2[0];
You are reading an archived discussion.
Related Posts
which is the best to do the smart metering
Quote:
The new system uses its on-board sensor to detect the risk of collision with an obstacle or pedestrian, initially warning the driver with visual and audible alerts.
Pre-collision brake...
Can any one correct the following code and explain...
The function decode is returning a string and i want that local string to be stored in another string in main...
Hello Friends
I am badly stuck with following Problem
I have a Project set up on my localhost in following hierarchy
www/htdocs/abl
Now the Problem is I want to rewrite...
Chinese researchers have finally made a breakthrough in the wireless internet segment with Li-Fi technology, where wireless signals get transmitted via lightbulbs. These brilliantly awesome Chinese folks proposed that four...