天天看點

C語言程式設計(4)

常見關鍵字

auto , break , case , cha r, const , continue , default , do  , double , else , enum , extern

float , for , goto , int , long , register , return , short , signed , sizeof , static , struct , switch

typedef , funion , unsigned , void , volatile , while

//auto -自動
#include <stdio.h>
int main()
{
  auto int a = 10;  //局部變量 -自動變量 建立銷毀
  return 0;
}      

register 寄存器關鍵字

計算機存儲資料:

寄存器

高速緩存

記憶體  8G/4G/16G

硬碟  500G

//signed  unsigned 無符号
#include <stdio.h>
int main()
{
  //int 定義的變量是有符号的 signed int;
  int a = 10;
  a = -2;
  unsigned int num = -1; //永遠都是正數
  return 0
}      
//typedef -類型定義 -類型重定義
#include <stdio.h>
int main()
{
  typedef unsigned int u_int;
  unsigned int num = 20;
  u_int num2 = 20;
  return 0;
}      

static

static修飾局部變量

局部變量的生命周期邊長

#include <stdio.h>
void test()
{
  static int a = 1; //a是一個靜态的局部變量
  a++;
  printf("a = %d\n",a);
}
int main()
{
  int i = 0;
  while(i<5)
  {
    test();
    i++;
  }
  return 0;
}      

static修飾全局變量

改變了變量的作用域 -讓靜态的全局變量隻能在自己所在的源檔案内部使用,出了源檔案就沒法再使用了

static int g_val = 2021;      
#include <stdio.h>
int main()
{
  //extern -聲明外部符号的
  extern int g_val;
  printf("g_val = %d\n",g_val);
  return 0;
}      

static修飾函數

也是改變了函數的作用域 -說法不準确

static修飾函數改變了函數的連結屬性

外部連結屬性 -->内部連結屬性

static int Add(int x,int y)
{
  int z = 0;
  z = x + y;
  return z;
}      
#include <stdio.h>
extern int Add(int,int);
int main()
{
  int a =10;
  int b =20;
  int sum =Add(a,b);
  printf("sum = %d\n",sum);
  return 0;
}      
//#define 定義函數和宏
#include <stdio.h>
#define MAX(x,y) (x>y?x:y)
int main()
{
  int a = 10;
  int b = 20;
  max = MAX(a,b);
  printf("max = %d\n",max);
  return 0;
}      

指針

#include <stdio.h>
int main()
{
  int a = 10; //4個位元組
  int* p = &a; //&取位址
  //有一種變量是用來存放位址的 -指針變量
  printf("%p\n",&a);
  printf("%p\n",p);
  *p = 20;  //* -解引用操作符
  printf("a = %d\n",a);
  return 0;
}      
#include <stdio.h>
int main()
{
  char ch = 'w';
  char* pc = &ch;
  *pc = 'q';
  printf("%c\n",ch);
  return 0
}      

指針變量的大小

#include <stdio.h>
int main()
{
  int a = 10;  //申請了四個位元組的空間
  printf("%p\n",&a);
  int* p = &a; //p是一個變量 -指針變量
  printf("%p\n",p);
  *p = 20; //* -解引用操作符/間接通路操作符
  printf("a = %d\n",a);
  return 0;
}      
#include <stdio.h>
int main()
{
  double d = 3.14;
  double* pd =&d;
  *pd = 5.5;
  printf("%lf\n",d);
  printf("%lf\n",*pd);
  return 0;
}      

結構體

#include <stdio.h>
//建立一個結構體類型
struct Book
{
  char name[20];
  short price;
};  //用來結束這個類型定義

// . (結構體變量.成員)
#include <stdio.h>
int  main()
{
  //利用結構體類型 -建立一個該類型的結構體變量
  struct Book b1 = {"C語言程式設計",55};
  printf("書名:%s\n",b1.name);
  printf("價格:%d\n",b1.price);
  b1.price = 15;
  printf("修改後的價格:%d\n",b1.price);
  strcpy(b1.name,"C++"); //數組名本質上是個位址 strcpy -string copy -字元串拷貝 -庫函數
  printf("修改後的書名:%s\n",b1.name);
  return 0;
}      
#include <stdio.h>
//建立一個結構體類型
struct Book
{
  char name[20];
  short price;
};  //用來結束這個類型定義
//-> (結構體指針->成員)
int main()
{
  struct Book b1 = {"C語言程式設計",55};
  struct Book* pb = &b1;
  printf("%s\n",(*pb).name);
  printf("%d\n",(*pb).price);
  printf("%s\n",pb->name);
  printf("%d\n",pb->price);
  return 0;
}