Skip to main content

iDev C: Command-line arguments in the C language

Command-line arguments in the C language by R.C. Maher 
Image
The C language provides a method to pass parameters to the main() function. This is typically accomplished by specifying arguments on the operating system command line (console). 
The prototype for main() looks like: 
int main(int argc, char *argv[]) { 
// Code .....
 } 
There are two parameters passed to main(). The first parameter is the number of items on the command line (int argc). Each argument on the command line is separated by one or more spaces, and the operating system places each argument directly into its own null-terminated string. The second parameter passed to main() is an array of pointers to the character strings containing each argument (char *argv[]). 
For example, at the command prompt: 
test_prog 1 apple orange 4096.0 
There are 5 items on the command line, so the operating system will set argc=5 . The parameter argv is a pointer to an array of pointers to strings of characters, such that: 
  1. argv[0] is a pointer to the string “test_prog” 
  2. argv[1] is a pointer to the string “1” 
  3. argv[2] is a pointer to the string “apple” 
  4. argv[3] is a pointer to the string “orange” and
  5. argv[4] is a pointer to the string “4096.0”
Notes 
ImageImage
The main() routine can check argc to see how many arguments the user specified. 
The minimum count for argc is 1: the command line just contained the name of the invoked program with no arguments. 
The program can find out its own name as it was invoked: it is stored in the argv[0] string! Some operating systems don't provide this feature, however. 
The arguments from the command line are not automatically converted: the characters are just copied into the argv strings.
If an argument on the command line is to be interpreted as a numerical constant, such as argv[1] and argv[4] in this example, it can be converted using a string conversion. 
int int_val; float float_val; 
sscanf(argv[1],”%d”,&int_val); 
sscanf(argv[4],”%f”,&float_val); 
and  
printf(“The 3rd and 4th items on the command line are %s and %s\n”, argv[2], argv[3]); 
results in: 
The 3rd and 4th items on the command line are apple and orange 
The string functions atoi(), atol(), atof(), etc., will also work. 
Thanks 
Keep Coding :)

Comments

Popular posts from this blog

WPF-MVVM: RelayCommand Implementation

In WPF if we are implementing MVVM pattern then we need to play with Command rather than Events. You can use ICommand interface to create each command class. Implementation of ICommand in a class gives you CanExecute(), Execute() methods which take part in the action performed by Command.   Rather than making Command Class for each Command we can implement a generic Relay Command to get Command. Below is a RelayCommand class that we will implement.   ///   <summary>      ///  To register commands in MMVM pattern      ///   </summary>      class   RelayCommands  :  ICommand     {          readonly   Action < object > _execute;          readonly   Predicate < object > _canExecute;  ...

Install node.js using NVM and Homebrew on Mac OS X

Reference:http://dev.topheman.com/install-nvm-with-homebrew-to-use-multiple-versions-of-node-and-iojs-easily/ Less than a month ago,  iojs  was released (multiple releases followed) and 6 days ago, the v0.12.0 of  node  was released. I still had the same v0.10.x (can’t remember the patch 🙂 ) of node on my computer I installed a few months ago … As a nodejs developer, I decided it was time  to get rid of my old version  and switch to  nvm  so that I could test my projects (websites and node modules) on different engines and versions – moreover not to be stuck in the case some module should only work on one or an other … This post is more a reminder for future me when I’ll make the install again, though it could help some people. First, you’ll need  Homebrew . If you’re a MacPorts user (or a Linux user), I assume it’s nearly the same, you may even have your own way which is faster and better, no need to troll 😉 – for Windows us...

Foursquare Introduces ‘Super-Specific’ Search And Filter Options For iOS And Android To Help You Find New Venues

As  Foursquare evolves , it wants to help you find either new places to check out or lead you to places where your friends have already been. Mixed in with that is recommendation technology to show you places that you might be interested in based on where you’ve been before. Today, Foursquare updated its  iOS  and  Android  apps with an  advanced search option that lets you control how the service seeks out new venues  for you. In its blog post today, Foursquare “dares” you to get “super specific” with your searches. Basically, the company is saying that they have enough data to find any place that you could imagine. One of the example searches is: “A cheap sushi place that’s nearby and open now, but that I haven’t been to yet.” Again, this is a search performed based on all of the data that Foursquare has collected over the years, but its first move into a more conversational search experience. Companies like Google are jumping on this bandwago...