• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

UNIX C programming variable manipulation

UNIX korn shell
I have a Korn shell program I am writing where I would like to pass a variable
number to a C program.  This will get passed as a char variable but I would like
to convert it to a long

This is my call to the C program from Korn


This will get passed into the main via:

int GETCUSTIDAPP(int argc, char **argv)

Where I set the variable passed in to a variable called

char custId[10]

Now, I need to pass in this custId to another function Get_Address and convert it to a long I'm having trouble with it understanding how to do it:

Here is what I have:
int Get_Address(char* customer) --> is this the right way to pass it in?
Also how do I convert this value to a long within the function?  I need to use this value in a sql call as a number later in the code.

I hope this made some sense.
  • 2
  • 2
1 Solution
evilrixSenior Software Engineer (Avast)Commented:
Use scanf to convert from a char[] to a long.
Something like below...

long nCustId = 0;
scanf("%ld", custId);

Open in new window

if (argc < 2)
 fprintf(stderr, "Need parameter");

strncpy(custId, argv[1], 10);
custId[9] = 0; //good habit

long num = strtol(custId, NULL, 10);
evilrixSenior Software Engineer (Avast)Commented:
Sorry, my head is somewhere else... ignore above! I'll try again!

Use sscanf to convert from a char[] to a long.
Something like below...

long nCustId = 0;
sscanf(custId, "%ld", &nCustId);

Open in new window

There are several ways to convert strings to numbers, I'd prefer 'strtol()' because of the error checking. You can also use 'atoi()', but it has almost no error control. See also http://cplusplus.com/reference/clibrary/cstdlib/strtol.html and http://cplusplus.com/reference/clibrary/cstdlib/atoi.html
#include <stdio.h>
#include <stdlib.h>
int Get_Address(char* customer);
int main(int argc, char **argv) {
    char custId[10];
    if (argc != 2) {
        printf("Exactly one parameter expected\n");
        return -1;
    return 0;
int Get_Address(char* customer) {
    long l;
    char* cnv_end;
    l = strtol(customer,&cnv_end,10);
    if (*cnv_end != '\0') { // error - conversion found non-numeric characters
        printf("Error - not a number\n");
        return -1;
    printf("ID: %d\n", l);
    return 42;

Open in new window

> strcpy(custId,argv[1]);
is vulnerable to buffer overflow easily.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now