简体   繁体   English

命令行参数和c中的scanf函数有什么区别?

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

What is difference between command line argument and scanf function in c? 命令行参数和c中的scanf函数有什么区别? 1) what does command line argument mean? 1)命令行参数是什么意思? 2)If i can take input from user through scanf then what is the need of commad line argument. 2)如果我可以通过scanf接受用户的输入,那么需要使用逗号行参数。 3) what is principal difference between scanf and command line rgument 3)scanf和命令行参数之间的主要区别是什么

There are three types of standard inputs basically: 基本上有三种类型的标准输入:

1.Compile time 1,编译时间

2.Load time 2.加载时间

3.Run time 3,运行时间

1.Compile time: In this type the programmer himself gives input in code only while compiling. 1.编译时间:这种类型的程序员自己只在编译时才输入代码。

2.Load time: Load time means when the program is being loaded into RAM for execution. 2.加载时间:加载时间是指程序被加载到RAM中执行的时间。 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. 在Linux终端的命令提示符下,键入./a.out(或任何可执行文件名称)时,您正在将可执行文件加载到RAM中,这是在编译后获得的。 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. 简而言之, 命令行args是加载时提供的输入。

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. 3.运行时间: scanf()是程序运行或执行时的时间,它是可用于在运行时提供输入的功能之一。 So with use of scanf () we can provide input to our program at run time basically. 因此,使用scanf(),我们基本上可以在运行时为程序提供输入。

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. 当您运行带有命令行参数的程序时,它们可作为参数供main函数使用。 Since they're C strings, you can read them as such. 由于它们是C字符串,因此您可以这样阅读它们。 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. 那正在运行带有两个参数的store程序。

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. 另一方面, scanf函数从标准输入中读取信息,这需要单独提供给可能提供的任何命令行参数。

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. 在程序启动后,scanf从输入管道读取信息。 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的程序,则可以使用

test.exe someArg

From the command line. 从命令行。

The scanf() function reads input according to what you specify from the standard input buffer stdin . scanf()函数根据您从标准输入缓冲区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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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