how do i print all command line arguments?

i have no idea what command line is

all i know is that argc is the number of arguments
lets say i put 7 arguments
does this mean argc = 7?

i can do this
int temp = 0;
for(i=0;i<argc/2;i++)
{
temp = argv[i];
argv[i] = argv[size-i-1];
argv[size-i-1] = temp;
cout << argv[i] << " "<< endl;
}

do i have the right idea?
can anyone help me?


> lets say i put 7 arguments does this mean argc = 7?

If you put in seven arguments after the name of the program, argc == 8

argv[0] is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment).
http://en.cppreference.com/w/cpp/language/main_function


Run this program with different command lines to see what the arguments passed to main are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>
#include <iterator>

int main( int argc, char* argv[] )
{
    // print all command line arguments
    std::cout << "name of program: " << argv[0] << '\n' ;

    if( argc > 1 )
    {
        std::cout << "there are " << argc-1 << " (more) arguments, they are:\n" ;

        std::copy( argv+1, argv+argc, std::ostream_iterator<const char*>( std::cout, "\n" ) ) ;
    }
}
Last edited on
since you already got the answer, here's a cool modern way to do it:
1
2
3
4
5
6
7
#include <iostream>
#include <range/v3/all.hpp>
int main(int ac, char** av) 
{
    for(auto& s : ranges::view::counted(av, ac)) 
      std::cout << s << '\n';
}
live demo: http://coliru.stacked-crooked.com/a/ac67451cc5764844 <- go there, click Edit, and experiment with the program!

(for plain standard C++, that would be for(auto& s : std::vector<char*>(av, av+ac)) : http://coliru.stacked-crooked.com/a/1499c6b5ade61392 )
Last edited on
for plain standard C++, that would be for(auto& s : std::vector<char*>(av, av+ac))

A disadvantage is that this creates a lot of unnecessary code and can be replaced by a dead-simple for loop:
1
2
    for(int i = 0; i < argc; ++i)
        cout << argv[i] << '\n';

When I compile the two versions, the simple for loop is about half the size (3521 bytes of code vs. 7353). Sure it's less that 4k, but on the other hand, why create 4k of code from a 2 line for loop if you don't have to? Code bloat matters.
dhayden wrote:
Code bloat matters.

it is not the only thing that matters and wasn't the purpose of my comment - I was just showing a few other ways to approach the similar task.

But even just looking at that code size, my demo using ranges generated shorter code than the C-style loop (replacing operator<< with puts to trim noise) with the first compiler I tried, which was gcc 6.3.0 on a 64bit linux target (whatever coliru has)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <range/v3/all.hpp>
int main(int ac, char** av) 
{
    for(auto& s : ranges::view::counted(av, ac)) 
      puts(s);
}
main:
	movslq	%edi, %rdi
	pushq	%rbp
	pushq	%rbx
	leaq	(%rsi,%rdi,8), %rbp
	subq	$8, %rsp
	cmpq	%rbp, %rsi
	je	.L5
	movq	%rsi, %rbx
.L6:	movq	(%rbx), %rdi
	addq	$8, %rbx
	call	puts
	cmpq	%rbx, %rbp
	jne	.L6
.L5:	addq	$8, %rsp
	xorl	%eax, %eax
	popq	%rbx
	popq	%rbp
	ret
#include <stdio.h>

int main(int ac, char** av) 
{
      for(int i = 0; i < ac; ++i)
          puts(av[i]);
}
main:
	testl	%edi, %edi
	jle	.L7
	leal	-1(%rdi), %eax
	pushq	%rbp
	pushq	%rbx
	movq	%rsi, %rbx
	leaq	8(%rsi,%rax,8), %rbp
	subq	$8, %rsp
.L4:	movq	(%rbx), %rdi
	addq	$8, %rbx
	call	puts
	cmpq	%rbp, %rbx
	jne	.L4
	addq	$8, %rsp
	xorl	%eax, %eax
	popq	%rbx
	popq	%rbp
	ret
.L7:	xorl	%eax, %eax
	ret


Creation and destruction of a local vector can be optimized out (and is optimized out in simpler cases), but is indeed unnecessary bloat.

also, I disagree with
dhayden wrote:
dead-simple for loop:

There is nothing simple about introducing a new variable, a comparison (did you get the types right?) an increment, and indexed access only to iterate over a sequence. Just because something is familiar doesn't make it right.
Last edited on
it is not the only thing that matters and wasn't the purpose of my comment - I was just showing a few other ways to approach the similar task.
I understand completely. The point of my comment was just to point out that it comes with a cost that might not be obvious.

my demo using [for(auto& s : ranges::view::counted(av, ac))] generated shorter code
My comment was addressed at for(auto& s : std::vector<char*>(av, av+ac)) which generates this: on g++ 5.4.0:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cstdio>
#include <vector>
int main(int ac, char** av)
{
    for(auto& s : std::vector<char*>(av, av+ac))
        puts(s);
}
main:
.LFB766:
        pushq   %r12
        .seh_pushreg    %r12
        pushq   %rbp
        .seh_pushreg    %rbp
        pushq   %rdi
        .seh_pushreg    %rdi
        pushq   %rsi
        .seh_pushreg    %rsi
        pushq   %rbx
        .seh_pushreg    %rbx
        subq    $32, %rsp
        .seh_stackalloc 32
        .seh_endprologue
        movl    %ecx, %ebx
        movq    %rdx, %r12
        call    __main
        movslq  %ebx, %rbx
        salq    $3, %rbx
        movq    %rbx, %rbp
        sarq    $3, %rbp
        testq   %rbp, %rbp
        je      .L10
        movabsq $2305843009213693951, %rax
        cmpq    %rax, %rbp
        jbe     .L3
.LEHB0:
        call    _ZSt17__throw_bad_allocv
.L3:
        movq    %rbx, %rcx
        call    _Znwm
.LEHE0:
        movq    %rax, %rdi
        jmp     .L2
.L10:
        movl    $0, %edi
.L2:
        leaq    (%rdi,%rbx), %rsi
        testq   %rbp, %rbp
        je      .L4
        movq    %rbx, %r8
        movq    %r12, %rdx
        movq    %rdi, %rcx
        call    memcpy
.L4:
        cmpq    %rdi, %rsi
        je      .L5
        movq    %rdi, %rbx
.L6:
        movq    (%rbx), %rcx
.LEHB1:
        call    puts
.LEHE1:
        addq    $8, %rbx
        cmpq    %rbx, %rsi
        jne     .L6
.L5:
        testq   %rdi, %rdi
        je      .L12
        movq    %rdi, %rcx
        call    _ZdlPv
        jmp     .L12
.L11:
        movq    %rax, %rbx
        testq   %rdi, %rdi
        je      .L9
        movq    %rdi, %rcx
        call    _ZdlPv
.L9:
        movq    %rbx, %rcx
.LEHB2:
        call    _Unwind_Resume
.LEHE2:
.L12:
        movl    $0, %eax
        addq    $32, %rsp
        popq    %rbx
        popq    %rsi
        popq    %rdi
        popq    %rbp
        popq    %r12
        ret
Minimalism
1
2
3
4
5
#include <iostream>
int main( int argc, char *argv[] )
{
   while( --argc ) std::cout << *(++argv) << "\n";
}


test.exe Hello cplusplus forum 
Hello
cplusplus
forum

Last edited on
Topic archived. No new replies allowed.