<br>#include <stdio.h>
#include <stdlib.h>
 
int myarray[50000]={1,2,3,4,5};
 
int main(int argc, char const *argv[])
{
         
    return 0;
}

 

#include <stdio.h>
#include <stdlib.h>
 
int myarray[50000];
 
int main(int argc, char const *argv[])
{
    myarray[0]=3;
    return 0;
}

 如果你用ls -l命令查看上面的2个程序文件的话,你就会发现两个文件的大小差别很大。其中的原因就在于第一个程序是中的myarray是已经初始化的静态数据,是可执行模块的一部分,所以会比版本2的程序大很多。

在声明的时候没有显式的进行初始化的静态变量会在运行的时候被初始化为0,注意在程序的印象中,已经初始化的静态变量和未初始化的静态变量占据不同的区域。通常情况下,已经初始化的静态变量是可执行模块的一部分,而未初始化的静态变量则不是,当然,自动变量不是可执行模块的一部分。只有当定义他们的程序被调用的时候,才会分配内存。

在主存储器中程序印象的布局大致为:

Linux c基础学习

#include <stdio.h>
#include <stdlib.h>
 
extern char ** environ;
 
int main(int argc, char const *argv[])
{
    int i;
    printf("The environment list is as floows:\n");
    for ( i = 0; environ[i]!=NULL; ++i)
        {
            printf("*environ[%d]: %s\n",i,environ[i] );
        }  
    return 0;
}

 上面的程序打印出了环境列表的内容

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char const *argv[])
{
    char* str;
    str=getenv("PATH");
    if(str!=NULL){
        printf("%s\n",str );
    }else{
        printf("doesn't exits PATH \n");
    }
    return 0;
}

 上面的程序显示了如何使用getenv函数

 

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <sys/times.h>
 
static void showtimes()
{
    double ticks;
    struct tms tinfo;
 
    if((ticks=(double)sysconf(_SC_CLK_TCK))== -1){
        perror("Filed to detemine clock ticks per second\n");
    }else if(times(&tinfo)==(clock_t)-1){
        perror("Filed to get times infomation\n");
    }else{
 
        fprintf(stderr, "user time:  %f seconds\n",tinfo.tms_utime/ticks );
        fprintf(stderr, "system time:  %f seconds\n",tinfo.tms_stime/ticks );
 
        fprintf(stderr, "childermn's user time:  %f seconds\n",tinfo.tms_cutime/ticks );
        fprintf(stderr, "childermn's system time:  %f seconds\n",tinfo.tms_cstime/ticks );
    }
}
 
int main(int argc, char const *argv[])
{
 
    if(atexit(showtimes)){
        fprintf(stderr, "Filed to install showtimes function\n" );
        exit(1);
    }
    return 0;
}

上面的程序是一个带有退出处理程序的例子,退出处理程序输出了CPU的使用情况。 

 

#include <stdio.h>
#include <stdlib.h>
 
static void function1();
static void function2();
 
int main(int argc, char const *argv[])
{
    printf("main function start...\n");
 
    if(atexit(function1)){
        fprintf(stderr, "atexit function calls error\n");
        exit(1);
    }
    if (atexit(function2))
    {
        fprintf(stderr, "atexit function calls error\n");
        exit(1);
    }
 
    printf("main function end...\n");
    return 0;
}
 
static void function1(){
 
    printf("This is function1\n");
}
 
static void function2(){
    printf("This is function2\n");
}
 
/*
    this program run result as follows:
     
    main function start...
    main function end...
    This is function2
    This is function1
 
*/

 上面的例子演示了atexit函数的用法,大家注意输出的顺序。

 

#include <stdlib.h>
#include <stdio.h>
 
int main(int argc, char const *argv[])
{
    system("ps ax");
    return 0;
}

 

 上面的这个例子显示了system的用法。其余6个exec的变种大家自行查找学习。

相关文章: