Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


Search this Thread
Old 06-10-2006, 09:19 PM   #1
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, 09:27 PM   #2
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.
Old 06-10-2006, 11:06 PM   #3
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
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.


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

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Here's a simple example:
#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-11-2006, 12:12 AM   #5
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, 09:22 AM   #6
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


I appreciate the help.


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

All times are GMT -5. The time now is 02:28 PM.

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