1 / 14

Passing UNIX parameters to C (1)

array of strings (char *). Number of arguments. Passing UNIX parameters to C (1). Often a user wants to pass parameters into the program from the UNIX prompt obelix% progname arg1 arg2 arg3 This is accomplished in C using argv and argc For example:

piera
Télécharger la présentation

Passing UNIX parameters to C (1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. array of strings (char *) Number of arguments Passing UNIX parameters to C (1) • Often a user wants to pass parameters into the program from the UNIX prompt obelix% progname arg1 arg2 arg3 • This is accomplished in C using argv and argc • For example: int main (int argc, char *argv[ ] ) { /* Statements go here */ } • Call this program from UNIX obelix% progname arg1 "second arg" arg3

  2. Passing UNIX parameters to C (2) Example: #include <stdio.h> int main(int argc, char *argv[]) { int count; printf ("Program name: %s\n", argv [0]); if (argc > 1) { for (count=1; count<argc; count++) printf ("Argument %d: %s\n",count,argv[count]); } else puts ("No command line arguments entered."); } • Say we compile this program to file myargs

  3. Passing UNIX parameters to C (3) Output: obelix% myargs first “second arg” 3 4 > myargs.out Program name: myargs Argument 1: first Argument 2: second arg Argument 3: 3 Argument 4: 4

  4. /* show file */ #include <stdio.h> int main(int argc, char *argv[ ]) { FILE *fp; int k; if(argc !=2) { printf(“Usage: %s filename \n”, argv[0]); return 0; } if((fp=fopen(argv[1], “r”))==NULL){ printf(“Cannot open file\n”); return 1; } while( (k=fgetc(fp))!=EOF ) putc(k); fclose(fp); } Generally a main function of a C program for Unix checks whether the arguments are valid and prints simple help information. Passing Unix arguments to C(4)

  5. Variables declared outside of any blocks are global variables. Variables declared inside of a block is local to that block. A local variable declaration hides the variable defined outside of the block with the same name. int a=1; void test(){ printf(“%d”,a); } int main( ) { int a=2; { int a=3; printf(“%d”,a); } printf(“%d”,a); test(); } Global and local variables

  6. You can use the global variable in any part of your program, even in another file. If the real declaration is in another file, you should use extern to let the compiler know. /* test.c */ extern int a; int b=3; int test( ) { printf(“a=%d, b=%d\n”,a,b); } /* test_main.c */ int a=4; extern int b; int test( ); int main( ) { printf(“a=%d, b=%d\n”,a,b); test(); return 0; } extern

  7. auto variables • Local variables are auto by default. • { int x; …… } is equivalent to • { auto int x; ……} • This means that virtually the variable only exists when the program is in the range of the block. • When the program goes out of the range, and goes into the range again, the variable should be considered as a new variable • The value at the last time is not kept. • You have to initialize the variable again. • Let us examine this by an example:

  8. #include <stdio.h> #include <stdlib.h> void func1() { int x; print(“%d ”,x); x = 10; } void func2() { int y = rand(); } int main() { int i; for(i=0;i<5;i++){ func1(); func2(); } return 0; } This prints out some random numbers. Means that the value of x is not predictable when next time goes into func1(). auto variables live locally

  9. static variables • Sometimes it is important to keep a local variable long-live. • For example we want a function to report how many times it has been called. int count( ){ int x=0; return (x++); } • The above codes do not work since x is auto. • We use static keyword to solve the problem: int count( ){ static int x=0; return (x++); } int main() { printf(“%d, %d, %d \n ”, count(), count(), count() ); }

  10. Auto and static • Auto variables only exist when the program is in the block that the variables are defined. • Static variables exist for all the time of your program’s running. • As a real example, the function rand( ) has a few of static local variable. • rand() changes that variable to a different value each time it is called

  11. Convert numbers between bases void int_to_b(char buf[ ], int k, int base) { static char digits[16]={‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’a’,’b’,’c’,’d’,’e’,’f’}; int i, j; for( j=0; ; j++){ buf[ j ] = digits[ k%base]; k = k / base; if(k == 0) break; } /* reverse the buf */ for( i =0; i< j/2; i++ ) { char tmp = buf [ i ]; buf [ i] = buf[ j - i ]; buf [ j - i ] = tmp; } }

  12. Inline functions • Recall the two ways to compute the maximum number between two integers: • #define max(a,b) ((a)>(b)? (a):(b)) • int max(int a, int b) { return a>b?a:b; } • Function calls need to jump to another part of your program and jump back. This needs • save the current registers’ status • allocate memories in the stack for the local variables in the function that is called • other overhead …… • Therefore, the first one is often more efficient since it does not need a function call. • But it is very error prone.

  13. Inline functions • The modern C and C++ compilers provide the inline functions to solve the problem: • put the inline keyword before the function head. inline int max(int a, int b) { return a>b?a:b; } • You use it as a normal function in your source code. • printf( “%d”, max( x, y) ); • When the compiler compiles your program, it will not compile it as a function. Rather, it just integrates some necessary codes in the line that max() is called so that the function call is avoided. • The above printf(…) is compiled in a way like • printf(“%d”, x>y?x:y);

  14. Inline functions • Write the small but often-used functions as inline functions can improve the speed of your program. • A small trouble is that you have to already included the inline function definition before you use it in a file. • For normal functions, only the function prototypes are needed. • Therefore, inline functions are often defined in the header (.h) files. • Once you include the header file, you can use • the inline functions whose definitions are in that header file. • the normal functions whose prototypes are in that header file.

More Related