char () and int () are functions in c++? [duplicate] - c++

This question already has answers here:
What is the difference between static_cast<> and C style casting?
(7 answers)
Closed 4 years ago.
after 30 min of googling I decided to ask here my doubt
It maybe invalid question if I am not understanding below line of code correctly
result += char(int(text[i]+s-65)%26 +65);
In above code char() is a function ? If yes so I am unable to find any information about it and if no so what is this ?
And same doubt for inner int() .
Above code is copied from a C++ program.

They're not functions. They're just alternate syntax for type-casting. char(x) is more-or-less equivalent to static_cast<char>(x).
In general, in C++, one should prefer the C++-specific constructs for casting objects (static_cast, dynamic_cast, const_cast, and reinterpret_cast), as those help ensure you don't do anything dumb when casting objects. So in your code example, I'd recommend rewriting it as
result += static_cast<char>(static_cast<int>(text[i]+s-65)%26 +65);
But functionally, it's all identical.

Related

C++ correct references [duplicate]

This question already has answers here:
Whats the difference between these two C++ syntax for passing by reference? [duplicate]
(1 answer)
Placement of the asterisk in pointer declarations
(14 answers)
Closed 4 years ago.
Can someone explain what the difference is, if any, between these two lines:
int& i;
int &i;
I know these are both references and both seem to work fine. Is there a reason to use one over the other? Is there any rule saying what is the right way?
Thanks in advance.
There is absolutely no difference in meaning between these two, it is a purely stylistic matter. Just pick one and try to be consistent within a project.
I believe the examples in the language standard put the & symbol on the left - that's as good a reason as any to prefer one way over the other, I suppose.
That said, as you've written it, neither line is valid code, because you can't have an uninitialised reference. You would need something like:
int a = 10;
int& b = a;

What does mean "int(i)=1;"? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).

beginner MACRO vs. const conceptual idea [duplicate]

This question already has answers here:
Inline functions vs Preprocessor macros
(14 answers)
C/C++ macros instead of const [duplicate]
(4 answers)
Closed 7 years ago.
What is the most significant difference of these two max operations? Which one do
you prefer to use in your system, and why?
#define max(a,b) (a)<(b)?(b):(a)
int max (const int a, const int b) { return (a) < (b) ? (b) : (a); }
I am trying to see if I am on the right track for the above question. My first thought is obviously that the #define indicates a preprocessor directive, or MACRO, named "max". Therefore, anywhere "max" is encountered in the program, it will be replaced with the defined value of this macro. Macros also dont require any memory allocation, so we can expect faster execution times.
The const keyword, on the other hand, does require memory allocation, and is not able to be changed by the executing program. The overall consensus through my notes and some online sources seems to be that macros are more efficient/faster since they do not require the memory allocation. Therefore, it would seem I would prefer to use macros for their speed advantages.
Basically my question is, am I nailing the main differences between these two? Or am I missing something major?

Is multiple assignment expression (a=123, b=456, c=789) well-defined? [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 7 years ago.
Edit: Apologies for posting a duplicate and such a basic question! I thought it was a trickier one than it actually was, and failed to discover the cited article in my original search.
For the language standard experts out there, is the value of an expression like:
(a=1, b=2, c=3)
... defined? From my tests, it appears that all of our compilers evaluate this expression as 3 (GCC, MSVC, Clang).
However, I'm not sure if we should lean on this behavior. I have no intention of writing code like this, but encountered some obscure code which was leaning on this behavior with multiple assignment in a conditional, and was wondering if it was well-defined.
If a, b, and c are all already defined, you are using a comma operator, which would return the value of c = 3, which is 3.
So to answer your question, yes, it is well defined.

What does (void)variableName mean [duplicate]

This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 8 years ago.
I saw following code few times
void func(SomeTypeEGInt varname) {
(void)varname;
}
I wish to know what it means and why people implement such functions.
It tell the compiler that those variables are unused. It is used to prevent the warnings which you will get.
The (void)varname; pattern is typically used to silence compiler warning about unused arguments. So this example is actually an empty function which does nothing.