Easy: Passing data to c program from terminal (Mac)

13,502

Solution 1

It sounds like you are looking for argv, which I suppose is difficult to search for if you don't know what it is called! This isn't specific to Mac OS X's Terminal.

The argv argument of a main() function is an array of strings; its elements are the individual command line argument strings.

The path to the program being run is the first element of argv, that is argv[0].

The number of elements in argv is stored in argc:

#include <stdio.h>

int main(int argc, char* argv[])
{
    int arg;
    for (arg = 0; arg < argc; ++arg)
    {
        printf("Arg %d is %s\n", arg, argv[arg]);
    }
    return 0;
}

Compile:

% gcc program_name.c -o program_name

Run:

% ./program_name 19982
Arg 0 is ./program_name
Arg 1 is 19982

Converting argv[1] to an int is left as an exercise.

Solution 2

You can use argc and argv to access program's arguments. argc is the "arguments count" - the number of arguments passed. argv is the "arguments vector", where the first member is the name of the program.

#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] )
{
    int Address;
    if (argc > 1)
    {
        Address = atoi(argv[1]);
    }
    else 
    {
        printf("No arguments passed\n");
        return 1;
    }
    return 0;
}

Solution 3

Typically, you'd use "argv/argc" in main. For example:

#include<stdio.h>

int
main (int argc, char *argv[])
{
  if (argc < 2)
    printf ("You didn't enter any arguments\n");
  else
    printf ("Your first argument is %s\n", argv[1]);
  return 0;
}

Under Linux, you'd compile and run like this:

gcc -o hello hello.c
./hello howdy!

Again under Linux, it would output something like this:

Your first argument is howdy!

Solution 4

All C (and C++, don't know about objective-c) programs start their execution in the function main. This function takes two arguments: An integer, usually named argc which is a counter of the number of arguments given to the program; The second function argument is an array of char pointers, usually called argv and is the actual command line arguments.

The first entry in argv is always the name of the command itself, which means that argc will always be at least 1.

The following program prints all arguments given on the command line:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Total number of values in argv: %d\n", argc);
    for (int a = 0; a < argc; a++)
        printf("argv[%02d]: %s\n", a, argv[a]);
}
Share:
13,502

Related videos on Youtube

1991DBA
Author by

1991DBA

Oracle DBA and aspiring EBS aficionado

Updated on May 25, 2022

Comments

  • 1991DBA
    1991DBA almost 2 years

    So, this is a really basic question. For an assignment we had to write a c program that would calculate the page and offset number of a virtual address. My program seems to work fine when I make a vocal variable of the virtual address that we are supposed to do calculations on, but I can't figure out how to pass it.

    The assignment says that we should run our program like this

    ./program_name 19982
    

    I just can't figure out how to pass that 19982 in terminal on my mac. Any help is appreciated. (And in before someone makes a mac joke.)

    • Tachikoma
      Tachikoma over 12 years
      Do you want to know, how to make your program take the number 19982 and use it, instead of creating hard-coded variable?
    • 1991DBA
      1991DBA over 12 years
      Yes that is exactly it. My input into terminal is gcc myprog.c to compile then /a.out to run but i don't know where to put the 19982 in that equation. I have my main already set up to accept an argument, but I just don't know how to pass it in terminal.
  • John Carter
    John Carter over 12 years
    Errr ... missing a string to int conversion?