开发者

Find out constant value by given string in c

开发者 https://www.devze.com 2023-03-14 09:20 出处:网络
Need to check Is the given string constant value (int/long/double) or not? Here strtol function is used to find constant value. 开发者_如何转开发But facing problem

Need to check Is the given string constant value (int/long/double) or not?

Here strtol function is used to find constant value. 开发者_如何转开发But facing problem

Example :

   if(li1 = strtol (str,NULL,0))
       printf("valid Integer ...");
  1. str = "1" output = 1.00
  2. str = "0.99999" output = 0.00
  3. str = "tab" output = 0.00

Then how to differentiate between "0.99999" and "tab" by looking at output?


For integers, strtol provides the second argument which will be set to point to the first unconvertible character.

If it's anything other the the null terminator \0, then there was rubbish at the end of the number. If it's equal to the original string, then no suitable characters were found.

Example:

char *str = "72";

char *estr;
float val = strtol (str, &estr, 10);
if (estr == str) {
    // there was no convertible characters.
}
if (*estr != '\0') {
    // there was rubbish at the end.
}
if (errno != 0) {
    // underflow/overflow.
}

For floating point, you need to use one of the strtoX functions.

It acts very similar to the strtol function.

Example usage:

char *str = "0.9999";

char *estr;
float val = strtof (str, &estr);
if (estr == str) {
    // there was no convertible characters.
}
if (*estr != '\0') {
    // there was rubbish at the end.
}
if (errno != 0) {
    // underflow/overflow.
}

A function to work out what type a string represented is shown in the complete program below:

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#define TYP_INTEGRAL 0
#define TYP_FLOATING 1
#define TYP_BAD      2
int getTypeAndData (char *str, long *valL, float *valF) {
    char *end;

    *valL = strtol (str, &end, 10);
    if ((end != str) && (*end == '\0'))
        return TYP_INTEGRAL;

    *valF = strtof (str, &end);
    if ((end != str) && (*end == '\0'))
        return TYP_FLOATING;

    return TYP_BAD;
}

int main (int argc, char *argv[]) {
    char *desc[] = {"INT", "FLT", "BAD"};
    int i, typ;
    long lvar;
    float fvar;
    for (i = 1; i < argc; i++) {
        lvar = 0; fvar = 0;
        typ = getTypeAndData (argv[i], &lvar, &fvar);
        printf ("%s: [%-10s] %10ld %10.3f\n", desc[typ], argv[i], lvar, fvar);
    }
    return 0;
}

When run with myprog 12345 hello 12.7 1e2 0.4 .1 "" 0, the output is:

INT: [12345     ]      12345      0.000
BAD: [hello     ]          0      0.000
FLT: [12.7      ]         12     12.700
FLT: [1e2       ]          1    100.000
FLT: [0.4       ]          0      0.400
FLT: [.1        ]          0      0.100
BAD: [          ]          0      0.000
INT: [0         ]          0      0.000

and you can see it's detecting at least the unit test cases I could come up with quickly.

Note that this doesn't directly communicate underflow and overflow conditions. The default values are returned in those cases since they are usually the sensible options, but you can check errno after returning if you want to catch those conditions.

0

精彩评论

暂无评论...
验证码 换一张
取 消