15 2. arguement passing to main

238 views
205 views

Published on

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
238
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

15 2. arguement passing to main

  1. 1. Argument Passing to main()
  2. 2. 2Passing Arguments to main( ) Passing Arguments to main( )– Example: print the arguments of main()C:> myprog a b c dprogram name: myprogargument 1: aargument 2: bargument 3: cargument 4: dC:>
  3. 3. 3Passing Arguments to main( ) Passing Arguments to main( )– You can declare formal parameters to main so that it canreceive arguments from the command line• argc : number of arguments from command line• argv : string array of arguments value from command line[Ex] int main(int argc, char *argv[])[Ex] myprog c java pascalargc = 4argv[0] => “myprog”argv[1] => “c”argv[2] => “java”argv[3] => “pascal”
  4. 4. 4Passing Arguments to main( )[Ex] #include <stdio.h>int main (int argc, char *argv[]){int count;printf(“program name: %sn”, argv[0]);if(argc > 1){for( count=1; count < argc; count++ )printf(“argument %d = %sn”, count, argv[count]);}elseputs(“No comand line arguments”);return 0;}> a.out hi helloa.outargv[1] = hiargv[2] = hello
  5. 5. 5Passing Arguments to main( ) in goormIDE Move the cursor to Terminal window
  6. 6. 6Passing Arguments to main( ) in goormIDE Type “cd bin” to change directory
  7. 7. 7Passing Arguments to main( ) in goormIDE Type “./main” and arguments

×