How do I input an argument in the main?


 
Thread Tools Search this Thread
Top Forums Programming How do I input an argument in the main?
# 1  
Old 06-10-2002
How do I input an argument in the main?

----------C program-----------------------------
include <stdio.h>

int main( int argc, char *argv[] )
{
int i;
for( i=0; i<argc; i++ )
printf("%\n", argv[i]);
return 0;
}
I wrote the C program above 'print.c'.
Then, I compiled. (gcc -o print.o print.c)

In unix, I've tried to execute it. (./print.o)

How do I input an argument in the main?
# 2  
Old 06-10-2002
Don't call the output print.o. print.o would be the default name for an object module. You would probably call the linked executable "print". At least that's a better choice than print.o. But "print" is also the name of a ksh built-in...

Anyway to run it with args just do...

./print arg1 arg2 arg3

And finally your format string in the printf call needs a little work. It's %s to print a string.
# 3  
Old 06-11-2002
Try the following program. Put it in a file called "howdy.c".

#include <stdio.h>

int main( int argc, char *argv[] )
{
if (!argc)
{
printf("Missing input argument!\n");
printf("Usage: howdy %1\n");
return 1;
}

printf("Well howdy there %s! YeeHaw!!!\n", argv[1]);
return 0;
}

To do a simple compile, type "gcc howdy.c -o howdy"
After it's compiled (assuming you have no errors), give it a run similiar to the following:

./howdy DUDE

(But try substituting something other than dude...)

Thanks!
---> Kelsey
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to iterate a function untill last argument with any order of input?

HI I need to get the function "kick" to get executed in any way the parameters are passed in to the function. The parameters are first stored in a dictionary self.otherlist = {} print self.otherlist self.populateTestList(self.system_type) print... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

2. Shell Programming and Scripting

Check for spaces in input argument!

Hi guys, I have created a csh script which allows user to pass input argument with the script like: cluster_on_lev3.csh -t <value> -p <value> Example: cluster_on_lev3.csh -t 2.3 -p 0.05 Now I want to create an error code where if user passes input argument without spaces , the code... (16 Replies)
Discussion started by: dixits
16 Replies

3. Shell Programming and Scripting

Utilize input file in place of argument

First I apologize for my ignorance as I am very new to the world of UNIX but love it and have a huge desire to learn it. A question I have is if a Korn script utilizes/relies on an argument to run, can you add these into a file and pipe them to the script without changing anything inside the... (2 Replies)
Discussion started by: djzah
2 Replies

4. Shell Programming and Scripting

Renice command with input argument as a file

Hi all could you help me how to give pids in a file as an argument to renice command. Thanks in Advance.. :) Pradeep (4 Replies)
Discussion started by: nanz143
4 Replies

5. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

6. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

7. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

8. Shell Programming and Scripting

use input filename as an argument to name output file

I know this is a simple matter, but I'm new to this. I have a shell script that calls a sed script from within it. I want the output of the shell script to be based on the input file I pass as an argument to the original script. In other words... ./script.sh file.txt (script.sh calls sed... (2 Replies)
Discussion started by: estebandido
2 Replies

9. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

10. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies
Login or Register to Ask a Question