Chinaunix首页 | 论坛 | 博客
  • 博客访问: 147289
  • 博文数量: 49
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 595
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 23:08
文章分类
文章存档

2011年(1)

2009年(48)

我的朋友

分类: C/C++

2009-03-07 09:55:39

 

LinuxC程序开发的过程

使用vi等编辑工具编写源程序

保存为*.c

使用gcc编译成二进制可执行文件

./a.out执行

有问题可以使用gdb进行调试

vi hello.c

 

#include <stdio.h>
int main(int argc,char **argv)
{
        printf("Hello World!\n");
        return 0;
}

gcc -o hello hello.c

./hello


几个字符串函数

strlen

strcmp

strcat

strcpy

指针及指针变量,指向指针的指针


vi example1.c

 

#include <stdio.h>
main()
{
int a,b;
int *p,*q;
a=100,b=2;
p=&a;
q=&b;
int **p1;
p1=&p;
printf("a=%d\nb=%d\n*p=%d\n*q=%d\n*p1=%d\n",a,b,*p,*q,**p1);
}
指针引用传递
vi example2.c
#include <stdio.h>
int swap(int *a,int *b){
if(*a>*b){
int t=*a;
*a=*b;
*b=t;
}
}
main()
{
int a=200,b=100;
swap(&a,&b);
printf("a=%d,b=%d",a,b);
}

指针函数
vi example3.c
#include <stdio.h>
int *swap(int *a,int *b){
if(*a>*b){
int t=*a;
*a=*b;
*b=t;
}

return a;
}
main()
{
int a=200,b=100;
int *c;
c=swap(&a,&b);
printf("a=%d\nb=%d\n*c=%d\n",a,b,*c);

指针函数
vi example4.c
#include <stdio.h>
int max(int a,int b){
return a>b?a:b;
}
main()
{
int a=200,b=100;
int (*c)();
c=max;(*c)(a,b);
printf("a=%d\nb=%d\nc=%d\n(*c)(a,b)=",a,b,(*c)(a,b));
}
指向数组的指针
 vi example5.c
#include <stdio.h>
main()
{
int *p,i,a[10];
p=a;
for(i=0;i<10;i++)
{
        scanf("%d",p++);
}
p=a;
for(i=0;i<10;i++)
{
printf("%d\n",*p++);
}
}
数组指针的移动
vi example6.c

#include <stdio.h>
main()
{
int a[5]={1,3,5,6,7};
int *p=a+3;
printf("*p=%d\n*(p+1)=%d\n*p+3=%d\n",*p,*(p+1),*p+3);
}
数组指针的移动
vi example7.c
#include <stdio.h>
main()
{
char a[80],b[80],*p,*q;
int n;
gets(a);
scanf("%d",&n);
p=a;q=b;
p+=n-1;
while(*p!='\0')
{
*q=*p;
p++;q++;
}
*q='\0';
puts(b);
}

阅读(802) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~