Linux编程笔记--C语言基础

去年学习linux时的部分笔记,浅显易懂,比较简单

*A是65

//dataTypeExample1.c
#include<stdio.h>
main()
{
   int x;
   x=10*9*8*7*6*5*4*3*2*1;
   print("%d",x);
}

//dataTypeExample2.c
#include<stdio.h>
#define PI 3.14159
main()
{
  float s,r;
  scanf("%f",&r);
  s=r*r*PI;
  printf("%8.3f\n",s);
}

//dataType1.c
#include<stdio.h>
main()
{
   int a=3,b=5,x;
   x=3+5;
   printf("%d\n",x);
   x=a-b;
   printf("%d\n",x);
   x=a*b;
   printf("%d\n",x);
   x=a%b;
   printf("%d\n",x);
   x=a/b;
   printf("%d,%d\n",x,x);

   int k=5; float l=3.5;
   float m;
   m=!k&&!l;
   printf("%f\n",m);
  
   printf(sizeof(int));
   printf(sizeof(float));
}

printf,scanf不是保留字;
getchar()函数,输入字符,程序被中断,等待用户输入;
putchar()函数,输出字符;
pow(x,y)表示X的y次方;

//顺序程序设计 example1.c
#include<stdio.h>
main()
{
  int a=11,b=22,c=33;
  printf("a=%d\tb=%d\tc=%d\n",a,b,c);
  {
    int b=23;float c=3.4;
    printf("a=%d\tb=%d\tc=%d\n",a,b,c);
    a=b;
  }
  printf("a=%d\tb=%d\tc=%d\n",a,b,c);
}
//顺序程序设计 example2.c
#include<stdio.h>
main()
{
  int a;
  char b;
  a=getchar();
  b=getchar();
 
  printf("a=%c\ta=%d\tb=%c\tb=%d\n",a,a,b,b);

}
//两个getchar要连续输入再敲回车;


//顺序程序设计 example3.c
#include<stdio.h>
main()
{
  int a=65;
  char b='B';
  putchar(a);
  putchar(65+1);
  putchar(b);
  putchar('B'+1);
  putchar('\n');//回车
 
}

//顺序程序设计 example4.c
#include<stdio.h>
main()
{
  int i=8;
  printf("%d\t%d\t%d\n",++i,--i,i--);
  //从右至左计算
}

//选择程序设计 Xexample1.c
#include<stdio.h>
main()
{
   int x,y,max;
   scanf("%d,%d",&x,&y);
   if(x>=y)
   {
       max=x;
       printf("%d\n",max);
   }
   else
   { 
      max=y;
      printf("%d\n",max);
   }
}

//循环程序设计 xunhuan.c
#include<stdio.h>
main()
{
    char ch;
    int num=0;
    ch=getchar();
    while(ch!='\n')
    {
        num++;
        ch=getchar();
    }
    printf("%d\n",num);
}

////////////////////////////////////
数组
字符串拷贝 strcpy()
字符串连接 strcat()
%u 无符号整形值
字符串以\0结束
字符串长度 strlen()
字符串比较 strcmp()
puts(str) 输出字符串
gets(str) 输入字符串
//例1  二维数组
#include<stdio.h>
main()
{
   int i,j,a[3][4];
   for(i=0;i<3;i++)
   {
        for(j=0;j<4;j++)
        {
           scanf("%d",&a[i][j]);
        }
   }
    for(i=0;i<3;i++)
   {
        for(j=0;j<4;j++)
        {
           printf("%d",&a[i][j]);
        }
          printf("\n");
   }
}
//例2  gets函数有警告 没关系 继续运行
#include<stdio.h>
main()
{
     int i,j,m;
     char s1[20],s2[20];
     gets(s1);
     scanf("%d",&m);
     i=m-1;j=0;
  
     while(s1[i]!='\0')
     {
           s2[j]=s1[i];
           i++;j++;
     }
     s2[j]='\0';
     puts(s2);
}


//////////////////////////////////////
变量的存储特性:
自动型     auto
静态型     static
寄存器型   register 最多两个
外部型     extern

