Differenze tra le versioni di "Sprintf ()"

Da Gambas-it.org - Wikipedia.
Riga 15: Riga 15:
 
  <FONT Color=gray>' ''int sprintf (char *__restrict __s, const char *__restrict __format, ...)''
 
  <FONT Color=gray>' ''int sprintf (char *__restrict __s, const char *__restrict __format, ...)''
 
  ' ''Write formatted output to S.''</font>
 
  ' ''Write formatted output to S.''</font>
  Private Extern sprintf(__s As Pointer, __format As String, f As Float)
+
  Private Extern sprintf(__s As Pointer, __format As String, f As Float) As Integer
 
   
 
   
 
   
 
   

Versione delle 01:39, 19 ago 2016

La funzione

int sprintf (char *__restrict __s, const char *__restrict __format, ...)

scrive nel Puntatore "__s" gli argomenti passati formattati.

Questa funzione viene solitamente utilizzata in C per convertire agevolmente un valore di tipo numerico in una Stringa.


Volendo utilizzare tale funzione in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale essa è contenuta:

Private Extern sprintf(__s As Pointer, __format As String, valore As ...) As Integer


Mostriamo un semplice esempio, nel quale viene convertito in stringa un valore di tipo Float.

Library "libc:6"

' int sprintf (char *__restrict __s, const char *__restrict __format, ...)
' Write formatted output to S.
Private Extern sprintf(__s As Pointer, __format As String, f As Float) As Integer


Public Sub Main()
 
 Dim p As Pointer
 
' Allochiamo un'area di memoria adeguata a contenere la stringa finale:
  p = Alloc(SizeOf(gb.Float), 4)
 
  sprintf(p, "%lf", 123.456789)
   
  Print String@(p)
   
' Liberiamo la memoria precedentemente allocata:
  Free(p)
  
End



Riferimenti