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
Related
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
Why can't I assign my new_age(age) to another variable (in this case it will be new)?
I get the following errors:
main.cpp:21:9: error: expected type-specifier before ‘=’ token
21 | new = new_age(age)
| ^
This is my code:
#include <iostream>
using namespace std;
int new_age (int & age)
{
return (age + 100);
}
int main()
{
int age {};
new = new_age(age)
cout << "How old are you "
cin >> age
cout << "In 100 years you will be " << new:
return 0;
}
The word new is a keyword in C++. It has a special meaning and cannot be used as name of a variable.
For a full list of all keywords see here.
As an aside, even if new was allowed as an identifier, you have not declared it. To declare and initialize it directly with the value you need to add the variable's type in front of the identifier:
int not_new = new_age(age);
(Also note the semicolon at the end of the declaration which you forgot or mistyped in a lot of statements as well.)
It also seems that you'd want to call new_age(age) after taking input from the user.
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
C and C++ documentation for the use of #define suggests that this should not work as I am using the define to replace the text MyFunc() with _myfunc(), which is a function that does not exist:
#define MyFunc _myfunc
void MyFunc()
{
cout << "This Prints!" << endl;
}
int Main()
{
_myfunc();
return 0;
}
My guess is that the compiler is being clever. It knows that _myfunc() does not exists and therefore does not replace the text and simple uses MyFunc().
I can't find any documentation to support this theory. Does anyone know whether this is correct?
After the preprocessor has run, your program will look like:
void _myfunc()
{
cout << "This Prints!" << endl;
}
int Main()
{
_myfunc(); // #1
return 0;
}
Ignoring other errors here (lack of includes, ...), the compiler can find _myfunc declared and defined, so naturally it will be found by overload resolution at the call site #1.
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 2 years ago.
Improve this question
I'm playing around with the Sleep() function in C++ right now, and I am not understanding why this code operates the way it does.
I made a program to find the difference between two times, but it is not working as expected.
#include <iostream>
#include <chrono>
#include <Windows.h>
using namespace std;
int main() {
int timeA = (int)chrono::system_clock::now;
cout << timeA << "\n";
for (int i = 0; i < 5; i++) Sleep(1000);
int timeB = (int)chrono::system_clock::now;
cout << timeB << "\n";
int timeDifference = timeB - timeA;
cout << timeDifference;
cin.get();
return 0;
}
It seems as if the program is setting the variables at the same time, and then sleeping. Is this the case? If so, help me to understand why, please.
now is a function, not an attribute or variable. You failed to call it, and are casting the function pointer itself to int, which will always produce the same value for a given run (on a typical 64 bit system, the low 32 bits of the address where now is located).
Change both lines to use chrono::system_clock::now(), not chrono::system_clock::now.
Note that this is one of the reasons to avoid C-style casts, as well as a reason to compile with warnings turned up; it protects you from casting to wildly incorrect end results, without at least some sort of alert.
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 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.
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
This is a function to check a number for a power of two. Although the compiler does not print anything, it's also not showing any errors. Please let me know if the logic is correct or not.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
bool isPowerofTwo(long long n)
{
// Your code here
for (int i = 1; i <=n; i<<1)
{
if(i==n){
return true;
}
}
return false;
}
int main()
{
cout << isPowerofTwo(2);
return 0;
}
The expression i<<1 in the third statement (the "iteration expression") of your for loop doesn't actually do anything (that is, it doesn't modify the value of i). You need, instead, i<<=1 (or, in 'long form', i = i << 1).
Also, please read: Why should I not #include <bits/stdc++.h>?. In your code, the only standard header you need is #include <iostream>. Another good post to read is: Why is "using namespace std;" considered bad practice?.