Difference between stdin and calling arguments?

前端 未结 1 1820
孤城傲影
孤城傲影 2021-01-14 09:05

If I had a method

int main(int argc, char* argv[]) {

I am under the impression that calling arguments and receiving data through standard i

相关标签:
1条回答
  • 2021-01-14 09:27

    The difference is in how you access them.

    Arguments are accessible through argv. Standard input is accessible through the stdin file descriptor.

    case 1 - command line arguments:

    int i;
    for (i=1; i < argc; i++) {
        printf("%s", argv[i]);      // Prints "1 2 3 4"
    }
    

    case 2 - standard input:

    char buffer[121];
    while (scanf("%120s", buffer) == 1) {
        printf("%s", buffer);      // Prints "5 6 7 8"
    }
    
    0 讨论(0)
提交回复
热议问题