文件名称:get_env.c
编译指令:gcc -std=c99 get_env.c -o get_env.out
#include <stdlib.h>
#include <stdio.h>
static inline int readenv_atoi(char *env) {
char *p;
//getenv函数是gcc自带的函数,在stdlib.h头文件中声明
if (( p = getenv(env) ))
return (atoi(p));
else
return(0);
}
int main(int argc, char** argv)
{
int numprocs = readenv_atoi("OMP_NUM_THREADS");
printf("numprocs = %d\n",numprocs);
return 0;
}

代码来源: openblas的源代码
本文提供了一个使用C语言从环境中读取整数型变量的示例程序,并展示了如何通过getenv和atoi函数获取和转换环境变量OMP_NUM_THREADS。
433

被折叠的 条评论
为什么被折叠?



