Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474981
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 14:18:00


点击(此处)折叠或打开

  1. //Makefile

  2. #练习编写Makefile文件

  3. src = $(wildcard *.c)
  4. targets = $(patsubst %.c, %, $(src))

  5. CC = gcc
  6. CFLAGS = -Wall -g

  7. all:$(targets)
  8. $(targets):%:%.c
  9.     $(CC) $(CFLAGS) $< -o $@
  10. clean:
  11.     rm $(targets)
  12. .PHONY:clean

点击(此处)折叠或打开

  1. //endian.c
  2. //测机器的大小端

  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     int n = 0x1234567, *p;
  7.     p = &n;
  8.     
  9.     if(*(short*)p == 0x1234)
  10.         printf("big endian\n");
  11.     else
  12.         printf("little endian\n");
  13.     return 0;
  14. }

点击(此处)折叠或打开

  1. //partition.c
  2. //用位操作,将一个32位数,分割成两个数

  3. #include <stdio.h>

  4. void partition(unsigned int n)
  5. {
  6.     unsigned int a, b;
  7.     a = n & 0xffff;
  8.     b = (n >> 16) & 0xffff;
  9.     printf("%#x\n", a);
  10.     printf("%#x\n", b);
  11. }


  12. int main(int argc, const char *argv[])
  13. {
  14.     unsigned int n;

  15.     printf("n=0x");
  16.     scanf("%x", &n);
  17.     
  18.     partition(n);
  19.     return 0;
  20. }

点击(此处)折叠或打开

  1. //show.c
  2. //将一个数,逆序输出

  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     int n, num = 0, tmp, i;
  7.     int arr[5] = {0};

  8.     printf("n=");
  9.     scanf("%d", &n);

  10.     tmp = n;
  11.     
  12.     do{
  13.         num++;
  14.         if(num > 5)
  15.             break;
  16.         arr[num-1] = tmp%10;
  17.         tmp/=10;
  18.       }while(tmp);
  19.     
  20.     if(num > 5)
  21.         printf("the numbers of n: (1~5)\n");
  22.     else
  23.     {
  24.         printf("forward: %d\n",n);
  25.         printf("reverse: ");
  26.         i = 0;
  27.         while(arr[i])
  28.         {
  29.             printf("%d",arr[i]);
  30.             i++;
  31.         }
  32.         printf("\n");
  33.     }
  34.     

  35.     return 0;
  36. }

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