C++ shows '123', 3224115 in output (using cout) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
this is my c++ homework, please help me. (I already googled it and found nothing)
int main()
{
std::cout<<'123';
getchar();
return 0;
}
in output you'll get 3224115.
why?

Write instead
std::cout << "123";
^ ^
'123' is a character literal that has implementation defined value and type int.
It can be imagine for example the following way
#include <iostream>
int main()
{
int x = 0x313233;
std::cout << x << std::endl;
return 0;
}
where 0x31, 0x32, 0x33 are ASCII codes for characters '1', '2', '3' correspondingly.
For this program the output can be
3224115

Quoting the C standard (pg 61)
The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined.

Related

cout macro value doesn't work as expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
#include<iostream>
using namespace std;
#define C 1<<(8*1)
int main(){
if(C==256){
int a=C;
cout<<a;
}
}
My expectation is 256 but it print 18. What's wrong with it? Thanks!
I assume your question is about std::cout << C;, not about std::cout << a;.
Macros are simple copy-and-paste text replacement. When preprocessor encounters macro name, it replaces it with the definition as text without any analysis. So, what happens is that
std::cout << C;
is replaced with
std::cout << 1<<(8*1);
which should indeed print 18.
That's one very good reason to not use macros. Instead, use a constant (or constexpr) variable:
constexpr int C = 1 << (8 * 1);
This is type safe and will never surprise you when used in any context.
If you really have to use macro for some reason, make sure to wrap it in extra parantheses:
#define C (1 << (8 * 1)) // but seriously, don't use macros

Why is this code not printing the ASCII value of the given char? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
#include<iostream>
using namespace std;
int main() {
int x;
char ch=x;
cin>>ch;
cout<<x;
}
When I give input as 'A' , it shows '32766'.
For input 'B', it shows the same '32766'.
Why is this code not printing the ASCII value of the given char ?
It looks like you are quite new to programming. It would work if you'd output something related to the input. Unfortunately, in your code, x is completely unrelated to the input (and unitialized):
First improvement will just print the character:
char ch;
cin>>ch;
cout<<ch;
Second improvement will display the character as an int:
char ch;
cin>>ch;
cout<<(int)ch;
Or, third alternative, probably what you tried but in the wrong order:
char ch;
cin>>ch;
int x = ch; // there's already a value in ch now
cout<<x;

How to use comparison operator correctly in this case? (string array[0][0] == "string") [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to compare a character in a string that is in an array with another string. This is a functioning but simple version of my problem:
#include <iostream>
using namespace std;
int main() {
string a_ray[1] = {"asd"};
if (a_ray[0][0] == "a") {
bool a;
}
return 0;
}
Error message: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
What causes this? And how can I do what I want to do in the correct way?
Thank you in advance!
Since you are comparing against a character, your code should be
if (a_ray[0][0] == 'a')
You are trying to compare a character with a character array, hence the error message.

why no overflow when char is more than one byte? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if char is only 1 byte (8bit, 255 max size), how 65534th char can be saved in char ?
#include <iostream>
int main() {
unsigned wchar_t a = 65534;
char b = (char)a;
std::cout << b << std::endl;
return 0;
}
also in java you can write byte b = 5000;
do anyone knows why ?
For the C++ code, the wchar_t value gets truncated to fit in the char variable. Normally there would be a warning from the compiler about this loss of data, but by using the C-style cast you've told the compiler "don't worry, I know what I'm doing" and taken responsibility for the consequences of your (questionable) actions.

use of strchr() giving error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to find the occurrence of characters of one string(s1) in other string(s2).
This is part of my code.
for(;i<strlen(s1);i++)
{
int x=strchr(s2,s1[i]);
if(x>0)
count++;
}
But on compiling I get an error pointing to strchr() and says
error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
Anyone explain what is problem in using strchr() function.
Assignment is wrong strchr doesn't returns int but returns address of first char in string found:
int x=strchr(s2,s1[i]);
should be:
char* x = strchr(s2, s1[i]);
// ^ returns char*
Read manual
char *strchr(const char *s, int c);
RETURN VALUE
The strchr() and strrchr() functions
return a pointer to the matched character or NULL if the character
is not found. The terminating null byte is considered part of the
string, so that if c is specified as '\0', these functions return a
pointer to the terminator.
And so:
if(x>0)
should be:
if(x != NULL)
or just if(x)