Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the atoi() function to convert a C string into an integer at compile time. The atoi() function takes a C string as input and returns the corresponding integer value. You can then assign this integer value to a variable of type int. Here is an example:

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

int main() {
    char str[] = "123";
    int num;
    num = atoi(str);
    printf("The integer value of '%s' is %d\n", str, num);
    return 0;
}

This program declares a C string str that contains the value "123". It then declares an integer variable num and assigns to it the integer value of str using the atoi() function. Finally, it prints out the value of str and num using the printf() function.

The output of this program will be:

The integer value of '123' is 123

Note that the atoi() function only works for C strings that contain valid integer values. If the C string does not contain a valid integer value, the behavior of atoi() is undefined.