C總結1

C-總結

 

1,C的開發環境是用Visual C++ 6.0 SP6簡體中文版。

2. C的標準輸入是:

如:inta=0;   %的意思是轉義符。

scanf(%d\n,&a);  &的意思是取地址。%d標識是輸入的是int類型。

輸出是:printf(%d,a);//輸出a的值。%d標識輸出的是int類型的值。

3.const 關鍵字是限定它定義的變量是不可變的。

4.C的三種基本數據類型:整數,浮點,字符。

5,sizeof(數據類型)函數是獲取數據類型的所佔長度。

6.signed和unsigned 修飾符分別代表:有符號的和無符號的,signed修飾的整數可以是所以整數,而unsigned修飾的整數只能是非負整數。

7.typedof 這個關鍵字的作用是爲數據類型起個別名,如:typedof char mychar;

mychar m=’c’;

8.volatile 這個關鍵字是編譯器優化時,每次都重新讀取這個值,而不是使用寄存器中的備份。

9.%d 代表是整數

 %c 代表是字符

 %f  代表是float

 %lf 代表是double

10, 數組的定義:int i[]={1,3,5,4}; (不能把中括號放在變量前面。)

11,C函數的定義分爲函數聲明和函數體兩部分。

12,C函數的調用,如果想把函數放在主函數下面運行,那麼在運行之前,一定 要在主函數上面聲明才行,而如果不想聲名,那就只能把函數放在主函數上面了。

13,函數的傳值,分爲 值傳遞,引用傳遞,和指針傳遞三種方式。

14,計算直角三角形的斜邊。

  #include <stdio.h>

#include<math.h> //數學庫頭文件

                                         

/* FunctionPrototype */

double hypot (double x , double y);//聲明一個函數原型。

 

int main ()

{

double side1, side2;

printf(“Enter 2 sides of Right triangle\n”)       ;

scanf(“%lf%lf”,&side1, &side2);

printf(“The hypotenuse is: %f\n“, hypot(side1,side2));//函數調用,調用hypot。

                                           //實參:函數調用時,實際傳遞給函數的參數。

                                           //將打印hypot函數的返回值

return 0;

}

 

double hypot(double x , double y) //形參:函數聲明或定義時的函數參數。

{

return (sqrt( x*x + y*y)); //開平方函數。可以把一個表達式,作爲函數調用時傳遞的實參。

//把return 後面表達式的值,作爲函數的返回值。

//該返回值的類型,要和函數頭裏聲明的返回值類型一致。

}

 

 

15,內聯函數,在執行主函數時,如果執行到是內聯函數的話,它不會跳轉到這個函數體裏面執行,而是把內聯函數體內的代碼加載到調用該函數的函數中,提高運行效率。

For example:

inline int func( int a, int b )

{

return a+b;

}

 

16,函數重載(C++特有)

兩個函數名字一樣,參數不一樣(參數個數不同、參數類型不同),成爲函數重載。

不同的參數列表。

 Example

#include <iostream>

using namespace std;

 

int square(inti)

{ return i * i; }

 

float square(floatf)

{ return f * f; }

 

int main( )

{

         inta = 2;

         floatb = 0.5;

 

         cout<< "The square of " << a << " is "<< square(a) << endl;

         cout<< "The square of " << b << " is "<< square(b) << endl;

         return0;

}

 

17,chara=’q’;

//聲明一個指針變量

char* p =&aChar;  // p = 145600

18,用地址輸出a 的值

  Int a =5;

  Int* p=&a;

  Int** q=&p;

  Int* **r=&q;

scanf(“%d”, &a);

scanf(“%d”, p);

scanf(“%d”, *q);

scanf(“%d”, **r);

 

19,

For example:

char c;

char *pc;

int a;

int *pa;

double x ;

double *px;

Print

sizeof(c)= 1 sizeof(pc)= 4 sizeof(*pc)= 1

sizeof(a)= 4 sizeof(pa)= 4 sizeof(*pa)= 4

sizeof(x)= 8 sizeof(px)= 4 sizeof(*px)= 8

 

20,練習代碼:

 //day03.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <math.h>

//int hp(int a,int b);

//void replace1(int c,int d);

//void change(int* a,int* b);

//int square(int i);

//float square(int f);

//int* smaller(int* p1,int* p2);

void replace(int** ,int**);

void b(int a[]);

int main(int argc, char* argv[])

{

   //int  i= hp(1.0,2.0);

 

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

 

/** int c=3;

 float d=4.0f;

 //replace1(c,d);

int i=  square(c);

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

 float f= square(c);

 printf("%f\n",f);

 **/

    /**

    inta=5;

    intb;

    int*p1=&a;

    int*p2=&b;

    int**p3=&p2;

    int***p4=&p3;

    floatx=12;

    scanf("%d",&b);

    scanf("%d",p2);

        scanf("%d",*p3);

            scanf("%d",**p4);

    printf("%d\n%d\n%d\n%d\n",b,*p2,**p3,***p4);

 

**/

 

 

 

// printf("%f\n",x);

 

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

//int* p=0;

//p=smaller(p1,p2);

//printf("%d\n",*p);

/**

 

int a=1;

int b=2;

int* p1=&a;

int* p2=&b;

 

int** p3=&p1;

int** p4=&p2;

 

replace(p3,p4);

 

printf("%d\n",*p2);

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

**/

 

    inta[]={1,2,5,6,7};

 

  b(a);

 

  printf("%d\n",a[0]);

    return0;

}

 

void replace(int** p3,int** p4){

int*p;

p=*p3;

*p3=*p4;

*p4=p;

 

}

void b(int a[]){

 

a[0]=5;

}

//int* smaller(int* p1,int* p2){

 

// return(*p1<*p2?p1:p2);

 

//}

 

 

/** void change(int* a,int* b){

int temp=0;

 

temp=*a;

*a=*b;

*b=temp;

 

}

 

 

int square(int i)

{ return i * i; }

 

float square(int f)

{

    return((float)f);

 }

 

 

//int hp(int a,int b){

 

  //return (sqrt(a*a+b*b));

 

//}

 

void replace1(int c,int d){

 

    inttemp=0;

    temp=c;

    c=d;

    d=temp;

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

 

}

**/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章