简体   繁体   中英

What is difference between command line argument and scanf function in c?

What is difference between command line argument and scanf function in c? 1) what does command line argument mean? 2)If i can take input from user through scanf then what is the need of commad line argument. 3) what is principal difference between scanf and command line rgument

There are three types of standard inputs basically:

1.Compile time

2.Load time

3.Run time

1.Compile time: In this type the programmer himself gives input in code only while compiling.

2.Load time: Load time means when the program is being loaded into RAM for execution. In linux terminal in command prompt when you type ./a.out (Or any executable name) you are loading your executable file into RAM, which you've got after compiling it. So while loading the executable along with the executable whatever you are passing through command prompt is treated as command line arguments. And that can be used in code some where at run time. In short command line args is the input provided at load time.

3. Run time: Its the time while program is running or being executed, scanf () is one of the functions that can be used to provide input at run time. So with use of scanf () we can provide input to our program at run time basically.

They're two different ways of getting information into the program.

When you run a program with command line arguments, they're made available to the main function as parameters. Since they're C strings, you can read them as such. Running it with command line arguments is basically something like:

store picture_of_zx80.jpg myPornDirectory

That's running the store program with two arguments.

The scanf function, on the other hand, reads information in from standard input, something that needs to be provided separately to any command line arguments that may be provided.

A command line argument is added when you start the program.

e.g. notepad.exe myletter.txt

scanf reads information from the input pipe in other words after the program has started. it also can apply some formatting to the input data.

Command Line arguments are arguments you pass to your program when you start executing it, which then may be used in the program, for instance to control certain behaviour. They can be specified when you run it, for instance, if you had a program called test.exe, you could run it with

test.exe someArg

From the command line.

The scanf() function reads input according to what you specify from the standard input buffer stdin . In programs executed on the command line this is typically done to recieve user input, for instance like this:

int main(void) {
int input = 0;
scanf("%d",input);
printf("You inputted: %d",input);
return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM