免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1096 | 回复: 0
打印 上一主题 下一主题

Linux下C语言基础 [复制链接]

论坛徽章:
8
2015年辞旧岁徽章
日期:2015-03-03 16:54:15午马
日期:2015-02-04 12:00:07羊年新春福章
日期:2015-02-04 11:57:56双子座
日期:2014-12-02 11:44:59金牛座
日期:2014-10-08 16:47:08狮子座
日期:2014-08-29 13:37:46巳蛇
日期:2014-08-26 17:32:29NBA常规赛纪念章
日期:2015-05-04 22:32:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-09 16:13 |只看该作者 |倒序浏览


//main.c
#include
int main(int argc,char **argv)
{
     printf("this is in main\n");
     return 0;
}
vi编辑main.c时。插入输入方式按ESC退出时,则在下面输入命令,用于进一步的操作。
:%s/printf("/printf("%s(%d)-%s/g
将字符串printf("替换成printf("%s(%d)-%s
运行结果:

//main.c
#include
int main(int argc,char **argv)
{
     printf("%s(%d)-%sthis is in main\n");
     return 0;
}
:%s/this/: this/g
运行结果:
//main.c
#include
int main(int argc,char **argv)
{
     printf("%s(%d)-%s: this is in main\n");
     return 0;
}
:%s/");/", __FILE__ ,__LINE__ ,__FUNCTION__);/g
//__FILE__前后分别由两个下划线组成的。
运行结果:
//main.c
#include
int main(int argc,char **argv)
{
     printf("%s(%d)-%sthis is in main\n", __FILE__ ,__LINE__ ,__FUNCTION__);
     return 0;
}


//global.h
#ifndef GLOBAL_H
   #define GLOBAL_H
   #define DEBUGFMT "%s(%d)-%s: "
   #define DEBUGARGS __FILE__ ,__LINE__ ,__FUNCTION__
#endif
//此头文件用于辅助调试程序,运行时打出程序运行结果的所在程序行。我将此头文件存于/usr/include/目录下。
//main.c
#include
#include
int main(int argc,char **argv)
{
    printf(DEBUGFMT "this is in main\n",DEBUGARGS);
    return 0;
}
#gcc -Wall main.c -o main
#./main
main.c(5)-main:this is in main


//hello.c
#include
int main(int argc,char **argv)
{
     printf("hello""Linux"",%s""\n",argv[0]);
     return 0;
}
#gcc -Wall hello.c -o hello
#./hello
hello Linux, ./hello
#!gcc   //运行前一次运行的gcc命令。


//hello.c
#include
int main(int argc,char **argv)
{
     printf("hello"\
"Linux"\
",%s"\
"\n",argv[0]);
     return 0;
}


GDB调试程序,指针,函数调用时的参数传递
//getNextWord.c
#include
#include
char * GetNextWord(char *s)
{
     char *d = 0;
     if (!s)
        return 0;
     d = s;
     while (*d !=' ' && *d !='\t')
//判断是否为空格键和Tab键 。
          d++;
     d++;
     while (*d == ' ' && *d == '\t')
          d++;
     return d;
}
//s="hello world" d="world"
//用于查找下一个单词。


//funca.c
#include
#include
int funca(void)
{
    char sa[] = "Hello, funca";
    char *da = 0;
    da = GetNextWord(sa);
    printf(DEBUGFMT "sa=%s da=%s\n",DEBUGARGS, sa, da);
    return 0;
}


//funcb.c
#include
#include
int funcb(void)
{
    char *sb = 0;
    char *db = 0;
    sb = (char *)malloc(12);//分配内存
    bzero(sb,12);//内存置零
    memcpy(sb,"Fine, funcb", 11);//向内存填入字符串
    db = GetNextWord(sb);
    printf(DEBUGFMT "sb=%s db=%s\n", DEBUGARGS, sb, db);
    return 0;
}


//main.c
#include
#include
int main(int argc,char **argv)
{
    funca();
    funcb();
    printf(DEBUGFMT "this is in main\n",DEBUGARGS);
    return 0;
}
#gcc -g -Wall main.c funcb.c funca.c getNextWord.c
//-g参数是为了下面GDB调试,做准备。
//按Alt+句号键,按一次得到上一个命令的最后一个参数,按两次得到上上一次命令的最后一个参数,......
#gdb ./a.out   //对a.out进行GDB调试
(gdb)list
//显示最近的十行源代码,再list显示再后面的十行源代码。
(gdb)list funca
//显示funca.c的源代码。如果源代码多,则需要多list几次。
//程序运行过程中,要查看当前运行位置周围的代码,用list。
(gdb)b
//break(断点)的简写。
(gdb)b funca
//在funca设置一个断点
(gdb)b GetNextWord.c:6
//在GetNextWord.c的第6行设置一个断点。
(gdb)r
//run的简写,让程序运行起来的命令。
(gdb)list
//查看运行所到位置的周围的源代码。
(gdb)n
//next:下一步的简写。
(gdb)print sa
//查看sa的内容
$1 = "Hello, funca"
(gdb)print &sa
//查看sa的地址
$2 = (char (*)[13])0xbf85c797
//0xbf85c797是H的地址。
//0xbf85c798是e的位置。
(gdb)print sa+7
$3 = 0xbf85c79e "funca"
//即:f的位置。



//argc.c
#include
#include
int main(int argc,char *argv[])
{
     printf
          (DEBUGFMT
           "argc=%d &argc=%08X main=%08X\nargv[0]=%s argv[1]=%s argv[2]=%s\n",
DEBUGARGS, argc, &argc, main, arhv[0], argv[1], argv[2]);
//打出argc的值,argc的地址,main函数的地址,打出程序的第一个参数,第二个参数,第三个参数。
     if (argv[3])
//如果第四个参数存在。
        printf(DEBUGFMT "at least 3 parameters\n", DEBUGARGS);
     else
        printf(DEBUGFMT "less than 3\n", DEBUGARGS);
     if (argc > 3)
//
        printf(DEBUGFMT "at least 3 parameters\n", DEBUGARGS);
     else
        printf(DEBUGFMT "less than 3\n", DEBUGARGS);
     return 0;
}
#gcc -g -Wall argc.c
#gdb ./a.out
(gdb)print argc
$1 = 1
(gdb)print sizeof(argv)
@2 = 4
(gdb)q
//退出


//readfile.c
#include
#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
    int fd = -1;
    char teststr[1024 * 4 + 1] = "";
    int ret = -1;
    if (argc
    {
       printf(“Usage: %s filename\r\n",argv[0]);
       return 1;
    }
    fd = open(argv[1],0);//打开文件。
    if (fd == -1)
    {
       printf("open file '%s' error. Info:%s\r\n",argv[1],strerror(errno));
//若fd=-1,则说明打开文件失败,并且打出失败的原因。
       return 2;
    }
    printf("show content of '%s' as following:\r\n",argv[1]);
    while (1)
    {
       if((ret = read(fd,teststr,sizeof(teststr)-1))
       {
          if(ret)
             printf("read error!!\r\n");
          else
             printf("end of file reached\r\n");
          break;
       }
       printf("%s",teststr);
       bzero(teststr, sizeof(teststr));
//内容置空。
     }
     if (fd > -1)
         close(fd);
     return 0;
}



//system.c
#include
#include
#include

int main(int argc, char **argv)
{
    int ret;
    ret = system(argv[1]);
//执行system的内容的命令。
    printf("ret=%d\n",ret);
    if (WEXITSTATUS(ret) == 1)
//WEXITSTATUS确保是system的内容的执行命令的返回值 。
        printf("parameter error\n");
    else if (WEXITSTATUS(ret) == 2)
        printf("filename error\n");
    else
        printf("OK! return value is:%d\n",WEXITSTATUS(ret));
    return 0;
}



//popen.c
//本函数块:执行一个命令,并且取得命令执行的输出内容。
#include
#include
int main(int argc, char **argv)
{
    FILE *fp = 0;
    int ret = 0;
    fp = popen(argv[1],argv[2]);
    if ((argv[2][0] == 'r') || (argv[2][0] == 'R'))
    {
       printf("----output from the command '%s' is:----\n",argv[1]);
       while ((ret = getc(fp)) != E0F)
          printf("%c",ret);
       printf("-----------------------\n");
    }
    if ((argv[2][0] == 'w') || (argv[2][0] == 'W'))
    {
       fprintf(fp, "mary 20\r\n");
    }
    if (fp)
        ret = pclose(fp);
    if (WEXITSTATUS(ret) == 1)
        printf("parameter error\n");
    else if (WEXITSTATUS(ret) == 2)
        printf("filename error\n");
    else
        printf("OK! return value is:%d\n",WEXITSTATUS(ret));
    return 0;
}




输入和输出
//printf_scanf.c
#include
int main(int argc,char **argv)
{
    char name[16] = "";
    unsigned char age = 0;//不用int,是因为char型对于表达年龄,已经足够,可以省下内存空间。
    printf("Please input your age and name:");
    scanf("%d %s",&age,name);
    printf("Your name is %s,and age is %d\n",name,age);
    return 0;
}


#man -a printf   //查看printf的帮助文档。输入/printf,可以用于在帮助文档中查找printf。

//writelog.c
#include
int main(int argc,char **argv)
{
    FILE *fp = 0;
    fp = fopen("/temp/myapp.log","a");
    if (!fp)
    {
       printf("cannot open file /tmp/myapp.log\n");
       return -1;
    }
    fprintf(fp, "This is my log written into a file\n");
//将后面的内容输入到fp指针指向的文件中去 。
    fclose(fp);
    return 0;
}


//fprintf_fscanf.c
#include
int main(int argc,char **argv)
{
    char name[16] = "";
    unsigned char age = 0;
    fprintf(stdout, "Please input your age and name:");
    fscanf(stdin, "%d %s",&age, name);
    fprintf(stdout, "Your name is %s,and age is %d\n",name, age);
    return 0;
}


//read_write.c
#include
#include
#define PROMPTName "Please input your name and age: "
#define OUTPUTName "Your name is: "
#define OUTPUTAge ",and your age is: "
int main(int argc, char **argv)
{
       char name[9] ="",age[4] = "";
       write(1,PROMPTName, strlen(PROMPTName));
       read(0,name,8);
       read(0,age, 3);
       write(1,OUTPUTName,strlen(OUTPUTName));
       write(1,name,strlen(name));
       write(1,OUTPUTAge,strlen(OUTPUTAge));
       write(1,age,strlen(age));
       return 0;
}
//在unistd.h中,有如下定义:
/*Standard file descriptors. */
#define STDIN_FILENO   0  /*Standard input. */
#define STDOUT_FILENO  1  /*Standard output. */
#define STDERR_FILENO  2  /*Standard error output. */
//0 - stdin
//1 - stdout
//2 - stderr
//程序中加入
#include
fclose(stdout);
或者
#include
close(1);
//则关闭标准输出。那么程序就不会输出内容了 。


//Linux下的链接库
.a结尾的是静态链接库。
.so结尾的是动态链接库。
//assert.c
#include
#include
#include
int main(int argc,char **argv)
{
   if(argc
   {    printf("参数个数不够\n");
       exit(0);
   }
   printf("OK\n");
   return 0;
}
或者改成
#include
#include
#include
int main(int argc,char **argv)
{
   assert(argc >= 3);
//如果argc不大于等于3,就退出。
   printf("OK\n");
   return 0;
}


//ctype.c
//先查看头文件的内容,查看其中的函数,通过man函数名,以判断函数是否存在。
#include
#include
#include
#include

void Str2Upper(char *s)
//若为const char *s则s是只读的,不能改变的。
{
     int i,len;
     if(!s) return;
     printf("begin s=%s\n",s);
     len = strlen(s);
     for(i = 0;i
          s = toupper(s);
     printf("end s=%s\n",s);
     
}
int main(int argc,char **argv)
{
    int len;
    assert(argc>=2);
    len=strlen(argv[1]);
    for(i = 0;i
    {
        if(isdigit(argv[1]))
        printf("第%d个字符是数字\n",i+1);
    }
    Str2Upper(argv[1]);
    return 0;
}
//isupper用于判断是否大写。


//error.h
#include
#include
#include
int main(int argc,char **argv)
{
    FILE *fp = 0;
    fp = fopen(argv[1],"r");
    if(!fp)
    {
       printf("打不开文件'%s'失败!\n",argv[1]);
       printf("错误代码%d,%s\n",errno,strerror(errno));
       exit(0);
    }
    printf("打开文件成功!\n");
    fclose(fp);
    return 0;
}
//也可改成

#include
#include
#include
int main(int argc,char **argv)
{
    FILE *fp = 0;
    fp = fopen(argv[1],"r");
    if(!fp)
    {
       printf("打不开文件'%s'失败!(%d)%s\n",argv[1]),errno,strerror(errno));
       exit(0);
    }
    printf("打开文件成功!\n");
    fclose(fp);
    return 0;
}


//pointer_err.c(错误的源码)
#include
#include
#include
int main(int argc,char **argv)
{
    char * p = 0;
    GetMem(p);
    prontf("%s\n",p);
    return 0;
}
void GetMem(char *x)
{
    char s[] = "Hello,Linux";
    int len = strlen(s);
    x = (char *)malloc(20);
    memset(x, 0, 20);
    memcpy(x, s, len);
}

//pointer_err.c(正确的源码)
#include
#include
#include
int main(int argc,char **argv)
{
    char * p = 0;
//只是声明p的类型,即p的类型是(char *),并对p进行赋值:p = 0。变量p的类型是(char *) 。
    GetMem(&p);
//调用函数GetMem(),并传递参数。因为我们希望的是调用函数GetMem()后,对P进行赋值,并且最终打出p的值。所以此处是按地址传递,即传递p的地址,而不是p的值。
    prontf("%s\n",p);
    return 0;
}
void GetMem(char **x)
//因为传递来的参数p的类型是(char *),所以此处的指针x的类型需定义成(char *)* x。一个*是指针定义所加的,一个*是是因为传递来的参数p的类型(char *)决定的 。因为传递来的是p的地址,所以此处的 x = &p。指针定义 。
{
    char s[] = "Hello,Linux";
    int len = strlen(s);
    *x = (char *)malloc(20);
    memset(*x, 0, 20);
    memcpy(*x, s, len);
}



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/54642/showart_460349.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP