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;
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);
}
**/