Easy Tutorial
❮ C Examples Display Alphabets C Macro Erange ❯

C Library Function - snprintf()

C Standard Library - <stdio.h>

Description

The C library function int snprintf(char *str, size_t size, const char *format, ...) formats the variable arguments (...) according to the format and copies the formatted string to str, with a maximum of size characters to be written. Any content beyond size will be truncated.

Declaration

Here is the declaration for the snprintf() function.

int snprintf ( char * str, size_t size, const char * format, ... );

Parameters

Return Value

Example

The following example demonstrates the use of the snprintf() function.

#include <stdio.h> 
  
int main() 
{ 
    char buffer[50]; 
    char* s = "tutorialprocom"; 
  
    // Read the string and store it in buffer
    int j = snprintf(buffer, 6, "%s\n", s); 
  
    // Output the buffer and character count
    printf("string:\n%s\ncharacter count = %d\n", buffer, j); 
  
    return 0; 
}

Output:

string:
runoo
character count = 10

C Standard Library - <stdio.h>

❮ C Examples Display Alphabets C Macro Erange ❯