Rahul_cs12
Rahul_cs12

Reputation: 984

Is it fine to use sizeof operator in snprintf

Is it fine to use sizeof operator with "snprintf" ? for example

   char cstring[20];
   snprintf(cstring,sizeof(cstring),"%s","somestring......");

Upvotes: 6

Views: 5835

Answers (5)

rabi shaw
rabi shaw

Reputation: 441

#include <stdio.h>                                                                                                                            
int main()                                                                                                                                    
{                                                                                                                                             
        char str[16];                                                                                                                         
        int len;                                                                                                                              

        len = snprintf(str, sizeof( str ), "%s %d %f", "hello world", 1000, 10.5);                                                            
        printf("%s\n", str);                                                                                                                  

        if (len >= 16)                                                                                                                        
        {                                                                                                                                     
                printf("length truncated (from %d)\n", len);                                                                                  
        }                                                                                                                                     
} 

output:
=======
 ./a.out
hello world 100
length truncated (from 26)

/* You can see from the output only 15 char + '\0' is displayed on the console ( stored in the str ) */


/* Now changing the size of the str from 16 to 46 */

#include <stdio.h>
int main()
{

        char str[46];
        int len;

        len = snprintf(str, sizeof( str ), "%s %d %f", "hello world", 1000, 10.5);
        printf("%s\n", str);

        if (len >= 46)
        {
                printf("length truncated (from %d)\n", len);
        }
}

output:
==========
./a.out
hello world 1000 10.500000

Upvotes: 1

user694733
user694733

Reputation: 16043

It is fine in example you posted.

However, it's not fine in any case where array decays in to pointer:

void func(char s []) {
    snprintf(s,sizeof(s),"%s","somestring......"); // Not fine, s is actually pointer
}
int main(void) {
    char cstring[20];
    func(cstring); // Array decays to pointer

Upvotes: 5

sharon
sharon

Reputation: 734

You can use the sizeof operator in the snprintf, but if the length of the string is bigger than the size which you have specified, then the remaining characters in the string will be lost.

Upvotes: 4

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

Yes you can use. But if the string is higher than the sizeof value then the string is truncated. or up to the given value is stored in that array.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249173

Yes, it's fine, the specific case you posted is good except that you don't check the return value, so you won't know if the string was truncated.

Upvotes: 9

Related Questions