Printf variable number of decimals in float

You can also use “.*” with floating points, see also http://www.cplusplus.com/reference/cstdio/printf/ (refers to C++, but the format specifiers are similar) .number: For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6). … .*: The precision is not specified … Read more

How to combine two 32-bit integers into one 64-bit integer?

It might be advantageous to use unsigned integers with explicit sizes in this case: #include <stdio.h> #include <inttypes.h> int main(void) { uint32_t leastSignificantWord = 0; uint32_t mostSignificantWord = 1; uint64_t i = (uint64_t) mostSignificantWord << 32 | leastSignificantWord; printf(“%” PRIu64 “\n”, i); return 0; } Output 4294967296 Break down of (uint64_t) mostSignificantWord << 32 | … Read more

Converting a void* to a std::string

You just need to dynamically allocate it (because it probably needs to outlive the scope you’re using it in), then cast it back and forth: // Cast a dynamically allocated string to ‘void*’. void *vp = static_cast<void*>(new std::string(“it’s easy to break stuff like this!”)); // Then, in the function that’s using the UserEvent: // Cast … Read more

Awk print string with variables

printf allows interpolation of variables. With this as the test file: $ cat file a,b,APPLE,d,MANGO,f We can use printf to achieve the output you want as follows: $ awk -F, ‘{printf “/p/%s_abc/xyz/%s_abc_def/\n”,$3,$5;}’ file /p/APPLE_abc/xyz/MANGO_abc_def/ In printf, the string %s means insert-a-variable-here-as-a-string. We have two occurrences of %s, one for $3 and one for $5.

Using floats with sprintf() in embedded C

Since you’re on an embedded platform, it’s quite possible that you don’t have the full range of capabilities from the printf()-style functions. Assuming you have floats at all (still not necessarily a given for embedded stuff), you can emulate it with something like: char str[100]; float adc_read = 678.0123; char *tmpSign = (adc_read < 0) … Read more

C++ Streams vs. C-style IO?

This is an heated topic. Some people prefer to use the C++ IO since they are type-safe (you can’t have divergence between the type of the object and the type specified in the format string), and flow more naturally with the rest of the C++ way of coding. However, there is also arguments for C … Read more

Cross platform/compiler consistent sprintf of floating point numbers

You can switch to LuaJIT. It formats numbers consistently between platforms. From the extensions page: tostring() etc. canonicalize NaN and ±Inf All number-to-string conversions consistently convert non-finite numbers to the same strings on all platforms. NaN results in “nan”, positive infinity results in “inf” and negative infinity results in “-inf”. tonumber() etc. use builtin string … Read more

Centering strings with printf()

printf by itself can’t do the trick, but you could play with the “indirect” width, which specifies the width by reading it from an argument. Lets’ try this (ok, not perfect) void f(char *s) { printf(“—%*s%*s—\n”,10+strlen(s)/2,s,10-strlen(s)/2,””); } int main(int argc, char **argv) { f(“uno”); f(“quattro”); return 0; }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)