Finners
Finners

Reputation: 37

Passing a Float into a String in C

How do I add a variable into a string? I want to transmit the string with the actual value of depth. So how do I add this into the message?

float depth = ((pressure) / (g)) * 100;
char message[] = "$DBS,,f,depth,M,,,F*hh<CR><LF>";  //Put actual depth here

Upvotes: 2

Views: 336

Answers (4)

Kami Kaze
Kami Kaze

Reputation: 2080

I would suggest to use snprintf() the usage is similar to printf() and the suggested sprintf(), but you have a parameter for max length of the string.

This way you can avoid buffer overflows.

depth = ((pressure)/(g))*100;
char message[100];
snprintf(message, sizeof message, "$DBS,,f,%f,M,,,F*hh\r\n", depth );

if the string would be larger then the array you can accomodate for this by checking the return value


int length = snprintf(message, sizeof message, "$DBS,,f,%f,M,,,F*hh\r\n", depth );

if (length > sizeof message)
{
  //do something to handle if neccessary
}

In the linked article you can find a list of specifiers and modifiers that can be used to modify the format of the float in your string, which can be very useful. Depending on how big your number is you might want to use %e (for decimal exponent notation or often times called engineering notation). Additionally you can influence how many digits are used and other things. It is really worth to check out.

Alternativly you can check out http://www.cplusplus.com/reference/cstdio/snprintf/ and http://www.cplusplus.com/reference/cstdio/printf/ which is easier to understand for some people, but not that in detail.

Upvotes: 7

chux
chux

Reputation: 153592

How do I add a variable into a string?

Use sprintf() or better snprintf().

I want to transmit the string with the actual value of depth. So how do I add this into the message?

A problem not yet answered is how to convey all the information of float depth to the recipient. This gets into what format should be used.

"%.*e", "%.*g" and "%a" are 3 good choices. "%f" is a weak choice as it provides no info on small values and excessive digits on large values. See Printf width specifier to maintain precision of floating-point value

Suggest:

//                -d.        dddddddd        e-ddd\0
#define STR_FLT_N (3 + (FLT_DECIMAL_DIG -1) + 6)
#define FMT "$DBS,,f,%.*e,M,,,F*hh<CR><LF>"
#define BUF_N (sizeof(format) + STR_FLT_N)

char message[BUF_N];
snprintf(message, sizeof message, FMT, FLT_DECIMAL_DIG-1, depth);

Upvotes: 1

Roberto Caboni
Roberto Caboni

Reputation: 7490

Use sprintf(). Similar to printf() but it prints within a string.

Of course the target buffer must be big enough to contain the whole string:

char message[100];
float depth = ((pressure)/(g))*100;

sprintf(message, "$DBS,,f,%f,M,,,F*hh\r\n", depth );

So %f is the conversion character that will be substituted by depth value in the final string. In order to define the number of decimals you can use the %0.Nf format, that will show your value with a N decimals precision.

Upvotes: 1

DarkAtom
DarkAtom

Reputation: 3161

Try the sprintf():

char message[100];
sprintf(message, "$DBS,,f,%f,M,,,F*hh<CR><LF>", depth);

That %f in the string is where the depth value will go.

Upvotes: 2

Related Questions