Upload
kbscribd
View
219
Download
0
Embed Size (px)
Citation preview
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 1/15
stdarg
Vijaya Saradhi
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 2/15
In troductio n
So far: ± writte n fun ctio n s with fixed n umber of argume n ts ±
fixed data typespri n tf (sca n f) ca n take variable n umber of argume n ts
Variable data typesCan we write fu n ctio n s which accepts variablen umber of argume n ts?
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 3/15
ExampleFinding minimum of given set of numbers:
int min2( int n1, int n2 )
{
if(n1 < n2 ) return n1;
elsereturn n2;
}
int min3( int n1, int n2, int n3 ){
int n = min2(n1, n2 ) ;return ( min2( n, n3) ) ;
}
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 4/15
Variable argume n ts?
Can we have a fu n ctio n min that acceptsvariable argume n ts?
Calls to this fu n ctio n looks some thi n g like: ± min (3, 2); ± min (4, 3, 2); ± min (5, 4, 3, 2); ± min (6, 5, 4, 3, 2); ± etc.
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 5/15
O bservatio n
N umber of argume n ts are u n kn ow n
T heir type is n ot specified before ha n d
How to declare this ki n d of fu n ctio n s?
What is declaratio n of pri n tf/sca n f? ± in t pri n tf(con st char *format, ); ± in t sca n f(con st char *format , );
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 6/15
stdarg
Has three useful macros ± va_start ±
va_arg ± va_e n d
Help us go through variable argume n ts list
Un
derstan
d type of argumen
tsva_list is a type which holds the i n formatio n to argume n t list
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 7/15
How to do?
Four steps i n volved
Get a hold of argume n t list ± through va_list
In itialize the argume n t list
walk through the argume n t liste n d the walk
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 8/15
Example# include<stdio.h># include<stdarg.h>
int main(int argc, char *argv[] )
{
int min(int length, ... ) ;int k = 0;
k = min(2, 3, 2 ) ;k = min(3, 4, 3, 2 ) ;
k = min(4, 5, 4, 3, 2)
;k = min(5, 6, 5, 4, 3, 2 ) ;k = min(6, 7, 6, 5, 4, 3, 2 ) ;k = min(7, 8, 7, 6, 5, 4, 3, 2 ) ;
}
int min(int length, ... )
{
va_list arg_list;int i = 0, min_m = 100000;
va_start (arg_list, length ) ;for( i = 0; i < length; i++ )
{
int n = va_arg (arg_list, int ) ;if( n < min_m )
min_m = n;}va_end (arg_list ) ;return min_m;
}
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 9/15
M in i prin tf # include<stdio.h># include<stdarg.h>
int main(int argc, char *argv[] )
{
void miniprintf(char *format, ... ) ;int k = 10;double d = 70.7;
miniprintf(³%d%f\n´, k, d ) ;
}
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 10/15
M in i prin tf Co n td.void miniprintf(char *format, ... )
{
va_list arg_list;int k = 0;double d = 0;char *p = NULL;
va_start (arg_list, format ) ;for( p = format; *p; p++ )
{
if( *p != µ%¶)
{
putchar(*p ) ;continue;
}
switch( *++p )
{
case µd¶:k = va_arg (arg_list, int ) ;printf(³%d\n´, k ) ;break;
case µf¶:d= va_arg (arg_list, double ) ;printf(³%f\n´, d ) ;break;
}}va_end (arg_list ) ;
}
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 11/15
ctype
Get to k n ow about the classificatio n of charactersT
he followin
g are useful fun
ction
sisdigi t ( in t c ) digi t or not ( 0 to 9 )
isbla n k ( in t c ) space or tab
iscn trl ( i n t c ) co n trol character
isgraph( i n t c ) pri n table characterislower ( i n t c )
isupper ( i n t c )
isspace ( i n t c ) tab, n ew li n e, space etc.
isxdigit ( i n t c ) hexa decimal?
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 12/15
setjmp
with goto ca n tra n sfer co n trol withi n afun ctio n
to tran
sfer con
trol to differen
t fun
ction
± setjmp ± lon gjmp
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 13/15
Example
mai n ( ) read_li n e get_word get_cmd
mai n
read_li n e
get_word
get_cmd
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 14/15
What we wa n t?
mai n
read_li n e
get_word
get_cmd
8/7/2019 stdarg
http://slidepdf.com/reader/full/stdarg 15/15
Setjmp/lo n gjmp# include<stdio.h># include<setjmp.h>
jmp_buf jump_point;
int main(int argc, char *argv[] )
{
if( setjmp(jump_point) != 0 )
printf(³I am back to main\n´ ) ;else
read_line()
;return 0;}
void read_line( )
{
printf(³I am in read_line\n´ ) ;get_word( ) ;
}
void get_word( )
{
printf(³I am in get_word\n´ ) ;if( 1 )
{
printf(³before longjmp\n´ ) ;longjmp(jump_point, 1);
}}