snprintf() nella libreria C

IL funzione snprintf() è definito nel file di intestazione e viene utilizzato per archiviare la stringa specificata fino a una lunghezza specificata nel formato specificato.

Caratteristiche del metodo snprintf():

  • La funzione snprintf() formatta e memorizza una serie di caratteri e valori nel buffer dell'array.
  • La funzione snprintf() accetta un argomento 'n', che indica il numero massimo di caratteri (inclusa la fine del carattere null) da scrivere nel buffer.
  • La funzione snprintf() viene utilizzata per reindirizzare l'output della funzione printf() su un buffer.
  • snprintf() restituisce anche il numero di caratteri che avrebbero dovuto essere scritti nel buffer (escluso il terminatore null), indipendentemente dal valore di 'n' passato.
  • Quindi, solo quando il valore restituito è non negativo e minore di ‘n’, la stringa è stata scritta completamente come previsto.

Sintassi: La sintassi del metodo snprintf() è:

int snprintf(char *str, size_t dimensione, const char *format, …);

parametri:

    *str: è un buffer. dimensione: è il numero massimo di byte (caratteri) che verranno scritti nel buffer. format : stringa C che contiene una stringa di formato che segue le stesse specifiche di format in printf … : the argomenti facoltativi (...). sono solo i formati di stringa come (%d, myint) come visto in printf.

Valore di ritorno:

  • Il numero di caratteri che sarebbero stati scritti nel buffer se 'n' fosse stato sufficientemente grande. Il carattere nullo finale non viene conteggiato.
  • Se si verifica un errore di codifica, viene restituito un numero negativo.

Di seguito è riportato un esempio per illustrare il funzionamento del metodo snprintf():

Esempio 1:

C




// C program to demonstrate snprintf()> #include> int> main()> {> > char> buffer[50];> > char> * s => 'geeksforgeeks'> ;> > // Counting the character and storing> > // in buffer using snprintf> > printf> (> 'Writing %s onto buffer'> > ' with capacity 6'> ,> > s);> > int> j = snprintf(buffer, 6,> '%s '> , s);> > // Print the string stored in buffer and> > // character count> > printf> (> ' String written on '> > 'buffer = %s'> , buffer);> > printf> (> ' Value returned by '> > 'snprintf() method = %d '> , j);> > return> 0;> }>

Produzione

Writing geeksforgeeks onto buffer with capacity 6 String written on buffer = geeks Value returned by snprintf() method = 14 

Esempio 2:

C




// C program to demonstrate snprintf()> #include> int> main()> {> > char> buffer[50];> > > // join two or more strings> > char> * str1 => 'quick'> ;> > char> * str2 => 'brown'> ;> > char> * str3 => 'lazy'> ;> > int> max_len => sizeof> buffer;> > int> j = snprintf(buffer, max_len,> > 'The %s %s fox jumped over the %s dog.'> ,> > str1, str2, str3);> > printf> (> ' The number of bytes printed to 'buffer' '> > '(excluding the null terminator) is %d '> ,> > j);> > if> (j>= max_len)> > fputs> (> 'Buffer length exceeded; string truncated'> ,> > stderr);> > puts> (> 'Joined string:'> );> > puts> (buffer);> > return> 0;> }>

Produzione

The number of bytes printed to 'buffer' (excluding the null terminator) is 45 Joined string: The quick brown fox jumped over the lazy dog.