C command line parameters

Source: Internet
Author: User

C language command line parameters[Transfer]

In an environment that supports the C language, you can pass the command line parameters to the program when the program starts to execute.

When calling the main function, there are two parameters. The first parameter indicates the number of parameters in the command line when the program is running. The second parameter indicates the pointer to the string array, each string corresponds to a parameter.

Main (INT argc, char * argv [])

Argc and argv are habitual usage and can be changed.

Argv is a pointer to a pointer,

Each element of this array is a character pointer. The first parameter to which the Pointer Points is the program name, and the last parameter is null.

# Include <stdio. h>

Int main (INT argc, char
* Argv [])
{
Int I;
For (I
= 1; I <argc; I ++)
Printf ("% S % s", argv [I], (I <argc
-1 )?
"":
"");
Printf ("/N ");
System ("pause ");
Return
0;
}

Compile the program and generate an executable file. The name is ee.exe and the path is D :\.
Note: This code is ineffective in the compiling environment and must be run in DOS.

Enter the DOS environment, enter D: \, and enter

EE Hello world will print out Hello world. According to the C language, the value of argv [0] is the name of the program that starts the program, so the value of argc is at least 1. If the value of argc is 1, there is no command line parameter after the program name. In the preceding example, the value of argc is 3, the values of argv [0], argv [1], and argv [2] are "ee", "hello", and "world", respectively ". In Unix systems, C language programs have a common convention that a parameter starting with a negative sign represents an optional flag or parameter. Pattern lookup program: # include <stdio. h>
# Include <string. h>
# Define maxline 1000
Int Getline (char line [], int max );

Main (INT argc, char
* Argv [])
{
Char line [maxline];
Long lineno =
0;
Int C, counter T =
0, number =
0, found =
0;

While (-- argc> 0 &&
(* ++ Argv) [0] =
'-')
While (C
= * (++ Argv [0])
Switch (c)
{
Case 'X ':
Counter T =
1;
Break;
Case 'N ':
Number =
1;
Break;
Default:
Printf ("find: Illegal option % C \ n", C );
Argc =
0;
Found =
-1;
Break;
}
If (argc
! = 1)
Printf ("Usage: Find-X-N pattern \ n ");
Else
While (Getline (line, maxline)>
0)
{
Lineno ++;
If (strstr (line, * argv )! = NULL)
! = Snapshot T)
{
If (number)
Printf ("% ld:", lineno );
Printf ("% s \ n", line );
Found ++;
}
}

Return found;
}

Int Getline (char s [], int Lim)
{
Int C, I;
For (I
= 0; I <lim-1 &&
(C = getchar ())! = '\ N ';
I ++)
S [I] = C;
S [I] =
'\ 0 ';
Return I;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.