C语言基础知识

C语言基础知识




找到了一点培训的资料,自己又添加了一部分,拿出来大家指点。没有写完整的函数掉用关系,只是写了代码的片断,希望对学习C语言的初学者有帮助。

1.数据类型部分

   1.1 简单又简单的数据运算和输出

         /*  定义变量并赋初值  */

        int    a = 5;      

       char   c = 'a';  

       float  f = 5.3;    

       double m = 12.65;

       double result;

        

         /* 同类型数据间进行运算并输出结果 */

         printf("a + c = %d\n"a + c);

         printf("a + c = %c\n"a + c);//按照ASCII数值计算只不过输出形式不一样而已

         printf("f + m = %f\n"f + m);

 

         /* 不同类型数据间进行运算并输出结果 */

         printf("a + m = %f\n"a + m);

         printf("c + f = %f\n"c + f);

 

         /* 将上述四个变量进行混合运算,并输出结果 */

         result = a + c * (f + m);

         printf("double = %f\n"result);

          getch();

   1.2格式化输出一下
          

        /* 换行符'\n',用于输出换行 */

         printf("How are you?\n");

         printf("I am fine.\n\n");

 

         /* 横向跳格符'\t',使跳到下一个输出区 */

         printf("How are you?\t");

         printf("I am fine.\n\n");

 

         /* 退格符'\b',使当前的输出位置退一格,即输出的起始位置左移一位 */

         printf("  How are you?\n");

         printf(" \bI am fine.\n\n");

 

         /* 回车符'\r',使当前输出位置回到本行开头 */

         printf("                I am fine.");

         printf("\rHow are you?\n\n");

 

         /* 多个转义字符的混合运用 */

         printf("note:\n  a s\ti\b\bk\rp\n");


  1.3简单的bool运算
   

        /* 定义一个整数类型的变量,用来存放后面算式的值 */

         int logic;   

 

         int a = 1;

         int b = 2;

         int c = 3;

 

         logic = a+b>c&&b<=c;

         printf("logic = %d\n", logic);

 

         logic = a>=b+c||b==c;

         printf("logic = %d\n", logic);

 

         logic = !(a<c)+b!=1&&(a+c)/2;

         printf("logic = %d\n", logic);

         getch();

 
   1.4 ++和--总是不清楚
    
  

         int i, j, k;

         int m, np;

 

         i = 8;

         j = 10;

         k = 12;

   

         /* 自增在操作数之前 */

         m = ++i;

        printf("i = %d\n"i);

         printf("m = %d\n", m);

 

         /* 自减在操作数之后 */

         n = j--;

         printf("j = %d\n", j);

         printf("n = %d\n"n);

 

         /* 自增、自减的混合运算 */

         p = (++m)*(n++)+(--k);

         printf("k = %d\n", k);         

         printf("p = %d\n"p);        

         getch();


  1.5 与或非一下
   

         /* 定义了一个无符号字符型变量,此变量只能用来存储无符号数 */

         unsigned char result;

   

         int a, b, c, d;

         a = 2;

         b = 4;

         c = 6;

         d = 8;

 

         /* 对变量进行“按位与”操作 */

         result = a & c;

         printf("result = %d\n"result);

 

         /* 对变量进行“按位或”操作 */

         result = b | d;

         printf("result = %d\n"result);

 

         /* 对变量进行“按位异或”操作 */

         result = a ^ d;

         printf("result = %d\n"result);

 

         /* 对变量进行“按位取反”操作 */

         result = ~a;

         printf("result = %d\n"result);

         getch();

   
   1.6 左右移

    unsigned a, b, c, d;

         int n;

 

         a = 64;

         n = 2;

 

         /* 将操作数a右移(6-n) */

         b = a >> (6-n);

         printf("b = %d\n", b);

 

         /* 将操作数a左移n */

         c = a << n;

         printf("c = %d\n", c);

 

         /* 对操作数a进行的混合位运算 */

         d = (a >> (n-1)) | (a << (n+1));

         printf("d = %d\n", d);

         getch();


  1.7 可恨又可爱的指针
   

        /* 定义整形指针 */

        int begin=0end=0;
        int *p1=0;
        const int* p2=0;
        int* const p3=&begin;
        const int* const p4=&end; 
         
 begin = 10;

         /* 让人头晕的赋值 */

         p1 = &begin;

         end = *p1;

         *p2=1;//Error
         *p3=1;//OK
         p2=&end;//ok
         p3=&end;//Error
         *p4=1//Error
         p4=&end;//Error

         printf("begin = %d\n"begin);

         printf("end = %d\n"end);

 

         /* 输出指针中的地址值 */

         printf("p 1= %d\n"p1);

         printf("&begin = %d\n", &begin);

         printf("*p 1= %d\n", *p1);

         getch();


  1.8 大公约和小公倍
     
    int xy, num1, num2, temp;

         printf("请输入两个正整数:\n");

         scanf("%d %d", &num1, &num2);

 

         if(num1 < num2)

         {

                   temp = num1;

                   num1 = num2;

                   num2 = temp;

         }

         x = num1;

         y = num2;

         while(y != 0)

         {

                   temp = x%y;printf("%d\n", temp);

                   x = y;

                   y = temp;

         }

         printf("它们的最大公约数为:%d\n"x);

         printf("它们的最小公倍数为:%d\n", num1*num2/x);

         getch();


   1.9 矩阵转置
 
    void convert(int element[N][N])

        {

            int ijt;

            for(i=0; i<Ni++)

                  for(j=i+1; j<Nj++)

                   {

                            t = element[i][j];

                            element[i][j] = element[j][i];

                            element[j][i] = t;

                   }

         }

 
   1.10 命令行参数

   void main(int argccharargv[])

        {

           int dispcount;

 

          if(argc < 2)

          {

                  printf("You must enter the length of the count\n");

                  printf("on the command line. Try again\n");

                   exit(1);    /* 非正常跳出程序 */

          }

 

          if(argc==3 && !strcmp(argv[2], "display"))

                   disp = 1;

          else

                   disp = 0;

          for(count = atoi(argv[1]); count; --count)

                   if(disp)

                            printf("%d\n"count);

 

          putchar('\a');    /* 将产生蜂鸣 */

          printf("Down");

          getch();

          return;

      }

   

  1.11 查找字符串原来是这样

     int find_substr(chars1chars2)

    {

         register int t;

         char *p, *p2;

 

        for(t=0; s1[t]; t++)

         {

                   p = &s1[t];

                   p2 = s2;

 

                  while(*p2 && *p2==*p)

                   {

                            p++;

                            p2++;

                   }

                   if(! *p2)

                            return t;

         }

         return -1;

     }


  1.12 也算年月日
             

      /* 给出年、月、日,计算该日是该年的第几天 */

     # include <stdio.h>

     # include <conio.h>

 

    int sum_day(int monthint day);

    int leap(int year);

 

    void main()

    {

         int yearmonthday;

         int days;

         printf("请输入日期(年,月,日)");

         scanf("%d, %d, %d", &year, &month, &day);

         printf("%d%d%d"yearmonthday);

         days = sum_day(monthday);    /* 调用函数sum_day() */

         if(leap(year) && month>=3)    /* 调用函数leap() */

                   days = days + 1;

         printf("是该年的第%d.\n"days);

         getch();

   }

 

    /* 定义静态存储变量并赋初值 */

   static int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

 

    int sum_day(int monthint day)    /* 计算日期 */

    {

         int i;

         for(i=1; i<monthi++)

                   day = day + day_tab[i];

         return day;

    }

 

    int leap(int year)

   {

         int leap;

         leap = (year%4==0&&year%100!=0)||(year%400==0);

         return leap;

    }

    1.13 插入字符
                              

       # include <stdlib.h>

       # include <string.h>

       # include <stdio.h>

 

      void main()

      {

         /* 声明子函数 */

         int binary(char *ptr[], char *strint n);    /* 查找函数声明 */

         void insert(char *ptr[], char *strint nint i);    /* 插入函数声明 */

 

         char *temp, *ptr1[6];

         int i;

         printf("请为字符形指针数组赋初值:\n");

         for (i=0; i<5; i++)

         {

                   ptr1[i] = (char *)malloc(20);    /* 为指针分配地址后 */

                  gets(ptr1[i]);    /* 输入字符串 */

         }

         ptr1[5] = (char *)malloc(20);

         printf("\n");

 

         printf("original string:\n");

         for(i=0; i<5; i++)    /* 输出指针数组各字符串 */

                  printf("%s\n"ptr1[i]);

 

         printf("\ninput search string:\n");

         temp = (char *)malloc(20);

         gets(temp);    /* 输入被插字符串 */

 

         i=binary(ptr1temp, 5);    /* 寻找插入位置i */

         printf("i = %d\n"i);

 

         insert(ptr1temp, 5, i);    /* 在插入位置i处插入字符串 */

         printf("output strings:\n");

 

         for(i=0; i<6; i++)    /* 输出指针数组的全部字符串 */

                  printf("%s\n"ptr1[i]);

    }

 

       int binary(char *ptr[], char *strint n)

      {

         /* 折半查找插入位置 */

         int higlowmid;

         low = 0;

         hig = n-1;

         if(strcmp(str,ptr[0]) < 0)

                   return 0;

        /* 若插入字符串比字符串数组的第0个小,则插入位置为0 */

         if(strcmp(str,ptr[hig]) > 0)

                   return n;

        /* 若插入字符串比字符串数组的最后一个大,则应插入字符串数组的尾部 */

        while(low <= hig)

         {

                   mid = (low + hig)/2 ;

                   if (strcmp(str,ptr[mid]) < 0)

                            hig = mid - 1;

                   else if(strcmp(str,ptr[mid]) > 0)

                            low = mid + 1;

                   else

                            return mid;    /* 插入字符串与字符串数组的某个字符串相同 */

          }

          return low;    /* 插入的位置在字符串数组中间 */

     }

 

      void insert(char *ptr[], char *strint nint i)

      {

         int j;

         for(j=nj>ij--)    /* 将插入位置之后的字符串后移 */

                  strcpy(ptr[j], ptr[j-1]);

         strcpy(ptr[i], str);    /* 将被插字符串按字典顺序插入字符串数组 */

      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值