You can do it with strtol, like this:

char *str = "ab234cid*(s349*(20kd", *p = str;
while (*p) { // While there are more characters to process...
    if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
        // Found a number
        long val = strtol(p, &p, 10); // Read number
        printf("%ld\n", val); // and print it.
    } else {
        // Otherwise, move on to the next character.
        p++;
    }
}

Link to ideone.

Answer from Sergey Kalinichenko on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-I-extract-an-integer-from-the-string-in-C
How to extract an integer from the string in C - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Discussions

Extract number from a string C - Stack Overflow
I have a bunch of strings structured like this one. I want to extract them, put them into an array or int. For example: DATA:PHONENUMBER123456AGE7890 TEL:123 TEL1A:123456 TEL2B:123456 I need to ex... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 19, 2017
Extract numbers from a character string in C - Stack Overflow
Please can you tell me what is wrong in the program I've written? I'm trying to create a new string with the numbers found in a string entered by the user. For example: "Enter a string: More on stackoverflow.com
๐ŸŒ stackoverflow.com
C: extract numbers from a string - Stack Overflow
I have a bunch of strings structured like this one Trim(2714,8256)++Trim(10056,26448)++Trim(28248,49165) and what I want to do is to save all the numbers into an array (for the sake of this answer... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to extract a number from a string in C - Stack Overflow
I am working on a project for school but I can't figure out how I can extract the year from a date in a string "20-02-2015" the date is always of the form XX-XX-XXXX Is there some way to use some ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 2, 2014
Top answer
1 of 2
1

There are many ways to parse a string containing numbers. If you expect the string to have a fixed format with 2 integers, the simplest solution is to use sscanf():

#include <stdio.h>

int parse2numbers(const char *str) {
    int a, b;
    // sscanf returns the number of successful conversions
    int n = sscanf(str, "%d%d", &a, &b);

    if (n == 2) {
        printf("success: a=%d, b=%d\n", a, b);
        return 1;
    }
    if (n == 1) {
        printf("failure: only one number provided: a=%d, str=%s\n", a, str);
        return 0;
    }
    if (n == 0) {
        printf("failure: invalid format: %s\n", str);
        return 1;
    }
    printf("failure: encoding error: n=%d, str=%s\n", n, str);
    return 0;
}

If the string can contain a variable number of integers, you could use strtol() to parse one integer at a time:

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

void parse_numbers(const char *str) {
    long a;
    char *p;
    
    for (;; str = p) {
        errno = 0;
        // strtol returns a long int
        //        updates `p` to point after the number in the source string
        //        sets errno in case of overflow and returns the closest long int
        a = strtol(str, &p, 10);
        if (p == str)
            break;
        if (errno != 0) {
            printf("overflow detected: ");
        }
        printf("got %ld\n", a);
    }
    if (*str) {
        printf("extra characters: |%s|\n", str);
    }
}
2 of 2
-1

I have not tested it but I think theoretically this should work:

int rows=2, columns=4 // defining length of array
char ch[rows] [columns] = {"1 90"}, {"2 90"}; // creating two dimensional array for sample data
for (int i = 0; i < rows; i++) { // looping throw first dimention

// this only works if data is sorted and there is no missing indexes in between like [1 200] [3 200] will not work but [1 200] [2 200] should
    char* index;
    if ( ch[i] [0] != itoa(i+1, index, 10) ) // checking if index does not match the row then skip this iteration and move to next one.
        continue; 
    for ( int j = i+2; j<columns; j++) { // looping through second dimension
        printf("%c\n", ch[i][j]); // printing that second dimension
    }
}
Find elsewhere
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 156405
Extracting Numbers from a C-String - C++ Forum
The c-string consist of: {'9', '1', '8', '0', '0', 'w', '9', '4', '0', '7', '7', '0', '\0'}. I'm trying (unsuccessfully so far) to pull out the part after the 'w'. Specifically, just the '94' and the '0770', but I don't know of a function or way to do this. Any ideas or suggestions. Below is the code I have so far and you can kind of see what I'm attempting. ... Topic archived. No new replies allowed. Home page | Privacy policy ยฉ cplusplus.com, 2000-2025 - All rights reserved - v3.3.3 Spotted an error?
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Pulling Numbers from a String | C For Dummies Blog
February 1, 2026 - The sample output from my solution reflects this condition: ... Remember, the extract() function doesnโ€™t know where a number exists within the string. Its job is to return the address of the first digit found, then to be called again and again to find subsequent digits.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ code-snippets โ€บ c-program-to-get-integer-number-from-string-using-sscanf.aspx
C program to get integer (number) from string using sscanf
November 2, 2016 - sscanf is used to read formatted data from a string, here we can get any kind of value by specifying its type (through format specifier) from a string. ... In this example we are reading a string (character array) though keyboard and will extract integer value; then store the value in an integer ...
๐ŸŒ
Quora
quora.com โ€บ How-can-you-extract-only-numbers-from-a-string-using-a-function-in-C-C-development
How to extract only numbers from a string using a function in C (C, development) - Quora
Answer (1 of 2): Question: How can you extract only numbers from a string using a function in C (C, development)? There are no functions of which Iโ€™m aware with just standard C. With a specific library like Windows API or a Linux kernel library, or otherwise - you may be able to parse by ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ extract-all-integers-from-a-given-string
Extract all integers from a given String - GeeksforGeeks
May 24, 2026 - The idea is to use regular expressions to directly find all continuous sequences of digits present in the string. The regex pattern \\d+ matches one or more consecutive digit characters.
๐ŸŒ
Tcl Wiki
wiki.tcl-lang.org โ€บ page โ€บ Extract+Numbers+From+a+String
Extract Numbers From a String
WJG 2022-10-01 PYK 2022-10-09: A quick snippet on extracting a list of numbers from a string without using regular expressions: proc extractNumbers str { set res "" foreach c [split $str ""] { if { [string is integer $c] } { set a 1 append res $c } elseif { $c eq "," || $c eq "." } { if {$a} { append res $c } } else { set a 0 append res " " } } return [string trim $res] }
๐ŸŒ
Quora
quora.com โ€บ How-do-I-extract-a-number-from-a-string-in-C
How to extract a number from a string in C - Quora
Answer (1 of 3): //This is the code to extract no. from string.... #include #include #include
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Pulling Numbers from a String โ€“ Solution | C For Dummies Blog
February 8, 2026 - #include <stdio.h> #include <stdlib.h> #include <ctype.h> /* return the first location of a number in string s */ char *extract(char *s) { static char *sp = NULL; /* check for recall */ if( s != NULL ) sp = s; else { /* guard against a NULL string passed the first time */ if (sp==NULL) return(NULL); /* find the next non-digit */ while( isdigit(*sp) ) sp++; } /* find the next digit */ while( *sp != '\0' ) { if( isdigit(*sp) ) { return(sp); } sp++; } return(NULL); } int main() { char sample[] = "abc10=13!260;1m"; char *r; int v; r = extract(sample); if( r != NULL ) { v = atoi(r); printf("%d\n",v); while( (r=extract(NULL)) ) { v = atoi(r); printf("%d\n",v); } } return 0; }