#include<stdio.h>
long fac(unsigned n)
{
   long f;
   if(n==0)
   {
       f=1;
   }
   else
   {
       f=n*fac(n-1);
   }
   return f;
}

main()
{
    long y;
    int  n;
    scanf("%d",&n);
    y=fac(n);
    printf("%d!=%d\n",n,y);
}


//////////////////////////////////////
3个预处理命令: 
宏定义
#define 宏名 串(宏体)
#define PI   3.14159
#undef 终止宏定义的作用域;
#undef PI
带参数的宏定义
#define S(a,b) a*b
引用宏只占编译时间,不占运行时间;
宏的引用时用表达式替代形参
引用  S(a+c,b+c)
展开  a+c*b+c

包含文件
#include<stdio.h>

条件编译
///////////////
#if  常量表达式
     程序段
#endif
///////////////
#if  常量表达式
     程序段 1
#else
     程序段 2
#endif
////////////////
#if  常量表达式
     程序段 1
#elif
     常量表达式
     程序段 2
#else
     程序段 3    
#endif
////////////////
#ifdef  标示符   //如果已经定义
        程序段
#endif 
////////////////
#ifndef  标示符  //如果没有定义
        程序段
#endif 
////////////////

//例1
#include<stdio.h>
#define PI 3,14
#define R  3.0
#define R2 3.0
#define L 2*PI*R
#define S PI*R*R
main()
{
   printf("L=%f\nS=%f\n",L,S);
   #define R2
}

//例2
#include<stdio.h>
#define DEBUG    //调试语句  发布给客户的时候注释掉 
main()
{
    #if  5
          printf("AAA\n");
    #else
          printf("BBB\n");
    #endif

    #ifdef DEBUG
          printf("CCC\n");
    #endif
}
///////////////////////////////////////////
指针:
指向函数的指针的定义方式:
类型   (*指针变量名) ();
例如:  int (*p)();

main()
{
   int max();
   int (*p)();
   int a,b,c;
   p=max;
   scanf("%d,%d",&a,&b);
   c=(*p)(a,b);
   printf("%d",c);
}

//指针函数
#include<stdio.h>
main()
{
   int a,b,*p;
   int *max();
   scanf("%d,%d",&a,&b);
   p=max(a,b);
   printf("max=%d",*p);
}
int *max(int x,int y)
{
     if(x<y)
    {
       return(&y);
    }
    else
    {
       return(&x);
    }
}
//////////////////////////////////////////
结构体:
定义类型的时候定义变量
结构体成员又是一个结构体变量

struct date
{
  int month;
  int day;
  int year;
};
struct student
{
  char name[20];
  char sex;
  int age;
  struct date birthday;
}stu1,stu2;
stu1={"hank",'N',22,4,24,1986};

///////////////////////////////////////////////
共同体:
union date
{
  int month;
  int day;
  int year;
}d1,d2,d3;

///////////////////////////////////////////////
枚举型
enum 枚举类型名 {取值表}
例如,enum weekdays {Sun,Mon,Tue,Wed,Thu,Fri,Sat};
元素自动编号 0,1,2,3......

///////////////////////////////////////////////
将变量名换成别名 在定义体前面加上typedof:
typedof struct date {.....} DATE;
typeof int INT;
INT x=100;   int的别名为INT;********

///////////////////////////////////////////////
位段结构:
特殊的结构类型,其所有成员均以二进制位为单位定义长度,
并称成员为位段;
加入无名字段,其后的字段从下一个字节开始存储;
例如,cpu的状态寄存器,按位段类型定义如下:
struct status
{
    unsigned sign:        1;  /*符号标志*/
    unsigned zero:        1;  /*零标志*/
    unsigned carry:       1;  /*进位标志*/
    unsigned :            0;  /*长度为0的无名位段*/
    unsigned parity:      1;  /*奇偶/溢出标志*/
    unsigned half_carry:  1;  /*半进位标志*/
    unsigned negative:    1;  /*减标志*/
}flags;

//////////////////////////////////////////////////

你可能感兴趣的:(编程,C++,c,linux,C#)