?? p2
字號:
.NHBASICS.NH 2Program Arguments.PPWhen a C program is run as a command,the arguments on the command line are made availableto thefunction.UL mainas an argument count.UL argcand an array.UL argvofpointers tocharacter stringsthat containthe arguments.By convention,.UL argv[0]is the command name itself,so.UL argcis always greater than 0..PPThe following program illustrates the mechanism:it simply echoes its argumentsback to the terminal.(This is essentially the.UL echocommand.).P1main(argc, argv) /* echo arguments */int argc;char *argv[];{ int i; for (i = 1; i < argc; i++) printf("%s%c", argv[i], (i<argc-1) ? ' ' : '\n');}.P2.UL argvis a pointer to an arraywhose individual elements are pointers to arrays of characters;each is terminated by.UL \e0 ,so they can be treated as strings.The program starts by printing .UL argv[1]and loops until it has printed them all..PPThe argument count and the argumentsare parameters to.UL main .If you want to keep them around so otherroutines can get at them, you mustcopy them to external variables..NH 2The ``Standard Input'' and ``Standard Output''.PPThe simplest input mechanism is to read the ``standard input,''which is generally the user's terminal.The function.UL getcharreturns the next input character each time it is called.A file may be substituted for the terminal byusing the.UL <convention: if.UL proguses .UL getchar ,thenthe command line.P1prog <file.P2causes.UL progto read.UL fileinstead of the terminal..UL progitself need know nothing about where its inputis coming from.This is also true if the input comes from another program viathe .U pipe mechanism:.P1otherprog | prog.P2provides the standard input for.UL progfrom the standard output of.UL otherprog..PP.UL getcharreturns the value.UL EOFwhen it encounters the end of file(or an error)on whatever you are reading.The value of.UL EOFis normally defined to be.UL -1 ,but it is unwise to take any advantageof that knowledge.As will become clear shortly,this value is automatically defined for you whenyou compile a program,and need not be of any concern..PPSimilarly,.UL putchar(c)puts the character.UL con the ``standard output,''which is also by default the terminal.The output can be captured on a fileby using.UL > :if.UL proguses.UL putchar ,.P1prog >outfile.P2writes the standard output on.UL outfile instead of the terminal..UL outfileis created if it doesn't exist;if it already exists, its previous contents are overwritten.And a pipe can be used:.P1prog | otherprog.P2puts the standard output of.UL proginto the standard input of.UL otherprog..PPThe function.UL printf ,which formats output in various ways,usesthe same mechanism as.UL putchardoes,so calls to.UL printfand.UL putcharmay be intermixed in any order;the output will appear in the order of the calls..PPSimilarly, the function.UL scanfprovides for formatted input conversion;it will read the standard input and break itup into strings, numbers, etc.,as desired..UL scanfuses the same mechanism as.UL getchar ,so calls to them may also be intermixed..PPMany programsread only one input and write one output;for such programs I/Owith.UL getchar ,.UL putchar ,.UL scanf ,and.UL printfmay be entirely adequate,and it is almost always enough to get started.This is particularly true ifthe.UC UNIXpipe facility is used to connect the output ofone program to the input of the next.For example, the following programstrips out all ascii control charactersfrom its input(except for newline and tab)..P1#include <stdio.h>main() /* ccstrip: strip non-graphic characters */{ int c; while ((c = getchar()) != EOF) if ((c >= ' ' && c < 0177) || c == '\t' || c == '\n') putchar(c); exit(0);}.P2The line.P1#include <stdio.h>.P2should appear at the beginning of each source file.It causes the C compiler to read a file.IT /usr/include/stdio.h ) (ofstandard routines and symbolsthat includes the definition of.UL EOF ..PPIf it is necessary to treat multiple files,you can use.UL catto collect the files for you:.P1cat file1 file2 ... | ccstrip >output.P2and thus avoid learning how to access files from a program.By the way,the call to.UL exitat the end is not necessary to make the program workproperly,but it assures that any callerof the program will see a normal termination status(conventionally 0)from the program when it completes.Section 6 discusses status returns in more detail.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -