LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-10-2006, 08:19 PM   #1
YankeeFan
LQ Newbie
 
Registered: Jun 2006
Posts: 21

Rep: Reputation: 15
C command line args in linux


I'm obviously new to linux, but having fun.
I actually have a couple of questions.

1) am I missing a search function on this site? I'd like to search for an answer before spamming everyone with a repeated question.

2) Not sure this goes here, but seems like the best place for me.

I'm re-aquainting myself to c and c++. I'm starting with the basics and going forward. I'm having a bug sending command line arguments to my program. I'm confident the code is okay, it is how to call the program from the command line I'm not sure of. The book I'm using shows it as I do it below.

The name of my program is argsc after compiling okay. I call it like so:

John$ ./argsc Hello World

I get a segmentation fault error. I've tried quoting the strings to no avail.
 
Old 06-10-2006, 08:27 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You probably didn't allocate space for the arguments before copying them to a target. Without the source, there is no way for me to tell if this is what you did.

I would highly recommend a book, Linux Programming by Example.
http://www.amazon.com/gp/product/013...lance&n=283155
 
Old 06-10-2006, 10:06 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by YankeeFan
I'm obviously new to linux, but having fun.
I actually have a couple of questions.

1) am I missing a search function on this site? I'd like to search for an answer before spamming everyone with a repeated question.
I'd suggest looking again... there's a search-link in the middle of
the menu at the top, and a search link in the top-field of the
menu on the right.


Cheers,
Tink

Last edited by Tinkster; 06-10-2006 at 10:12 PM.
 
Old 06-10-2006, 10:49 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Here's a simple example:
Code:
#include <stdio.h>

int main( int argc, char *argv[] )
{
  int arg_counter;

  printf( "Executed program name: %s\n", argv[0] );

  for( arg_counter = 1; arg_counter < argc; arg_counter++ )
    printf( "argument #%d:\t%s\n", arg_counter, argv[arg_counter] );

  return 0;
}
 
Old 06-10-2006, 11:12 PM   #5
LzW-x
Member
 
Registered: Jun 2006
Distribution: SuSe
Posts: 66

Rep: Reputation: 15
is "hello world" the name of the file?

or is world the command arguement?
 
Old 06-11-2006, 08:22 AM   #6
YankeeFan
LQ Newbie
 
Registered: Jun 2006
Posts: 21

Original Poster
Rep: Reputation: 15
C command line args

RE: Search. Yes, I found it about 1 minute after my post. I couldn't find it because it was right in front of me. Sigh!

My code:
=====================================
#include <stdio.h>

main(int argc, char *agrv){
int i;

printf("The value of argc is %d", argc);
printf("The first command-line arg is: %s\n", argv[0]);
for( i=1; i<argc; i++ ){
printf("\n %s", argv[i]);
}
return 0;
}
======================================

I compiled it with gcc -o argsc argsc.c
This gives me argsc


The online tutorial I was looking at says all I need to do in my terminal window is enter the name of my program and my arguments. For my example, it only prints what i enter at the command line - hopefully.

I enter:
John$ ./argsc Hello World

I think I should see:
argsc Hello World

Thoughts??

I appreciate the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
processing command args in bash slzckboy Programming 10 09-28-2005 04:49 PM
command line args in a countdown Squeak2704 Programming 1 04-26-2004 12:27 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
>>> control mplayer with command args??? nibjb Linux - Software 0 07-16-2003 11:09 PM
Command Line Linux Fusion07 Linux - Newbie 6 07-03-2002 12:47 AM


All times are GMT -5. The time now is 03:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration