Test Your C Skills (2) - Chapter 13 Command Line Arguments...

Info iconThis preview shows pages 1–20. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 14
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 16
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 18
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 20
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 13 Command Line Arguments Q 13.1- What do the the ‘c’ and ‘v’ in age and argv stand for?" Q 13.2. According to ANSI specifications which is the correct way of declar- in g mainO when it receives command line arguments? A. main ( int argc. char *argv[ ] ) B. main ( argc, argv ) int argc ; char *argv ; C. main( ) ' { } D. None of the above Q 13.3 What would be the output of the following program?- int argc ; char *argv ; 1* Samplec ‘3’ main ( i-nt aigc,_char “argv) 142 Test Your C Skills { argc=argc-(argc-1); print! ( '%s", argv[argc - 1] ) ; } Q 13.4. If different command line arguments are supplied at different times would the ouptut of the following program change? <Yesto> main ( int argc, char *argVI ] ) l } Q 13.5. If the following program (myprog) is run from the command line as print! ( '%d', argv[argc] ) ; myprog 1 2 3 what would be the output? main ( int argc, char ‘argvi ]) { . inli; for(i=0;i<argc;i++) printfl '%s ', argvfill; } Q 13.6. If the follOwing program (myprog) is run from the command line as - Chapter 13: Command Line Arguments J43 myprog 1 2 3 what would be the output? main ( int argc, char *argv[ ] ) ' { int i i i = argvfl] + argV[2] + aTQVlsl I printf ( "%d'. i ) ; l A. 123 B. 6 C. Error D. "123" Q _ 13.7. If the following program (myprog) is run from the command line as myprog 1 2 3 what would be the output? I _ main ( int arQC, char *aFQVl ll inti,j=0; for(i=0;i<argc;i++} i=i+atoi(argvfi]); prinlf ('"%d", j ) ; 144 Test Your C Skills D. "123" Q 13.8. Would the following program give the same output at all times? <Yesto> - main ( int argc, char *argV[ ]) { strcpy ( argv[0]. 'hello' ) ; strcpy ( argv[1], 'good morining“ ) ; printt { '%s %s". argv[0]. argv[1] ) ; } . Q 13.9. - If the following program (myprog) is'run from the command line as myprog one two three what would be the output? main ( int argc, char *argv[ ] ) l } Q 13.10. _ If the following program (myprog) is run from the command line as print! ( "%s", *++argv) ; myprog one two three What would be the output? Chem 1 3: Command Line Arguments 145 main ( int argc. char ‘argv[ ] ) l } Q 13.11. The variables argc and argv are always local to main <TruelFalse>. Q 13.12.- The maximum combined length of the command line arguments including the spaces between adjacent arguments is print! ( “%c“. ++**++argv) ; A. 128 characters B. 256 characters C. 6'? characters I D. It may vary from one operating system to another Q 13.13. What will the following program output? main ( int argc, char *argv[ ], char *env[ ]) { int i : for{i=1;i<argc;i++} printf ( "%s ', env[i] ) ; l A. List of all environment variables B. _ List of all command line arguments . . C. Error D. NULL 146 - . ' Test Your CSkiIls Q 13.14. If the following program (myprog) is run from the command line as myprog "".c' what would be the output? main ( int argc, char *argv[ ] ) { int i ; for-(i=1 ;i<argc;i++) printf ( "%s argvli] ) : l A. *.c B. List of all .0 files in the current directory C. "*.c" D. None of the above Q 13.15. If the following program (myprog) is run from the command line as myprog *.c what would be the output? main ( int argc, char *argvl ]) i _ int i .' for(i=1;i<af.90;i++-) printf ( was '. argv[i] } : Chapter I 3: Command Line Arguments I47 *.c A. B. List of all .c files in the current directory C D. None of the above Q 13.16. If we want that any wildcard characters in the command line argu- ments should be appropriately expanded, are we required to make any special provision? If yes, which? Q 13.17. Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function? <Yesto> Q 13.18. I If the following program (myprog) is run from the command line as myprog Jan Feb Mar what would be the output? I ' #include'dosh' main() { } lun( ) .{ int]; 1 _. '. for-(i=o;i<__ar_gc;i++) iunt ) ; Cha rer 1'3: Command Line Ar uments 149 Q 13.22. If the following program (myprog) is run from the command line as prinh‘ ( "%s _argv[i] } ; } Q 13.19. If the following program (myprog) is present in the directory c:\bc\tucs then what would be its output? - myprog monday tuesday wednesday thursday what would be the output? main { int argc, char *argv{] ) { while ( --argc > 0} print! ( '%s '. *++argV) : main ( int____'c__1_rgc, char *argv[ I) { prinfi ( "%s argv[0] } ; } } A- MYPROG A. myprog monday tuesday wednesday thursday B. _ C:\BC\TUCS\MYPROG B_. monday ruesday wednesday thursday 0 Error C. myprog tLiesday thursday D. C:\B C\TUCS D, None of the above Q 13.20. Q 13.23. Which is an easy way to extract mypmg from the output of program 13.19 above? Q 13.21. Which of the following is true about argv? If the following program (mypmg) is run from the command line as myprog friday tuesday sunday what would be the output? main ( int argc. char *argv{ ] ) l } A. It is an array of character pointers B. . It is a pointer to an array of character pointers C. It is an array of strings . ' D. None of the above printf ( '°/oc'. { *++anV‘)[0] ) ; A. m B . f C myprog Chapter 13: Command Line Arguments 15} I50 Test Your C Skills D friday C. m D. y Q 13.26. If the following program (myprog) is run from the command line as If the following program (myprog) is run from the command line as myprog friday tuesday sunday rd m d d myprog n ay es ay sun ay what would be the output? what would be the output? main { int argc, char *argv{ ]) { main ( int sizeotargv, char *argv ) printf ( "%c“, **++argv ) : { } while { sizeofargv ) print! ( “%s ", argv[--sizeofargv] ) ; A. m - } B. f ‘ C. myprog A. myprog friday tuesday sunday D. friday B. myprog tuesday C. sunday tuesdayfriday myprog Q 1325'- D. sunday tucsday friday j nswers If the following program (myprog) is run from the commandline as A 13.1 myprog friday tuesday sunday What “mum be the 011mm? Count of arguments and vector (array) of arguments A 13.2 main ( int argc, char *arng 1 ) { - } A.r B f printf ( '°/oc'. *++argv[11 ) : Test Your C Skills 152 C :\S AMPLEEXE A 13.4 No A 13.5 CI‘MMYPROGEXE l 2 3 A 13.6 A 13.7 B. When aroif) tries to convert argv[0] to a number it cannot do so (argv[0] being a file name) and hence returns a zero. A 13.8 No \A 13.9 one A 13.10 Chagter 13: Command Line Arguments I53 A 13.11 True K ' A 13.12 D A 13.13 B A 13.14 A A. 13.15 A A 13.16 Yes. Compilethe programas - - 'tcc myprog wildargsobj This compiles the file myprog.c and links it with the wildcard {:2 expansion module WILDARGSDBJ, then run the resulting ex- ecutable file MYPROGEXE .1.‘ 154 Test Your C Skills . Chagter 13: Command Line Arguments 155 If you want the wildcard expansion to be default so that you won’t prinii { '\n°/os\n°/os\n°/os\n°/os'. drive. dir, name, ext) : have to link your program explicitly with WILDARGS .OBJ, you can ' if: } modify your standard C?.LIB library files to have WILDARGSDBJ ' jf-j linked automatically. To achieve this we have to remove SETARGV. ' A "1.3.21 from the library and add WILDARGS. The following commands will invoke the Turbo Librarian to modify all the standard library files (assuming the current directory contains the standard C libraries, and A WILDARGSDBJ ): ' ' tlib” cs -selargv +wildargs . tlib cc -setargv +wildargs tlib cm -setargv +wi|dargs B tlib cl -setargv +wildargs . tlib ch -setargv +wi|dargs _ A 13 23 A 13.17 _ ' - _ B Yes. Using the predefined variables _argc, _argv. A 13 24 A 13.18 B C:\MYPROG.EXE Jan Feb Mai ‘35; A 13.25 A 13.19 A B , A 13.26 A 13.20 #include 'dir.h' j C main ( int argc. char *argv[ ] ) ' { char drive[3]. dir[50], name[8]. ext[3] ; printf ( '\n%s', argv[0] ) ; insle ( argviO], drive. dir. name, ext ) ; Chapter 14 Bitwise Operators Q 14.1 What would be the output of the following program? main{) { inti=32,j=0x20,k,l,m; k=i|j; |=i&§; m=k"‘1; printf ( "%d %d %d %d °/cd". i. j, k, I, m) ; } A 323232320 :_ B. 00000 "c. 032323232 D 3232323232 Q 14.2 What would be the outputof the following program? main( ) { unsigned int m = 32 ; 158 Test Your C Skills I Chagrer I4: Bitwise Operators 1'59 printf{ '%x', ~m) ; . d I: 2; } r ' e 81: 2 ; I "x I ' I ' f "= 2 i A. ffff , printl ( “\n%x %x %x %x %x %x'. a, b, c, d, e. f) ; B. 0000 . ' } '\..__., . C. ffdf D- _ d‘ifd _ E" Q 14.5 Q 14.3 To which numbering system can the binary number 1011011111000101 be easily converted to? Q 14.6 _What would be the output of the following program? main( ) { . unsigned int a = Oxflll ; Which bitwise operator is suitable for checking whether a particular “a ' = _ bit is on or off? ' priritl ( '%x', a) ; } i . Q 14.? ffff _. ._ 0000 I Which bitwise operator is suitable for turning off a particular bit in a 0011 _ number? None of the above - ' I ' Q 14.8 .o Poss? 14.4 . I Which bitwise operator is suitable for pnttin g on a particular bit in a Pomt out the error in the following program. number? main( ) ' ' Q M 9 unsigned int a, b, _c. d, e, f ; _ ' -a = b = c = d = e = f': 32 ; - On left shifting, the bits from the left are rotated and brought" to the a<<=2: _ . right and accommodated where there is empty space on the right? b >>= 2 ; i <FruefFalse> 160 Test Your C Skills Chapter I4: Bitwise Operators 161 Q 14.10 scani ( "°/ed", &n ) ; f0r(i=0;i<=7; i++) Left shifting a number by 1 is always equivalent to multiplying it be { . 2. <Yesto> if ( n & m[i]) prinfl ( "\nyes" ) ; Q 14.11 i } Left shifting an unsigned in: or char by l is always equivalent to A. Putting off all bits which are on in the number n multiplying it be 2. <YesiNo> B. Testing whether the individual bits ofn are on or off ' C. This program would give an error D. None. of the above Q 14.12 What would be the output oi‘ the following program? Q 14.14 - l main() What does the following program do? unsigned char i = 0x80; main( ) print! ( "\n%d', i << 1 ) ; { } char *3 ; s=fun(128,2); A. 0 printf ( "\n%s". s } ; B. 256 } C. 100 D. None of the above fun ( unsigned int num, int base) Q 14 13 staticiehar buff[33] ; char ptr; What is the following program doing? plr = &buff { sizeol ( bull ) i 1 ] ; ‘plr = ’\0’ ; main( ) do { { unsigned int m[ ] = ( 0x01, 0x02, 0x04, 0x08, 0x10, 0x20. 0x40. 0x80 } ; *--pir = "0123456789abcdef"{ num % base ] ; unsigned char n, i; num l: base ; 1’62 Test Your C Skills Chapter 1' 4: Bitwise Operators 163 }while(num!=0); { unsigned int num ; reiurn ptr ; int 6 = 0 : l scant (_ “%u", anum ) ; for ( ; n‘um..;znum >>= 1 ) A. It converts a number to given base i B. It gives a compilation error ii ( “um 3‘1 l C None of the above C++§ } Q 1415 } printf ( "%d". c) ; A It counts the number of bits which are on in the number mm. B. It sets all bits in the number rum to 1. C. It sets all bits in the number num to O. D None of the above #define CHARSIZE 8 #define MASK(y) ( 1 or. y % CHARSIZE} #deline BlTSLOT(y} ( v I CHARSIZE ) #define SET{ x. y) ( x[BITSLOT[y)] I: MASK(y) ) #define TEST(X, y) ( x[BITSLOT(y)] 81 MASK(y) ) #define NUMSLOTS{n) ([n + CHARSIZE - 1) lCHARSIZE) Q 1418 Given the above macros how would you . What would be the output of the following program? — declare an array arr of 50 bits — put the 20'11 bit on main“ - test whether the 40"1 bit is on or off { printf( "\n%x“, —1 >> 4) ; Q 1416 l A. ffff Consider the macros in Problem 14.15 above. On similar lines can 13- Offf you define a macro which would clear a given bit in a bit array? C- 0000 D. fffCl Q 14.17 What does the following program do? Q 14.19 In the statement expression} >> expressionZ if expression} is a main( l - signed integer with its leftmost bit set to 1 then on right-shifting it the . Ch t M:B'tw' 0 t 164 Test YourCSkles “ 8" I we em on I65 A 14.3 result of the statement would vary from computer to computer. <Truer’False> Q 14.20 What does the following program do? Error is in f ~= 2, since there is no operator like ~=. A 14.5 main() { unsigned int num ; int i ; scant ( "%u“. &num ) ; ior(i=0;i<16;i++} _ printf("°/od", ( num << i &1 << 15 }? 1 :0); Hexadecimal, since each 4—di git binary represents one hexadecimal digit. } A 14.6 A. It prints all even bits from mm B. It prints all odd bits from mm The & operator C. It prints binary equivalent of mm: ' D. None of the above A M 7 Answers The & operator A 14.1 ' ' A 14.8. A A 14.2 The | operator A 14.9 C False I66 5 Test Your C Skills I I. Chagrer I4: Birwise Ogemrors I67 . A 14.10 A 14.17 No A -_ A 14.11 A 14.18 x Yes A. On computers which don’t support Sign extension you may get B. A 14.12 A 14.19 B True A I 14.13 A 14.20 B C - A 14.14 A A 14.15 char arr[NUMSLOTS(50}} ; SET(ar_r. 20 } ; if (TEST ( arr', 40 ) ) ' A 14.16 #define CLEAR(x, y) (x[B|TSLOT(y)] &= ~MASKan Chapter 15 Sybtleties of typedef e H tn 1-... Are the properties of 1', j and x, y in the following program same? I <Yesto> typedei unsigned long int uli; uli i, i; unsigned long int x. Y ; .53: lypedef int ( fpmofun ){ char *. char * ) ; ptrtofun compare : What are the advantages of using typedef in a program? I70 ' Test Your C Skills Q 15.4 Is there any diffemce in the #define and the typedef in the following code? If yes, what? typedei char * string_l; #define slring_d char * string_t st, 32; slringfld $3, 54; Q 15.5 wpedefs have the advantage that obey scope rules, that is, they can be declared local to a function or a block whereas #defines always have a global effect. (FmefFalse) Q 15.5 Point out the error in the following declaration and suggest atleast three solutions for it. lypedei strucl l int data; NODEPTR link; }*NODEPTR; ‘P Q 15.7 In the following code can we declare a new typedef name amp even though struct employee has not been completely defined while using typedef? <Yesto> mm; ram: Q 15.9 What do the following declarations mean? Cheater 15: S ubtlett'es of agedef I71 I typedef struct employee *ptr ; slrucl employee { char name[20] ; in! age ; ptr next ; } ; . Q 15.8 There is an error in the following declarations. Can you rectify it? : typedef slruct int dalal ; BPTR link1 ; 3;" typedelstmcl ' { int data2 ; APTR Iink2 : ' lypedef char *pc; - Wpedelpcfpcl): lypedeflpc *plpc; typedel pfpc fpfpcl ) : lypedel fppr ‘pfpipc; pfpfpc a[N]; 172 . Test Your C Skills Cha ter 15: Subtlett'es 0 ede 173 Q 15.10 How would you define a[N] in 15.9 above without using typedef? Q 15.11 Improve the following code using typedef. Q 15.14 In the following code is p2 an integer or an integer pointer? typedef int " ptr ; Ptr P1. 132 ; slruct node _ { . int datal : float dataQ : struct node *left; struct node *right ; } : si'rucl node *ptr ; ptr = ( struct node * ) malloc ( sizeol ( slrucl node ) ) ; _ Q 15.12 Is the following declaration acceptable? <Yesto> It is a pointer to function which receives two character pointers and returns an integer. A 15.3 typedei long no, *ptrtono ; no n ; ptrtono p ; Q 15.13 In the following code what is constant. p or the character it is pointing to? There are three main reasons for using typedefs: (a) It makes writing of complicated declarations a lot easier. This helps in eliminating a lot of clutter in the program. (b) It helps in achieving portability in programs. That is, if we use typedefs for data types that are machine-dependent, only the wpedefi need change when the program is moved to a new machine platform. (6) It helps in providing a better doumentation for a program. For example, a node of a doubly linked list is better understood as prrroiist rather than just a pointer to a complicated structure. typedel char * charp ; const charp p ; LZ4—_______TM I Cha ter 15: Subtleties 0 ede [75 f 1 *NODEP - A 15.4 , typede srucl node _ TR, _ (c) Precede the structure declaration with typedef, so that you can In these declarations, s}, 32, and 33 are all treated as char *, but 54 is use the NODEPTR typedef when declaring the 1in field: treated as a char, which is probably not the inetention. iypedei struct node *NODEPTR ; A 15 5 : situct node { - int data ; True " _ NODEPTR link; " } : A 15.6 A typedef declaration cannot be used until it is defined, and in our example it is not yet defined at the point where the link field is I Yes declared. I - We can fix this problem in three ways: (:1) Give the structure a name, say node and then declare the link The problem with the code is the compiler doesn’tknow about BPTR field as a simple struct node * as shown below: . when it is used in the first structure declaration. We are violating the ‘ rule that a typedefdeclaration cannot be used until it is defined, and typedef struct nod : ' in our example it is not yet defined at the point where the link} field { ' is declared. int data ; - siruct node *Iink; ' ' To avoid this problem we can define the structures as shown below: } *NODEPTR ; '5 . struct a 03) Keep the typedef declaration separate from the structure defini- ' { tion: 5 int datal ; . siruci b *linkl ; struct node . _ - } ; { struct b int data ; _ { slruct-node *link ; int data2 ; } ; . "i struct a *Iink2 ; 176 TestYour C Skills l : typedef struct a *APTR ; typedel struct b *BPTR ; The compiler can accept the field declaration street 3;» *ptrl within struct 11, even though it has not yet heard of strucr b (which is "incomplete" at that point ). Occasionally, it is necessary to precede this couplet with the line struct b : This empty declaration masks the pair of structure declarations (if in an inner scope) from a different street .6 in an outer scope. After declaring the two structures we can then declare the @pedefs separately as shown above. Alternatively, we can also define the typedefs before the structure - definitions, in which case you can use them when declaring the link pointer fields as shown below: typedef struct a *APTR; typedei slruct b *BPTR; struct a { int datai ; BPTR linki ; } ; struct b _ { int data2 ; APTH link2 ; }; Chapter [5: Subtieties oftygedef 177 A 15.9 pc fpc pfpc fppr pfpfpc pfpfpc a[N] A 15.10 char *(‘(*a[N]l)( )N } I A 15.11 is a pointer to char. is function returning pointer to char is pointer to a function returning pointer to char is a function returning pointer to a function returning pointer to char is a pointer to function returning pointer to a function returning pointer to char is an array of N pointers to functions returning pointers to functions retuming pointers to characters typedef struct node * treeptr typedei struct node { int data1 ; float data2 ; treeptr *Ietl ; treeptr *right ; }treenode ; treeptr ptr; ptr = ( lreeptr ) malloc ( sizeol ( treenode ) } ; A 15.12 Yes I 78 Test Your C Skills A 15.13 Chapter 16 'pisconstant The const Phenomenon A 15.14 Integer pointer Q 16.1 Point out the error in the foliowing program. main( ) { const int x ; x = 128 ; printf ( "%d", x } ; } . Q 16.2 What would be the output of the following program? main{ ) { int y = 128; const int x = y; prinfi { "%d", x) ; bu-o A. 1 28 B. Garbage value C. Error ...
View Full Document

This note was uploaded on 08/08/2011 for the course CS 101 taught by Professor Jitenderkumarchhabra during the Summer '11 term at National Institute of Technology, Calicut.

Page1 / 20

Test Your C Skills (2) - Chapter 13 Command Line Arguments...

This preview shows document pages 1 - 20. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online