Home > Enterprise >  I need some advice on how to format my ints and doubles into a string for sprintf in C. I pasted my
I need some advice on how to format my ints and doubles into a string for sprintf in C. I pasted my

Time:03-27

I need help formatting my ints and doubles into a string type for sprintf in the organizeStyle function. You know how if you are using an int, but need it to be a double, you would normally just say something like:

int x = 5;
double y;
y = (double) x;

Well I need something similar to convert ints and doubles into strings in my code below for the sprintf command in the organizeStyle function.

typedef struct{
    int variableOne;
    int variableTwo;
    int variableThree;
    int variableFour;
    double variableFive;
    double variableSix;
} varSet;

void organizeStyle(char *stringout, varSet item){
    sprintf(stringout, "%s %s %s %s %s %s", item.variableOne,item.variableTwo,item.variableThree,item.variableFour,item.variableFive,item.variableSix);
}

int main{
    char putHere[200];
    varSet piece;
    piece.variableOne = 10;
    piece.variableTwo = 15;
    piece.variableThree = 9;
    piece.variableFour = 21;
    piece.variableFive = 3.1;
    piece.variableSix = 7.6;
    organizeStyle(putHere,piece);
    printf("%s",putHere);
    return 0;
}

Trying the following did not work:

sprintf(stringout, "%s %s %s %s %s %s",(char) item.variableOne,(char) item.variableTwo,(char) item.variableThree,(char) item.variableFour,(char) item.variableFive,(char) item.variableSix);

I also tried the following, and it did not work either:

sprintf(stringout, "%s %s %s %s %s %s",(char[20]) item.variableOne,(char[20]) item.variableTwo,(char[20]) item.variableThree,(char[20]) item.variableFour,(char[20]) item.variableFive,(char[20]) item.variableSix);

I sincerely apologize if this is too simple of a question, I just can't seem to find the appropriate documentation for it online. Also, yes I know I am way over-declaring the size of these strings, I plan to shrink them later when it is all settled & done. Thank you so much in advance!

CodePudding user response:

sprintf(stringout, "%d %d %d %d %f %f", item.variableOne,item.variableTwo,item.variableThree,item.variableFour,item.variableFive,item.variableSix);

are the correct format specifier. %d for int and %f for double, instead of %s.

https://godbolt.org/z/fvTGPvcdq

You should use more compiler flags ( -Wall at the minimum for gcc and clang for example) to get warnings about format specifier.

https://linux.die.net/man/3/printf is the documentation for the format specifier, I google for man printf when I need to look something up.

CodePudding user response:

Use snprintf() instead of sprintf() to avoid unintentional buffer-overflows.

#include <stdio.h>

typedef struct {
    int vOne;
    int vTwo;
    int vThree;
    int vFour;
    double vFive;
    double vSix;
} varSet;

void organizeStyle (char *stringout, int str_sz, varSet item) {
    snprintf (stringout, str_sz, "%d %d %d %d %lf %lf",
              item.vOne, item.vTwo, item.vThree, item.vFour, item.vFive, item.vSix);
}

int main () {
    char putHere[200];
    varSet piece;
    
    piece.vOne = 10;
    piece.vTwo = 15;
    piece.vThree = 9;
    piece.vFour = 21;
    piece.vFive = 3.1;
    piece.vSix = 7.6;
    organizeStyle (putHere, sizeof (putHere), piece);
    printf ("%s\n", putHere);
    
    return 0;
}

Checkout the list of : Format Specifiers

It's better to send base address of the structure:

organizeStyle (putHere, sizeof (putHere), &piece);

...
// and function would be
void organizeStyle (char *stringout, int str_sz, varSet* item) {
    snprintf (stringout, str_sz, "%d %d %d %d %lf %lf",
              item->vOne, item->vTwo, item->vThree, item->vFour, item->vFive, item->vSix);
}
  •  Tags:  
  • c
  • Related