C语言学习1

C语言笔记1

第一课 class 1

C is a language that faces processes.

It can recognize capital or not.

1.Install Dev-C++ (we can also use vs code)

2.Show page like this:

UA3EAs.png

*‘Include’ is a word that has been definited, we can’t use it as variable again.

*Content after ‘include’ is the stock of C

Inside ‘/\ */‘ is the annotation(注释).

*Another way to show annotation is after ‘//‘

The difference between ‘/\ */‘ and ‘//‘ : ‘/* */‘ can include paragraphs, but ‘//‘ can only include one line.

The blue one means the annotation.

UAGlk9.png

*‘Main’ is the entre of the function and inside the ‘ { } ’ is the body of the function. There is only one main function at anywhere.

*After every sentence, we should add ‘;’

3.Then we can write some simple code and click the compile(the red circle one) to check whether there is a grammatic error:

UAYFx0.png

*’Printf’ means to print/show the content inside the (). It won’t change to a new line unless ‘\n’ is added.

*Content in “ “ are considered as string, and printed out.

*’\n’ means to change to another line.

  1. After clicking compile: if there is an error, it will show you where it is; if there isn’t an error, it will create an exe file in the same file as the c file.

    We can also use the exe file to excute the code.

  1. If there is nothing wrong we can click the button next to compile(on the right), the ‘run’ button. cmd will appear and the content after printf will be shown.
  1. UA0o9g.png

    “%d” means the integer(s) behind the ‘,’ will be at the postion of “%d” when excuting.

    We need to identify the variables first and give them values. Then calculating,and ouput.

    UADoOs.jpg

  1. “scanf()” input values
1
scanf("%d,%d",&a,&b); //input numbers are separated by ','

add “”in””string:

example: “Please input “yes””

1
printf("Please input \"yes\"\n");

function in function:

1
2
3
4
5
6
7
8
9
10
max(a,b);



int max(int x,int y) //int means input is integer; if there is no requirement, use "void"
{int z;
if(x>y) z=x;
else z=y;
return z;
}

8.characters that can be used:

abcde… …z

ABCDE… …Z

0~9

+-*/=,.:;?“ ‘~|&^!%#(){}[]<> (space) (tab)

保留字(words that have been identified): (32)

type:int, long, float, … …

sentences: if…else, while, for, … …

store: auto, static,… …

calculation: sizeof

UAor6I.jpg

*put int in char, it will show the character through ASCII

*put char in int, it will show the value of ASCII

*if the integer is positive, it is stored as true form; if it is negative, it is stored as complement form.

*initialization:

1
2
int a=5;
in b=10,c=10,d=10; //not b=c=d=10

home exercise:guess number(simple form)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int n=10,g;
printf("guess a number from 0 to 100.\n");
scanf("%d",&g);
while(g!=n)
{
if (g>n) printf("It is too large!\n");
else printf("It is too small!\n");
scanf("%d",&g);
}
printf("You are right!\n");
}

out:

guess a number from 0 to 100.
50
It is too large!
25
It is too large!
12
It is too large!
6
It is too small!
8
It is too small!
9
It is too small!
10
You are right!


Process exited after 20.56 seconds with return value 0

第二课 class 2

1.’ ‘内加字符,” “加输入输出

2.int * i –>*指针

3.a=b: = 赋值 assignin

4.初始化赋值 initialization:int a=0;

5.sizeof :看字节数

6.输出输入(输入格式以scanf后引号内为准,&:取地址)

e.g. the input values a b are divided by “,”

1
scanf("%d,%d",&a,&b);

7.测试testing standard:正确数据 correct one,临界值 the boundary,错误数据 wrong one

const:定义常量 constant,不可改值 can’t be change,一般大写常量capital,在内存里有空间exist in memory

#define:用途同const,在内存内无空间 doesn’t exist in memory

8.

类型名称 类型说明符 字节数 数值范围
基本整型 int 2 -32768~32767
短整型 short [int] 2 -32768~32767
长整型 long [int] 4 -2147483648~ 2147483647
无符号基本整型 unsigned [int] 2 0~65535
无符号短整型 unsigned short [int] 2 0~65535
无符号长整型 unsigned long [int] 4 0~4294967295
运算符 运算规则 操作数数目 优先级 结合方向
负号 单目 2 右结合
加法 双目 4 左结合
减法 双目 4 左结合
乘法 双目 3 左结合
除法 双目 3 左结合
求余或模 双目 3 左结合

同级:左集合,从左往右:d=3*5/4

d=a=3:优先级先右后左

整数相除没有余数,截取整数:1/3+1/3+1/3=0

浮点数正常计算

10/33=10,10.0/3\3=10.0

9.自增自减:

x=10,y=++x,y=11,x=11

x=10,y=x++,y=10,x=11

i=6

++i=7,i=7

i++=7,i=8

a=–i,a=7,i=7

b=i–,b=7,i=6

-i++=-6,i=7

10.强制转换:(int)(x+y) (有损失some value will be lost)

11.关系运算(1/0表示真假)

5>(4<5) 可以运行4<5值为1(真),5>1

==比=优先级高

1、圆括号【()】、下标运算符【[]】、分量运算符的指向结构体成员运算符【->】、结构体成员运算符【.】;

2、逻辑非运算符【!】、按位取反运算符【~】、自增自减运算符【++】【 –】、负号运算符【-】、类型转换运算符【(类型)】、指针运算符和取地址运算符【*】【&】、长度运算符【sizeof】;

3、乘法运算符【\】、除法运算符【/】、取余运算符【%】;

4、加法运算符【+】、减法运算符【-】;

5、左移动运算符【<<】、右移动运算符【>>】;

6、关系运算符【< 】【>】【<=】【 >= 】;

7、等于运算符【==】、不等于运算符【!=】;

8、按位与运算符【&】;

9、按位异或运算符【^】;

10、按位或运算符【|】;

11、逻辑与运算符【&&】;

12、逻辑或运算符【||】;

13、条件运算符【?:】;

14、赋值运算符【=】【/=】【*=】【%=】【+=】【-=】【<<=】【>>=】【&=】【^=】【|=】;

15、逗号运算符【,】。

逻辑运算:0<x<5 变为 x>0&&x<5, &&为且

非:!(非0取反为0,0取反为1),与:&&,或:||

当计算一边值可确定真假时,另一边不会计算

表达式1 ? 表达式2 :表达式3 = if 表达式1 表达式2 else 表达式3 (可嵌套)

运算符:=

复合运算(从右往左):+=,-=,*=,/=,%= (a+=b—a=a+b)

,表达式

全部计算,最后一个表达式为整个式子的值

e.g. n=a=5+3,a+10,a*8

getchar():输入字符

putchar():输出字符