LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2015, 06:23 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
argv in a C program.


In C, what are the strings in argv[][] in main(int argc, *argv[]). Are they null terminated strings? Are they terminated by a line terminator?

Of course argv could be any other name.

Last edited by stf92; 03-11-2015 at 06:24 PM.
 
Old 03-11-2015, 06:32 PM   #2
maciuszek
Member
 
Registered: Nov 2010
Location: Toronto, Ontario
Distribution: Slackware + FreeBSD
Posts: 165

Rep: Reputation: 40
Edit: deleted
Didn't read it through

Last edited by maciuszek; 03-11-2015 at 06:34 PM.
 
Old 03-11-2015, 06:41 PM   #3
maciuszek
Member
 
Registered: Nov 2010
Location: Toronto, Ontario
Distribution: Slackware + FreeBSD
Posts: 165

Rep: Reputation: 40
Yeah they're terminated with a \0 all "strings" in the second dimension.

The first dimension I'm not sure of I haven't ever not based it on the count I don't think.. Although 0 will return the program call .. Hrm look how execl works for a better understanding . I would assume the final index to be a null pointer in the first dimension of the vector.. But not sure.
 
Old 03-11-2015, 06:45 PM   #4
maciuszek
Member
 
Registered: Nov 2010
Location: Toronto, Ontario
Distribution: Slackware + FreeBSD
Posts: 165

Rep: Reputation: 40
I was right:
Quote:
The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
Edit: also I advocate the use of stdin over such arguments.. Where it I sent regressive or incredibly impraticle to do so

Last edited by maciuszek; 03-11-2015 at 06:49 PM.
 
Old 03-11-2015, 08:22 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by maciuszek View Post
I was right:
Quote:
The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
[pedantic-mode]That just says how those members of the exec() family are called. It says nothing about what is actually passed to the invoked program.[/pedantic-mode]

The C Language Standard does state in section 5.1.2.2.1, "argv[argc] shall be a null pointer".
 
2 members found this post helpful.
Old 03-11-2015, 09:19 PM   #6
perbh
Member
 
Registered: May 2008
Location: Republic of Texas
Posts: 393

Rep: Reputation: 81
argv is kind-of doubly defined, both by argc and the final NULL-pointer.
You can loop through the arguments in two ways:
Code:
int i = 1;
char *s;
while (1) {
  s = argv[i++];
  if (s == NULL) break;
  /* ... */
}
Code:
int i;
char *s;
for (i=1; i<argc; i++) {
  s = argv[i];
  /* ... */
}
Note that a 'string' in c is always zero-terminated. You can easily have a byte-string with zero's in it, but then you also have to carry along a length-entity, and the usual c-library string-functions are no longer valid (well, not invalid but inappropriate)

Last edited by perbh; 03-11-2015 at 09:27 PM.
 
Old 03-11-2015, 10:35 PM   #7
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by perbh View Post
Code:
int i = 1;
char *s;
while (1) {
  s = argv[i++];
  if (s == NULL) break;
  /* ... */
}
Or more succinctly:
Code:
while (*(++argv)) {
    printf ("%s\n", *argv);
}
 
Old 03-12-2015, 12:19 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
In C, what are the strings in argv[][] in main(int argc, *argv[]). Are they null terminated strings? Are they terminated by a line terminator?
Yes. No (if you mean \n).
 
Old 03-12-2015, 04:30 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Why do you worry?

Inside your programme, you are dealing with \0 terminated strings like any other string.

I mean when you type it in at the command line, it is a series of strings (quoted if there are intermediate spaces) with each argument terminated by a space.

OK
 
Old 03-12-2015, 06:38 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by NevemTeve View Post
Yes. No (if you mean \n).
Unless the string was something like "a line terminator\n"...

for the OP:

But it does have to be explicitly stated. By default command parameters do not have newlines.

However, consider something like the awk parameters:
Code:
$  awk '
> BEGIN {count = 25;}
> {count++;}
> END {print count;}
> '
1
2
ctrl-d
27
$
The first parameter to awk does have a "\n" at the end...

The point is that argv[] are null terminated strings - which may include any valid character (even UTF-8 characters).

Last edited by jpollard; 03-12-2015 at 06:46 AM.
 
Old 03-16-2015, 12:33 PM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by stf92 View Post
In C, what are the strings in argv[][] in main(int argc, *argv[]). Are they null terminated strings? Are they terminated by a line terminator?

Of course argv could be any other name.
Do all the varieties of more complex answers solve your question?

Why did you ask? Are you looking to write a program which uses command line arguments and wish to ensure that you get things correct? There are a few ways. Firstly argc tells you how lengthy the argv array is, or should be. And further you can use library functions like the getopt(3) to assist you. I've done both and depending on how I want the invocation line to look, I either parse the arguments manually or choose to use getopt(), and honestly as good as getopt() is, I always have to look it up, thus wasting a bit of time. Well ... at least I've managed to save an example or two over the years so I know what works and what doesn't.
 
Old 03-17-2015, 07:47 AM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I did printf("%s", argv[1]) and it printed argv[1]. I saw then that argv[n] is a null terminated string for any n. Thanks also for the replies, which were very helpful.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming linux copying argv to wchar_t argv babbab Programming 1 08-09-2012 05:32 AM
[SOLVED] ARGV[0] number or letters? & ranning script after finish previous program. DoME69 Programming 21 03-22-2010 07:21 PM
C cidr argv quantt Programming 11 01-26-2009 07:03 AM
Question about argv[] ghoughto Programming 6 10-25-2004 09:38 AM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration