473,383 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Strange ARGC/ARGV Behaviour

Hi

I am by some people's standards a newbie to C, and I
am refreshing my memory as to some of its conventions. I would
like any patient soul out there to help out.

I am having a horrible time figuring out argc/argv in a C
program I am writing. I need a program to extract integers and
print the ASCII representations of them. Here is the source of
this very short program under Linux:

#include <stdio.h>
#include <strings.h>

int main (int argc, char *argv[]) {
/* this only takes a number as an argument */
int num = 0;
int count = argc;
for (; (count > 0); count--) {
num = strtol(argv[count]);
printf ("%d\t", count);
printf ("%d\t'%c'\n", num, num);
}
return 0;
}

Here is the command line with the parameters:

$ ascii 97 98 99 100 101

Here is the output of the program:

6 0 ''
5 101 'e'
4 0 ''
3 99 'c'
2 0 ''
1 97 'a'

So, there is a "magic" sixth parameter that appears out of
nowhere. In addition, only every second ascii character is
interpreted. What is interesting is that I get a segfault if I
substitute the two printf's with the line:

printf ("%d\t%d\t'%c'\n", count, num, num);

Paul King

Nov 15 '05 #1
4 1904
Your count should start from argc - 1 . i.e 5 in this case.

Just so u r clear ...

argc[0] == ascii
argc[1] == 97
....
....
argc[5] == 101

---cheers!

Xog Blog wrote:
Hi

I am by some people's standards a newbie to C, and I
am refreshing my memory as to some of its conventions. I would
like any patient soul out there to help out.

I am having a horrible time figuring out argc/argv in a C
program I am writing. I need a program to extract integers and
print the ASCII representations of them. Here is the source of
this very short program under Linux:

#include <stdio.h>
#include <strings.h>

int main (int argc, char *argv[]) {
/* this only takes a number as an argument */
int num = 0;
int count = argc;
for (; (count > 0); count--) {
num = strtol(argv[count]);
printf ("%d\t", count);
printf ("%d\t'%c'\n", num, num);
}
return 0;
}

Here is the command line with the parameters:

$ ascii 97 98 99 100 101

Here is the output of the program:

6 0 ''
5 101 'e'
4 0 ''
3 99 'c'
2 0 ''
1 97 'a'

So, there is a "magic" sixth parameter that appears out of
nowhere. In addition, only every second ascii character is
interpreted. What is interesting is that I get a segfault if I
substitute the two printf's with the line:

printf ("%d\t%d\t'%c'\n", count, num, num);

Paul King


Nov 15 '05 #2
Xog Blog <xo*****@hotmail.com> wrote:
#include <stdio.h>
#include <strings.h>
This header doesn't exist, nor do you need the one that does exist,
<string.h>. You are missing a header you do need, <stdlib.h>.
int main (int argc, char *argv[]) {
int num = 0;
int count = argc;
Wrong. Arrays in C are zero-indexed; the highest index of argv is
argc-1, as the previous poster pointed out.
for (; (count > 0); count--) {
num = strtol(argv[count]);
Again wrong. strtol takes three arguments; it's a wonder that gcc
(presumably your compiler, given that you are using Linux) accepted
it. It isn't a wonder that your program doesn't work.

<ot>
man strtol and do yourself a favor and invoke gcc thus:

gcc -Wall -ansi -pedantic
</ot>
printf ("%d\t", count);
printf ("%d\t'%c'\n", num, num);
It's a pedant's point, but %c is intended for a character, and you've
passed it an integer.
}
return 0;
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3
On Tue, 20 Sep 2005 01:46:39 +0000, Christopher Benson-Manica wrote:
Xog Blog <xo*****@hotmail.com> wrote:


....
printf ("%d\t", count);
printf ("%d\t'%c'\n", num, num);


It's a pedant's point, but %c is intended for a character, and you've
passed it an integer.


It outputs the argumnt as a character. However %c is specified as taking
an int argument. This isn't suprising as a char argument given in a
variable argument list will be promoted to int (or rarely unsigned
int) before being passed. int can be a reasonable type to hold a character
value, e.g. the return type of getc() and argument type of the various
functions in <ctype.h> is int.

Lawrence
Nov 15 '05 #4
Xog Blog wrote on 20/09/05 :
I am by some people's standards a newbie to C, and I
am refreshing my memory as to some of its conventions. I would
like any patient soul out there to help out.

I am having a horrible time figuring out argc/argv in a C
program I am writing. I need a program to extract integers and
print the ASCII representations of them. Here is the source of
this very short program under Linux:

#include <stdio.h>
#include <strings.h>
not standard. You meant

#include <string.h>

but it is useless here.
int main (int argc, char *argv[]) {
/* this only takes a number as an argument */
int num = 0;
int count = argc;
for (; (count > 0); count--) { num = strtol(argv[count]);
Missing <stdlib.h>
Missing parameters.
argv[argc] = NULL by definition.
The decreasing loop is tricky. Dou you really want to start from the
end ?
printf ("%d\t", count);
printf ("%d\t'%c'\n", num, num);
}
return 0;
}

Here is the command line with the parameters:

$ ascii 97 98 99 100 101

Here is the output of the program:

6 0 ''
5 101 'e'
4 0 ''
3 99 'c'
2 0 ''
1 97 'a'

So, there is a "magic" sixth parameter that appears out of
nowhere. In addition, only every second ascii character is
interpreted. What is interesting is that I get a segfault if I
substitute the two printf's with the line:

printf ("%d\t%d\t'%c'\n", count, num, num);


#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
/* this only takes a number as an argument */
int num = 0;
int count = argc - 1;

for (; (count > 0); count--)
{
num = strtol (argv[count], NULL, 10);
printf ("%d\t", count);
printf ("%d\t'%c'\n", num, num);
}
return 0;
}

5 101 'e'
4 100 'd'
3 99 'c'
2 98 'b'
1 97 'a'

No magic !

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pierre Quentel | last post by:
os.path.exists(path) returns True if "path" exists But on Windows it also returns True for "path" followed by any number of dots : Python 2.4 (#60, Nov 30 2004, 11:49:19) on win32 Type...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
23
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
17
by: Matt | last post by:
Hello. I've got a very strange problem. Basically I have a programme where I wish to view all the strings in the argv array so I can see what arguments are being passed to the programme. ...
8
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.