error: `itoa` was not declared in this scope

itoa is not ansi C standard and you should probably avoid it. Here are some roll-your-own implementations if you really want to use it anyway: If you need in memory string formatting, a better option is to use snprintf. Working from your example:

Categories C Tags

lvalue required as increment operand

x++ is the short form of x = x + 1. However, x here is an array and you cannot modify the address of an array. So is the case with your variable y too. Instead of trying to increment arrays, you can declare an integer i and increment that, then access the i‘th index … Read more

When is it ok to use a global variable in C?

Variables should always have a smaller scope possible. The argument behind that is that every time you increase the scope, you have more code that potentially modifies the variable, thus more complexity is induced in the solution. It is thus clear that avoiding using global variables is preferred if the design and implementation naturally allow … Read more

How to print a int64_t type in C

For int64_t type: for uint64_t type: you can also use PRIx64 to print in hexadecimal. has a full listing of available macros for all types including intptr_t (PRIxPTR). There are separate macros for scanf, like SCNd64. A typical definition of PRIu16 would be “hu”, so implicit string-constant concatenation happens at compile time. For your code to be fully portable, you must use PRId32 and so on for … Read more

How to use shared memory with Linux in C

There are two approaches: shmget and mmap. I’ll talk about mmap, since it’s more modern and flexible, but you can take a look at man shmget (or this tutorial) if you’d rather use the old-style tools. The mmap() function can be used to allocate memory buffers with highly customizable parameters to control access and permissions, and to back them with file-system storage if … Read more

What does the LEAL assembly instruction do?

LEA (load effective address) just computes the address of the operand, it does not actually dereference it. Most of the time, it’s just doing a calculation like a combined multiply-and-add for, say, array indexing. In this case, it’s doing a simple numeric subtraction: leal -4(%ebp), %eax just assigns to the %eax register the value of … Read more

How to use random() in C [duplicate]

So I am currently learning C, and I have some confusion about how the random() function works. I know I have to provide a seed, but I don’t know how to actually generate the random number. When I try this it gives me a compiler error: The end goal is to get a random number … Read more