Is the main function callable in C++ or C [duplicate] - c++

This question already has answers here:
Is it legal to recurse into main() in C++? [duplicate]
(5 answers)
Closed 7 years ago.
Can I call the main method in C/C++ from other functions. it seems to work but I do not know if it is a good software design in c++. please tell me the pros and cons ?
Thank you.

In C you can. In C++ you can't.
Quoting the C++ standard (§3.6.1.3):
The function main shall not be used within a program.
There's nothing in the C standard forbidding calling main.
Whether or not calling main is good design is quite opinion-based, but usually one would be better off using a loop instead.

According to C++ standard
5.2.2.9 "Recursive calls are permitted, except to the function named main"

You've already determined it is possible. However, it makes your entire program recursive. It also could make your code a little harder to understand.
So it's really hard for me to imagine any pros for this.

Related

If I treat my C code like C++ code, will it run like normal? [duplicate]

This question already has answers here:
can i use c++ compiler to compile c source code? [duplicate]
(3 answers)
What is the difference between g++ and gcc?
(10 answers)
Closed 2 years ago.
It's what I believe to be a very simple question.
Context: I'm following a tutorial that allows me to run C++ code in Visual Studio Code, but I'm trying to run C code, not C++ code. The program I'm trying to run is a simple Hello World program (shown below), but this question applies to all C code.
#include <stdio.h>
int main() {
printf("Hello World!")
}
C and C++ are different languages. And even though they share a similar syntax, the semantic meaning of certain constructs are different.
C++ incorporates a large part of C, but it also diverges. You cannot just assume that C code compiled as C++ will give the same result.
You can write code that is both valid C and valid C++ yet mean different things in the two languages.
While C++ can be seen for the most part a superset of C, there are some constructions that are invalid C++ and others that have different behavior.
Instead of dealing with that, tell your compiler to target C instead of C++. All the popular C++ compilers also support C (at least one version).

Is there any benefit to using `std::numeric_limits<uint64_t>::max()` instead of simply `UINT64_MAX`? (ie: `std::numeric_limits<>` vs cstdint) [duplicate]

This question already has answers here:
Are C++ Templates just Macros in disguise?
(25 answers)
Advantages of type traits vs static members?
(4 answers)
INT_[MIN|MAX] limit macros vs numeric_limits<T>
(5 answers)
C++: Why does numeric_limits work on types it does not know?
(1 answer)
Closed 2 years ago.
UINT64_MAX
is defined in <cstdint>: https://en.cppreference.com/w/cpp/header/cstdint.
This is what I've always used. However, I see C++ provides this alternative as well:
std::numeric_limits<uint64_t>::max()
(see: https://en.cppreference.com/w/cpp/types/numeric_limits/max), which is part of the many numeric limits options defined in the <limits> header: https://en.cppreference.com/w/cpp/types/numeric_limits.
The C++ version seems overly verbose to me and appears to have no benefits. I assume it's included simply to be thorough, since they were adding additional functionality via other numeric_limits<> created in C++, but wanted to cover everything, I imagine, while they were at it.
Is there any benefit of using the C++-only version (std::numeric_limits<uint64_t>::max()) over the C or C++ version (UINT64_MAX)?
I'd really prefer the UINT64_MAX version, but in general find a lot of pushback from hard-core C++ developers whenever they see me write something in C++ which also would be valid in C. :)
Note: I haven't had pushback on this one yet, but I'd like some insight first to see if there is something I'm missing which truly makes one version better than the other.
Update: here's my own answer:
I'm not too happy this question was closed literally in the very same second as I pressed the submit button to add my own answer, but here's my own answer to this question:
As #Bob__ pointed out in the comments under the question, std::numeric_limits<uint64_t>::max() is really preferred when writing a C++ template where the uint64_t part needs to be replaced with the typename T.
However, if not in a template, it seems to me UINT64_MAX would be preferred since it's short, straight-to-the-point, less verbose, and easier to read.

Why can't you end a while loop with a label? [duplicate]

This question already has answers here:
In C why do you need a statement after a goto label?
(3 answers)
Closed 4 years ago.
Instead of writing
while()
{
lbl:
}
You have to write
while()
{
lbl:;
}
Why? It doesn't make any sort of sense to me. This is the case even if there was actual code in the loop
If you code in C, because the C11 standard defines that. Check by reading n1570, §6.8.1. BTW, other standards of C have a similar rule.
If you code in C++, because the C++11 standard defines that. Check by reading n3337, §6.1. BTW, other standards of C++ have a similar rule.
Intuitively, a label concerns a statement, and the closing brace is not a statement (but a block terminator). And a semi-colon (when used alone) is ending an empty (or null) statement.
If you are not happy with that rule, feel free to use some different programming language, or to define your own one (and implement it, perhaps by compiling it to C). Maybe you could look into Scheme (read SICP then R5RS) and its call/cc primitive (a different, and more powerful, control primitive than plain goto).
You could also lobby the C++ (or C) standard committee to change the rules. In practice, that requires a world-wide reputation and many years of full time work (otherwise, they won't consider your proposal seriously). Before writing proposals to change the C++ standard, be sure to learn it very well and to provide a sample implementation of your ideas.
Many C++ implementations are free software (such as GCC or Clang). You are allowed to study them and to improve them (at least in your fork; however be sure to comply with their open-source license). However, there are large pieces of code (many millions lines of source code), so you'll need years to understand them.
Remember that a programming language is a specification (not a software) which defines not only the syntax but also the semantics.

Is google C++ style guide giving incorrect guidelines about use of inline functions? [duplicate]

This question already has answers here:
When to use the inline function and when not to use it?
(14 answers)
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 7 years ago.
I was reading this. It says that
Define functions inline only when they are small, say, 10 lines or
less.
But Bjarne Stroustrup in his book Programming Principles and Practices using C++ says that:
Section 9.4: Defining member functions
"The obvious rule of thumb is: Don't put member function bodies in
the class declaration unless you know that you need the performance
boost from inlining tiny functions. Large functions, say five lines of
code, don't benefit from inlining. We rarely inline a function that
consists of more than one or two expressions.
So is it appropriate & helpful to define a function having 10 lines or at least 10 lines as inline. Doesn't that make program executable file size larger or even compiler can ignore the request for inlining such big function?
Is Google C++ style gives incorrect guidelines about usage of inline functions in C++?

What is the meaning of unsafe in C++? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why is strtok() Considered Unsafe?
I have just noticed a warning (using Visual Studio) that strtok is unsafe, and strtok_s is not. Why is it unsafe and what is unsafe?
First part of my question is answered here but what is the meaning of unsafe and what are the problems and possible problems related to it?
strtok is not thread-safe. If two or more threads call strtok at the same time, the results will be undefined. I am reproducing here an answer by another user, Jonathan Leffler:
Be aware that strtok() destroys its input as it processes it. It is
also not thread-safe, and you have to be sure that no other function
that you call from your parser uses strtok(), and that no function
that calls your parser uses strtok(). The condition on functions
called is usually not too onerous; in library code, though, the second
condition (no calling function also using strtok()) is not
enforceable.
The response was given to this question: Dealing with input in C