473,802 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1924
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*****@hotmai l.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)cybers pace.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*****@hotmai l.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
2996
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 "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.path.exists('Lib/os.py') True # expected
3
2364
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
6
2939
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
10
2599
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 optimization). the codec between /***********/ is the initialization code. What supprise me a lot is the code with initialization(io==1) is much faster than without initialization(io!=1). The initialization code should takes some time and it should...
2
1539
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
23
2146
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
9803
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 data that I get in argv ? Q2. what is encoding of string constants defined in programs (for example L"--count") ?
17
1937
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. However, when I insert the following line at the start of a FOR loop early on the in the programme to do this: printf("\nCommand line arguement %d: %s. \n", i , argv ); I get back the first 3, then I get a segmentation fault followed by
8
3222
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: Whenever I declare the sockaddr_in structure inside the main, the debugger crashes at line X*, not being able to access argv parameters (see code below). It is very strange.. by only being there, sockaddr_in does not allow me to question argc...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.