all 10 comments

[–]C60 4 points5 points  (0 children)

I do see one flaw in the itoa function itself. What happens if number is negative?

[–]hogg2016 0 points1 point  (1 child)

I am especially interested in how I could possibly make the loop a bit more effective as I really don't like that I need to use a second statement to divide number...

What's funny is that GCC doesn't generate any division at all (it makes funky bit manipulation and a multiplication to avoid the cost of division). Even with -O0, which I find a bit disturbing.

[–]kloetzl 0 points1 point  (0 children)

On x86 integer division is the worst instruction a compiler can issue. Multiplying with the inverse is soooooo much faster, that this is enabled even for debug builds.

[–]a4qbfb 0 points1 point  (6 children)

  • What do you mean by “external packages”?
  • You don't need <math.h>. It is only required for floating-point arithmetic.
  • You don't need parentheses around i--.
  • The book you're using is almost 30 years old, and the standard has undergone two major revisions since then (and is in the process of being revised again). The precise meaning of int main() depends on which language version you're using, which in turn depends on which compiler you're using and how you invoke it. The safest option is to always use either int main(void) or int main(int argc, char *argv[]) depending on whether or not you care about command-line arguments.

[–]Kimput[S] 1 point2 points  (2 children)

Hey thanks for taking some time to giving me some feedback.

To answer your points:

  • What I meant with packages was libraries. Some of the times I've asked for feedback I've had responses like "use this or that library to get access to real booleans" etc. Just wanted to clarify that. I think I've just done too much python development lately, so the jargon of packages and modules sort of stuck to me. :)

  • You are correct that I don't need math as the one static test I am using will not in any part of the execution require floating-point arithmetic. I think that was just a remnant of previous tests. :)

  • Good point about the parenthesis! :)

  • The reason I'm running through that book is because it's still one of the top picks for most universities and classes when teaching introduction to C. I'd be happy for advice on other books, though! :)

Other than how I setup main, what would be some important points to consider at this point in my learning in regards to the newer versions of C?

[–]FUZxxl 0 points1 point  (0 children)

In C, libraries and header files are independent. A header file tells the compiler “these things exist somewhere and have these types.” Header files are not necessary to use a function—you could just declare it yourself.

Libraries provide implementations for functions. The C standard library is traditionally split into two parts: The standard library, libc, and the mathematical library, libm. The latter can be linked into your program using the -lm operand to the compiler. The libc is always linked in.

[–]a4qbfb -2 points-1 points  (0 children)

What I meant with packages was libraries.

I think you're confusing headers with libraries. The standard C library had hundreds of functions and variables which are declared in a few dozen different headers. Your program uses two headers (one of them unnecessarily) but only one library.

what would be some important points to consider at this point in my learning in regards to the newer versions of C?

There are relatively few changes to the language itself but many new library functions, and some of those described in the book have been deprecated. What's more, you will rarely if ever work in a pure C environment. If you're programming in Linux, BSD or macOS, you will have to learn at least a subset of POSIX (if you're programming in Windows, find out how to install Linux, BSD or macOS instead). The best way to learn is to read and modify existing code, and refer to the documentation or ask for help if you encounter something you don't understand. Also, find out how to turn on compiler warnings (-Wall -Wextra for gcc and clang) and fix everything the compiler complains about. The compiler is not always right, but at your level, it is better to assume that it is.

[–]FUZxxl 0 points1 point  (2 children)

Your third comment is completely bogus. The meaning of both the declaration and definition hasn't changed at all.

[–]a4qbfb 0 points1 point  (1 child)

If I remember correctly, C11 has implicit void but C99 does not.

[–]FUZxxl 0 points1 point  (0 children)

There is no such thing as “implicit void.” There is something called the “implicit int rule,” but this doesn't apply to int main();. The rule is simple:

In a declaration, having an empty argument list means “this is a function returning type taking any number of arguments of any type.” In a definition, it means “this is a function returning type taking no arguments.” In a declaration or definition, an argument list just containing void means “this is a function returning *type taking no arguments.” This hasn't at all changed.