gcc - stdarg and printf() in C -


the <stdarg.h> header file used make functions accept undefined number of arguments, right?

so, printf() funtion of <stdio.h> must using <stdarg.h> accept avariable number of arguments(please correct me if i'm mistaken).

found following lines in stdio.h file of gcc:

#if defined __use_xopen || defined __use_xopen2k8 # ifdef __gnuc__ #  ifndef _va_list_defined typedef _g_va_list va_list; #   define _va_list_defined #  endif # else #  include <stdarg.h>//////////////////////stdarg.h included!/////////// # endif #endif 

i can't understand of what's in it, appears including <stdarg.h>

so, if printf() uses <stdarg.h> accepting variable number of arguments , stdio.h has printf(), c program using printf() need not include <stdarg.h> it?

i tried program had printf() , user-defined function accepting variable number of arguments.

the program tried is:

#include<stdio.h> //#include<stdarg.h>///if included, program works fine.  void fn(int num, ...) {     va_list vlist;     va_start(vlist, num);//initialising va_start (predefined)      int i;      for(i=0; i<num; ++i)     {         printf("%d\n", va_arg(vlist, int));     }      va_end(vlist);//clean memory }  int main() {     fn(3, 18, 11, 12);     printf("\n");     fn(6, 18, 11, 32, 46, 01, 12);      return 0; } 

it works fine if <stdarg.h> included otherwise generates following error:

40484293.c:13:38: error: expected expression before ‘int’          printf("%d\n", va_arg(vlist, int));//////error: expected expression before 'int'/////////                                       ^~~ 

how this?

or printf() doesn't use <stdarg.h> accepting variable number of arguments? if so, how done?

consider:

stdio.h:

int my_printf(const char *s, ...);  

do need <stdarg.h>? no, don't. ... part of grammar of language - it's "built-in". however, want meaningful , portable such list of arguments, need names defined in there: va_list, va_start , on.

stdio.c:

#include "stdio.h" #include "stdarg.h"  int my_printf(const char *s, ...) {    va_list va;    va_start(va, s);     /* , on */ } 

but necessary, essentially, in implementation of libc don't see unless compile library on own. instead libc shared library, has been compiled machine code.

so, if printf() uses accepting variable number of arguments , stdio.h has printf(), c program using printf() need not include it?

even if so, cannot rely on that, otherwise code ill-formed: must include headers anyway if name belonging them used, regardless whether implementation or not.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -