stdarg.h

stdarg.h
Стандартная библиотека
языка программирования С

stdarg.h — заголовочный файл стандартной библиотеки языка программирования Си, предоставляющий средства для перебора аргументов функции, количество и типы которых заранее не известны.

Содержимое stdarg.h часто используют в функциях с произвольным количеством аргументов (к примеру, printf, scanf).

Заголовочный файл определяет тип va_list и набор функций для операций над ним: va_start, va_arg, va_copy (C99), va_end.

#include <stdarg.h>
type va_arg(va_list ap, type);
void va_copy(va_list dest, va_list src);
void va_end(va_list ap);
void va_start(va_list ap, parmN);

Макрос va_start служит для инициализации списка переменных аргументов и должен иметь соответствующий вызов va_end. Макрос va_arg используется для получения доступа к очередному аргументу, а va_copy - для копирования объектов типа va_list.[1]

Пример

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
 
void var(char *format, ...)
{
        va_list ap;
        va_start(ap, format);
        if(!strcmp(format, "%d"))
        {
                int x = va_arg (ap, int);
                printf ("You passed decimal object with value %d\n", x);
        }
 
        if(!strcmp(format, "%s"))
        {
                char *p = va_arg (ap, char *);
                printf ("You passed c-string \"%s\"\n", p);
        }
        va_end (ap);
}
 
int main(void)
{
        var("%d", 255);
        var("%s", "test string");
        return 0;
}

Литература

  • Брайан Керниган (Brian Kernigan), Деннис Ритчи (Dennis Ritchie) "Язык программирования C" ("The C programming language"). — второе. — Williams Publishing House. — 304 с. — ISBN 978-5-8459-0891-9

Примечания


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Полезное


Смотреть что такое "stdarg.h" в других словарях:

  • Stdarg.h — is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. C++ provides this functionality in the header ; the C header, though permitted, is deprecated in C++.The… …   Wikipedia

  • Standard C Library — Die Standard C Library ist eine genormte Funktionsbibliothek für die Programmiersprache C, die etwa 200 häufig benötigte Funktionen für Ein und Ausgabe, mathematische Operationen, Verarbeitung von Zeichenketten, Speicherverwaltung und andere… …   Deutsch Wikipedia

  • Fonction variadique — En programmation informatique, une fonction variadique est une fonction d arité indéfinie, c est à dire qui accepte un nombre variable de paramètres. De nombreuses opérations mathématiques et logiques peuvent se représenter sous forme de… …   Wikipédia en Français

  • C standard library — The C Standard Library consists of a set of sections of the ANSI C standard in the programming language C. They describe a collection of headers and library routines used to implement common operations such as input/output[1] and string handling …   Wikipedia

  • Variadic function — In computer programming, a variadic function is a function of variable arity; that is, one which can take different numbers of arguments. Support for variadic functions differs widely among programming languages.There are many mathematical and… …   Wikipedia

  • Funciones de la biblioteca estándar de C — Anexo:Funciones de la biblioteca estándar de C Saltar a navegación, búsqueda El propósito de este artículo es proporcionar un listado alfabético de todas las funciones de la biblioteca estándar de C, y unas pocas funciones no estándar. Contenido… …   Wikipedia Español

  • Стандартная библиотека языка Си — Стандартная библиотека языка программирования С assert.h complex.h ctype.h errno.h fenv.h float.h inttypes.h iso646.h limits.h locale.h math.h setjmp.h signal.h stdarg.h stdbool.h stddef.h …   Википедия

  • Anexo:Funciones de la biblioteca estándar de C — El propósito de este artículo es proporcionar un listado alfabético de todas las funciones de la biblioteca estándar de C, y unas pocas funciones no estándar. Contenido 1 assert.h 2 ctype.h 3 errno.h 4 float.h …   Wikipedia Español

  • Standard Template Library — C++ Standard Library fstream iomanip ios iostream sstream string …   Wikipedia

  • C file input/output — C Standard Library Data types Character classification Strings Mathematics File input/output Date/time Localiza …   Wikipedia


Поделиться ссылкой на выделенное

Прямая ссылка:
Нажмите правой клавишей мыши и выберите «Копировать ссылку»