how come an undeclared variable is outputting a value [duplicate] - c++

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}

You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.

Related

C++: using namespace directive for non-existing namespace [duplicate]

This question already has answers here:
"using namespace std;" without any #include? [duplicate]
(3 answers)
Closed 2 years ago.
I am tryting to compile following code with g++ (version 7.5.0)
using namespace nspace;
int main()
{
return 0;
}
It gives error as follow
$ g++ above_code.cpp
namespaces_mystery1.cpp:1:17: error: ‘nspace’ is not a namespace-name
using namespace nspace;
^~~~~~
namespaces_mystery1.cpp:1:23: error: expected namespace-name before ‘;’ token
using namespace nspace;
^
Above behaviour is what I have expected.
But when I try to compile following code, it compiles fine without error like above.
using namespace std;
int main()
{
return 0;
}
Why this different behaviour for namespace named std compared to namespace named nspace
The namespace nspace doesn't exist at the point using namespace nspace; is encountered, whereas the std namespace does. The latter could be true due to implicit or explicit inclusion of facets of the C++ standard library, or the compiler itself might even hardcode it.
If you had written
namespace nspace{}
before the using statement, then compilation would succeed.

I am new to C++ Programming , What is the difference between these codes , and Which one I should use? [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I got both the codes from Books from an Online PDF
First -
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
Second -
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" ;
return 0;
}
No difference, using namespace std; simply means everything that is otherwise available via std namespace no loner needs the std:: prefix. In a cpp file its a personal preference. In an h file - don't use using namespace std;, this is because std namespace is huge, and you may be not the only one including that h. For a beginner, or 'academic' code in general it doesn't really matter, but believe me, when you are on the receiving end of someone pulling the entire std namespace in on you in a big project, you aren't gonna like it.

Is using #define considered "bad practice"? [duplicate]

This question already has answers here:
static const vs #define
(11 answers)
Closed 4 years ago.
Goal
In the end, I want to know if using #define is bad for your code, and why.
Code
#include <iostream>
using namespace std;
#define favouriteNumber 20;
int main()
{
int number = favouriteNumber;
cout << number;
}
According to Stroustrup it's particularly "bad" for defining constants because the compiler can't check types then.
On the other hand, if one line of macro saves you 20 lines of explicit code, some people will certainly agree that it's useful even if it's inherently unsafe. That's because writing more code usually implies higher probability for mistakes.

Including windows.h causes an error [duplicate]

This question already has answers here:
How do I deal with the max macro in windows.h colliding with max in std?
(6 answers)
Closed 7 years ago.
This is rather bizarre, I have a class I've been building and currently I have this at the top of my file:
#pragma once
#include <cstdint>
#include <cstring>
#include <string>
#include <limits>
Now I need to add windows.h to the mix but as soon as I do that, I get "Error: expected an identifier" on this line:
inline uint32_t Hash2(std::string &Key) {
return (MurMur3::x86_32(Key.c_str(), Key.size(), 2) % (std::numeric_limits<uint32_t>::max() - 1)) + 1;
}
the red line appears under the ::max if that matters. As for the function itself, its supposed to use murmur3 to get me a hash that isn't 0.
If I remove
std::numeric_limits<uint32_t>::max()
and replace it with the constant 4294967295
then it works fine again.
I don't understand why this is happening. Does anybody have a clue?
Windows.h has a very bad habbit of defining macros in it. In particular, it defines min and max. You need to undef those.

Fatal Error in c++ while using local variables [duplicate]

This question already has answers here:
Where to get iostream.h
(3 answers)
Closed 7 years ago.
As am learning cpp tutorial
#include <iostream.h>
using namespace std;
int main()
{
//variable declaration
int a,b;
int c;
//actual initialization
a=10;b=20;
c=a+b;
cout<<c;
return 0;
}
my error
fatal error: iostream.h
Just change <iostream.h> to <iostream>
Reason is that .h header extensions were used for C includes but aren't used for C++ anymore.
In fact, you can actually use C libraries with .h it's just there isn't one for iostream since its C++ exclusive, hence the fatal error.