473,776 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Argument Processing


Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.
Nov 14 '05 #1
23 2181
On Fri, 05 Nov 2004 05:41:31 GMT, Daniel Rudy
<i0************ *************** *******@n0o1p2a 3c4b5e6l7l8s9p0 a1m2.3n4e5t6>
wrote in comp.lang.c:

Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
Replace "<=" with "<" in the loop. argv[argv] is a null pointer.
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
Note that 'return' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.
}


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

"Daniel Rudy"
<i0************ *************** *******@n0o1p2a 3c4b5e6l7l8s9p0 a1m2.3n4e5t6>
wrote in message news:fo******** *********@newss vr14.news.prodi gy.com...

Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
What are you passing to main from command line ?
You might try adding this before the first printf.

if ( argc < 3 )
{
printf ("Insufficie nt arguments to main\n");
return 0;
}
- Ravi


--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.

Nov 14 '05 #3
Daniel Rudy wrote:
#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
Rewrite this as
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}


Satyajit
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Nov 14 '05 #4
I think the problem is the declareation of the main function,
should be :
int main(int argc, char* argv[])

also should modify the <= to <

Nov 14 '05 #5
Daniel Rudy wrote:
I'm trying to learn how command line arguments are handled in C.
The following code segment that I wrote as a test program compiles
but when I try to run it, it core dumps.
This is under a FreeBSD environment.
What am I doing wrong here?
[snip]
cat main.c /*
*
* just echos the command line arguments onto the screen
* tests the format of argument processing
*
* */

#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {

printf("argc = %d\n", argc);
for (int i = 0; i < argc; ++i) {
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main arg1 arg2 arg3 argc = 4
argv[0] = ./main
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3 gcc --version gcc (GCC) 3.4.2 uname -o Darwin man 3 getopt

GETOPT(3) BSD Library Functions Manual GETOPT(3)

NAME
getopt - get option character from command line argument list

LIBRARY
Standard C Library (libc, -lc)

SYNOPSIS
#include <unistd.h>

extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;

int
getopt(int argc, char * const *argv, const char *optstring);
Nov 14 '05 #6
Mac
On Fri, 05 Nov 2004 00:01:59 -0600, Jack Klein wrote:
... argv[argv] is a null pointer.


Of course you mean argv[argc] is a null pointer.

--Mac

Nov 14 '05 #7
"Daniel Rudy"
<i0************ *************** *******@n0o1p2a 3c4b5e6l7l8s9p0 a1m2.3n4e5t6>
wrote in message news:fo******** *********@newss vr14.news.prodi gy.com...

Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?
See below.
/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
int main(int argc, char *argv[])

/* or */

int main(int argc, char **argv)
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
for (i = 0; i < argc; i++)

{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}

-Mike
Nov 14 '05 #8
no problem with the declartion it is fine

Nov 14 '05 #9

<su******@yahoo .com> wrote in message
news:10******** ************@c1 3g2000cwb.googl egroups.com...
no problem with the declartion it is fine


Look again. It's not.

-Mike
Nov 14 '05 #10

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

Similar topics

1
1636
by: Olaf Meding | last post by:
What does the below PyChecker warning mean? More importantly, is there a way to suppress it? PyChecker warning: ..\src\phaseid\integration.py:21: self is argument in staticmethod My best guess is that the warning is related to PyChecker not supporting C++ extensions.
1
3650
by: Patrick Rammelt | last post by:
Hi After upgrading my compiler (gcc-3.3.3 to gcc-4.0.0) I stumbled over an error that I do not understand. Maybe it is a compiler bug, but maybe I can get some new insights from this. Here is a small example: ------------------------------------------------------------------ template <class T> class A { public:
10
2992
by: Richard Heathfield | last post by:
I was recently asked whether it is legal to start processing a variable argument list a second time, /before/ you've finished with it the first time. (I have no idea why the questioner might want to do this, I'm afraid. Since he's normally fairly bright, I presume he had a good reason for asking.) The following code illustrates the point, I think (but don't use it as a va_arg tutorial!, as it kinda assumes there are at least six args...
19
4260
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
6
8844
by: Sunny | last post by:
Hi, I have a very serious issue at hand. I have created a small C# Console App (Csd.exe) that collects a list of files as its argument and generates their Md5 sets. This application is used by other applications in my dept, which simply call this Csd.exe app and passing a list of files. I want to ask how can I return a value from this Csd.exe application as an indicator or a signal to the calling app that Csd.exe has finished working....
4
4650
by: MattBell | last post by:
I've tried to search for an answer to this without much success, and I think it's probably a common thing to do: I have a web service I want to accept an XmlDocument as an argument which conforms to a specific XSD that is defined. Right now when I declare XmlDocument as my argument, it puts the old xml:any type in. How do I change that to reflect the XSD that I'm looking for? Thanks for any Help!
3
3407
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is to be supplied to the exception constructor, the default constructor should
2
2177
by: Nathan Sokalski | last post by:
I have a DataList in which the ItemTemplate contains two Button controls that use EventBubbling. When I click either of them I receive the following error: Server Error in '/' Application. -------------------------------------------------------------------------------- Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/in configuration or <%@ Page EnableEventValidation="true"...
4
3295
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of arguments. For example, #define MAX_SIZE 100000 int foo(double *x, unsigned ling sz, int option) {
0
10289
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
10120
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
10061
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
8952
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
7471
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
6722
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
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.