Repository navigation
Cva Arg
Paul Squires edited this page Oct 14, 2026
·
2 revisions
Home › Procedures › Cva_Arg
Macro to obtain the next argument from a variadic argument list object.
variable = Cva_Arg ( argument_list, datatype )argument_listdatatypeargument_list
The Cva_Arg macro allows the use of a variable number of arguments within a function:
- Not available in the -lang qb dialect unless referenced with the alias
__cva_arg.
- New to FreeBASIC
'' Example of a simple custom printf
Sub myprintf cdecl(ByRef formatstring As String, ...)
Dim As Cva_List args
'' Initialize the cva_list object to first var-arg
Cva_Start( args, formatstring )
'' For each char in format string...
Dim As UByte Ptr p = StrPtr(formatstring)
Dim As Integer todo = Len(formatstring)
While (todo > 0)
Dim As Integer char = *p
p += 1
todo -= 1
'' Is it a format char?
If (char = Asc("%")) Then
If (todo = 0) Then
'' % at the end
Print "%";
Exit While
End If
'' The next char should tell the type
char = *p
p += 1
todo -= 1
'' Print var-arg, depending on the type
Select Case char
'' integer?
Case Asc("i")
Print Str(Cva_Arg(args, Integer));
'' long integer? (64-bit)
Case Asc("l")
Print Str(Cva_Arg(args, LongInt));
'' single or double?
'' Note: because the C ABI, all singles passed on
'' var-args are converted to doubles.
Case Asc( "f" ), Asc( "d" )
Print Str(Cva_Arg(args, Double));
'' string?
Case Asc("s")
'' Strings are passed byval, so the length is unknown
Print *Cva_Arg(args, ZString Ptr);
End Select
'' Ordinary char, just print as-is
Else
Print Chr( char );
End If
Wend
Cva_End( args )
End Sub
Dim As String s = "bar"
myprintf(!"integer=%i, longint=%l single=%f, double=%d, string=%s, string=%s\n", _
1, 1ll Shl 32, 2.2, 3.3, "foo", s)Category: Procedures
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing