从零开始学C语言

4 篇文章 0 订阅
2 篇文章 0 订阅

前言:

             作为一位编程新手、一把年纪了,最后还要来学习自己唯一感兴趣的专业:计算机科学与应用。感觉自己是比别人慢长了好几年,但是时间有限,还是从c语言入手,开始登封造极的旅程吧。

《C程序设计语言(第二版)》

第一章

1.1入门

        在上下两行添加“”可以实现上下连接

#include <stdio.h>

int main()
{
	printf("成功了"
		"");
	return 0;
}
#include <stdio.h>

int main()
{
	printf("成功"
		"好");
	return 0;
}

引号""格式对称,但是不用太过在乎括号的形式。

#include <stdio.h>

int main()
{
	printf("ff\n"     #换行
		"ff\t"        #水平制表符
		"ff\r"        #回车
		"ff\b"        #退格符
		"ff\f"        #换页符
		"ff\\"        #反斜杠
		"ff\'"        #单引号
		"ff\""        #双引号
		"ff\0"        #空
		"ff\a"        #响铃符
		"ff\v"        #垂直表符
		"ff\255"      #八进制符
		"ff\A123");    #十六进制符
	return 0;
}
输出结果

ff
fff     ff
ff\ff'ff"ff
1.2算术表达式

思考问题:

1,优化温度表CODE

2,算法表达式优先级

3,32678是不是4的12次方,32位的INT取值范围又是多少?

4,while的结构体直观图片

5,结合int计算中的特性使用float会有什么不同?

6,因为可以指定宽度,那么其他的转义字符是成倍的嘛?

重要性质:浮点数的转换优先级比整数型高 

开始解决问题:

1,for循环,省略三个int数据;

#include <stdio.h>

int main()
{
	float fahr=0, celsius;

	for (float i = 0; i <= 300; i += 20)
	{
		fahr = i;
		celsius = (5.0 / 9.0) * (fahr - 32.0);
		printf("%3.0f\t%6.1f\n", fahr, celsius);
	}
}

输出结果:

  0      -17.8
 20       -6.7
 40        4.4
 60       15.6
 80       26.7
100       37.8
120       48.9
140       60.0
160       71.1
180       82.2
200       93.3
220      104.4
240      115.6
260      126.7
280      137.8
300      148.9

2, 优先级从下至上依次递减

1逗号运算符,从左到右
2赋值运算符=、+=、-=、*=、/=、 %=、 >=、 <<=、&=、^=、|=从右到左
3逻辑或||从左到右
4逻辑与&&从左到右
5按位或|从左到右
6按位异或^从左到右
7按位与&从左到右
8相等/不等==、!=从左到右
9关系运算符<、<=、>、>=从左到右
10位移运算符<<、>>从左到右
11加法/减法+、-从左到右
12乘法/除法/取余*(乘号)、/、%从左到右
13单目运算符!、*(指针)、& 、++、–、+(正号)、-(负号)从右到左
14后缀运算符( )、[ ]、->从左到右

 3,32768是4×2的14次方,就是2的15次方1024*128。所以不是,真是不给面子居然是2的32次方42949~5,

4,

5,不同就在于可以直接使用5/9计算结果也得以保证。

6,不是的,转义字符没有这个性质

!!!我的天CSDN挂在后台直接死机了我都想不起来我要干什么了~我的思想源泉呜呜呜~~

1.3 for语句

1,尝试再次简化之前的温度表

#include <stdio.h>

main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr = fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

2,练习1-5

#include <stdio.h>

int main()
{
    int fahr;
    for (fahr = 300; fahr >= 0; fahr = fahr - 20)
    printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
    return 0;
}

输出结果:
300  148.9
280  137.8
260  126.7
240  115.6
220  104.4
200   93.3
180   82.2
160   71.1
140   60.0
120   48.9
100   37.8
 80   26.7
 60   15.6
 40    4.4
 20   -6.7
  0  -17.8

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 11004) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

1.4. 符号常量

问题思考:

1,如果有两个#define,他们的内存地址如何?

2,define这种全局变量一般很少用到,因为什么?

解决问题:

1,

在C或C++中,#define是预处理指令,用于创建符号常量或宏。它们并不占用任何内存空间。所以,我们不能讨论它们的"内存地址",因为它们并没有内存地址。当你使用#define定义一个标识符时,预处理器会在编译之前将所有该标识符的实例替换为定义的值或表达式。

2,

全局变量在编程中通常被认为是一种不良的编程实践,原因如下:

    命名冲突:全局变量在整个程序范围内都是可见的,这意味着任何函数或模块都可以修改它们。这可能导致命名冲突和意外的行为,特别是当多个开发人员在同一个项目中工作时。

    难以维护:全局变量使得代码更难理解和维护,因为它们可以在任何地方被修改。这使得跟踪变量值的变化变得困难,也增加了出错的可能性。

    可测试性降低:全局变量使得编写单元测试变得更加困难,因为测试可能需要设置特定的初始状态或清理全局状态以进行下一个测试。

    并发问题:在多线程环境中,全局变量可能导致数据竞争和不一致的状态。为了避免这些问题,需要使用同步机制(如互斥锁),这会增加复杂性和性能开销。

    隐藏依赖关系:全局变量可能导致隐藏的依赖关系,使得代码之间的耦合度增加。这可能导致代码重用性降低,以及在更改一个模块时可能影响到其他模块。

1.5. 字符输入/输出

思考问题:

1,putchar是不是所有类型数据都可以进行返回?

#include <stdio.h>

int main()
{
	int i = 10;
	char c = 'c';
	putchar(c);
	putchar(i);
}


返回结果是:
c

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 18764) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .


上方的空格其实就是 ASCII码中10所对应的'\n'__也就是空行。

说明,它确实只是输出一个字符,并且如果进入数字也不过是通过ASCII标准转化成字符。

1.5.1 文件复制

思考问题:

1,练习1-6,练习1-7

2,用getchar输入A、B、G三个字符,各自的地址在哪里?数字又如何?

解决问题:

1,

#这道题居然问我getchar() != EOF这个表达式是 1还是0,(⊙﹏⊙)nnn我用了条件表达式验证了下
#include <stdio.h>

int main()
{
	if (getchar() != EOF)
		printf("1");
	else if (getchar() == EOF)
		printf("0");
}

#输出结果:
1
1
#下面式CRTL+Z不清楚为什么出现了两个
^Z
^Z
0
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 6684) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

#include <stdio.h>

int main()
{
	putchar(EOF);
}

#输出结果:

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 20300) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

 2.

#include <stdio.h>

int main()
{
	char a = getchar();
	char b = getchar();
	char g = getchar();
   
	printf("%d\n%d\n%d", &a, &b, &g);
}

结果:
A B G
-1521486268
-1521486236
-1521486204
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 3904) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

#每一次申请地址都是不同的,但是都是32的进位,说明了,它的申请是连续的,但是getchar()并非如此,考虑到getchar()是一个函数,而且是一个形式参数对他寻址没有任何意义,不过看起来对于变量的赋值在同一连续顺序处理上基本来说内存地址的申请是连续的,有时候也会不连续。

B G
1934620340
1934620372
1934620404
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 1436) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

#include <stdio.h>

int main()
{
	char a = 'A'
	char b = getchar();
	char g = getchar();
   
	printf("%d\n%d\n%d", &a, &b, &g);
}

1.5.2 字符计算

思考问题:

1,整理下C语言中的数据类型

2,画一个程序赋值的直观图

解决问题:

1,

2,

1.5.3 行计数

1,文章中提到,A在ASCII标准中值为65,如果我用getchar()输入A再用加一输出的putchar会是B吗?

2,课本练习1-8 1-9 1-10

3,更有问题的是,单引号下的\n也有值是10,那么为什么我在终端输入10,而输出不是\n

解决问题:

1,回答:是的

#include <stdio.h>

int main()
{
	int cat = getchar();
	int cats = cat + 1;
	putchar(cats);
}


输出结果:
A
B
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 21668) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

 2, 比较有意思:制表符其实只算作一个字符;

#include <stdio.h>

int main()
{
	int space = 0;
	int t = 0;
	int n = 0;

	char cats[20];
	for (int i = 0; i<20; i++)
	{
		cats[i] = getchar();
	}

	for (int i =0; cats[i] != EOF; i++)
	{
		if (cats[i] == '\n')
			n++;
		else if (cats[i] == '\t')
			t++;
		else if (cats[i] == ' ')
			space++;
	}

	printf("空格:%d,制表符:%d,换行符:%d", space, t, n);
	return 0;
}#include <stdio.h>

#比较有意思:制表符其实只算作一个字符#
输出结果:
        t space
n
n
n
n
n
n
空格:1,制表符:1,换行符:6
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 4064) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

#include <stdio.h>

int main()
{	
	int n=0;
	int t=0;
	int space=0;
	char e = '0';
	char cats[20];
	for (int i = 1; i;)
	{
		for (int i = 0; i < 20; i++)
		{
			cats[i] = getchar();
		}
		
		for (int i = 0; i < 20; i++)
		{
			putchar(cats[i]);
		}

		printf("输入0结束");
		e = getchar();
		if (e  ==  '0')
			break;
	}


	for (int i = 0; cats[i] != EOF; i++)
	{
		if (cats[i] == '\n')
			n++;
		else if (cats[i] == '\t')
			t++;
		else if (cats[i] == ' ')
			space++;
	}

	printf("空格:%d,制表符:%d,换行符:%d", space, t, n);
	return 0;
}

##注意换行符##

输出结果:

123456789  12345678
123456789  12345678
输入0结束0
空格:2,制表符:0,换行符:1
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 14216) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

#include <stdio.h>

int main()
{	

	char e = '0';
	char cats[20];
	for (int i = 1; i;)
	{
		for (int i = 0; i < 20; i++)
		{
			cats[i] = getchar();
		}
		
		for (int i = 0; i < 20; i++)
		{
		if (cats[i] == '\\')
				printf("\\");
				else if (cats[i] == '\t')
					printf("\t");
				else if (cats[i] == '\b')
					printf("\b");
			putchar(cats[i]);
		}

		printf("输入0结束");
		e = getchar();
		if (e  ==  '0')
			break;
	}
	return 0;
}


输出结果:


\\\\\\\\\  12345\\

\\\\\\\\\\\\\\\\\\  12345\\\\

输入0结束0

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 16788) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

3.\n的值为10是因为ASCII码本身的规定,而getchar()是输入一个字符,上限也不过是1,如果一定要将10转成\n那可以试试加1;

#include <stdio.h>

int main()
{	

	char CA;
	CA = getchar();
	putchar(CA - '0');

}


输出结果:

10

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 22352) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

 看起来getchar不光只是单独的数字,但是char cats[]中的字符是单个的。

1.5.4 单词计数

1,课本中的程序有些问题,Out作为0,如果按照自上向下的逻辑规则,那么应该不能在让nw++了,测试下这个程序

解决问题:

1,解决

#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{	
	int c, n1, nw, nc, state;

	state = OUT;
	n1 = nw = nc = 0;
	while((c = getchar()) != EOF)
		{
		++nc;
		if (c == '\n')
			++n1;
		if (c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if (state == OUT)
			{
			state = IN;
			++nw;
			}
		}
	printf("%d %d %d\n", n1, nw, nc);
}

#确实是有问题的,nw在对于连续的数字只会输出1#

#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{	
	int c, n1, nw, nc, state;

	state = OUT;
	n1 = nw = nc = 0;
	while((c = getchar()) != EOF)
		{
		++nc;
		if (c == '\n')
			++n1;
		if (c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if (state == OUT)
			{
			++nw;
			}
		}
	printf("%d %d %d\n", n1, nw, nc);
}

#state = IN多余不管就行了#

2,课本练习

解决问题:

第一题:同上述所述

第二题:上述的数组复制便有(数组只会输入一个字符),其实我看不懂题目。

1.6 数组

问题思考:
1,这里面的程序在输入和输出很奇怪,令人匪夷所思,9300001居然会有123个空格,仔细查看下到底是什么情况,debug。

2,课本练习

解决问题:

1,

#include <stdio.h>
/* count digits, white space, others */
int main()
{
	int c, i, nwhite, nother;
	int ndigit[10];
	nwhite = nother = 0;
	for (i = 0; i < 10; ++i)
		ndigit[i] = 0;
	while ((c = getchar()) != EOF)
		if (c >= '0' && c <= '9')
			++ndigit[c - '0'];
		else if (c == ' ' || c == '\n' || c == '\t')
			++nwhite;
		else
			++nother;
	printf("digits =");
	for (i = 0; i < 10; ++i)
		printf(" %d", ndigit[i]);
	printf(", white space = %d, other = %d\n",nwhite, nother);
	return 0;
}

#输出结果#


0 0 0 0 0 0 0 0 0 1 1 1 9
^Z
digits = 9 3 0 0 0 0 0 0 0 1, white space = 13, other = 0

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 6608) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

问题没有,他是程序直接复制进去。

2,用二维数组绘制的

#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{
    int c, n1, nw, nc, state;
    char a[25][50];
    for (int i = 0; i < 25; i++)
    {
        for (int g = 0; g < 50; g++)
        {
            a[i][g] = ' ';
        }
    }

    state = OUT;
    n1 = nw = nc = 0;
    while (nc != 20)
    {
        c = getchar();
        ++nc;
        if (c == '\n')
            ++n1;
        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if (state == OUT)
        {
            state = IN;
            ++nw;
        }
    }

    for (int i = 0; i <25; i++)
    {
        if (i == 0)
            a[0][0] = 'y';
        else if (i < 24) {
            a[i][0] = '|';
        }
        else if (i == 24)
        {             
            a[24][49] = 'x';
            for (int g = 0; g < 49; g++)
            {
                a[24][g] = '_';
            }
        }

    }

    for (;0<n1;n1--)
    {
        a[25-n1][9] = '@';
    }
    for (;0<nw;nw--)
    {
        a[25-nw][19] = '#';
    }
    for (;0<nc;nc--)
    {
        a[25-nc][29] = '$';
    }

    for (int i = 0; i < 25; i++)
    {
        for (int g = 0; g < 50; g++)
        {
            printf("%c", a[i][g]);
        }
        printf("\n");
    }

    return 0;
}


输出结果


give    me fire
hahahahahaha
y
|
|
|
|
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                            $
|                  #         $
|                  #         $
|                  #         $
_________@_________#_________$___________________x

D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 8456) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
1.7 函数

1,课本练习

大作业 (时限一个星期),制作一个9*9的预处理乘法表,不能用到递归,可以防止内存溢出,并且提高效率,顺便学习下debug的方法。

1.8 参数——传值调用

1,制作思维图,分清C中的本地变量(到目前已知的情况)

解决问题:

        

1.9 字符数组

1,课题作业

#include <stdio.h>

#define _MAXLINE 20		//定义最大长度20

int getline(char line[], int maxline);
void copy(char to[], char from[]);

int main()
{
	int k;											//记录空行的个数
	int len;										//记录线长度
	int max;										//限制最大可复制字符
	char line[_MAXLINE];					//一个存放字符的数组
	char longest[_MAXLINE];			//最大字符长度
	max = 0;
	while((len = getline(line, _MAXLINE))>0)
		if (len > max) {
			k = len;
			max = len;
			copy(longest, line);
		}
	if (max > 0)			//检查max是否大于0否则没有输入就是返回0
	{
		printf("%s", longest);
		//for循环用于打印任意输入行的长度
		for (; k > 0; k--)
		{
			printf("\n%d", k);
		}
	}
	return 0;
}


//获取字符的函数
int getline(char s[], int lim)
{
	int i;
	char c;
	 
	for (i = 0; i < (lim - 1) && (c = getchar()) != EOF && c != '\n'; ++i)		//将错误和空行进行筛选
	{
		s[i] = c;
	}
	if(c=='\n'){			//如果结尾是空行就使那个位置上加入空行
		s[i] = c;
		++i;
	}
	s[i] = '\0';				//并且在结尾输入‘\0’
	return i;
}


输出结果:

123456 123    //输入




^Z
123456 123        //输出

11
10
9
8
7
6
5
4
3
2
1
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 23052) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
#include <stdio.h>

#define _MAXLINE 1000		//定义最大长度20

int getline(char line[], int maxline);
void copy(char to[], char from[]);

int main()
{
	int k;											//记录空行的个数
	int len;										//记录线长度
	int max;										//限制最大可复制字符
	char line[_MAXLINE];					//一个存放字符的数组
	char longest[_MAXLINE];			//最大字符长度
	max = 0;
	while((len = getline(line, _MAXLINE))>0)
		if (len > max) {
			k = len;
			max = len;
			copy(longest, line);
		}
	if (max > 0)			//检查max是否大于0否则没有输入就是返回0
	{
		printf("%s", longest);
		//for循环用于打印任意输入行的长度
		for (; k > 0; k--)
		{
			printf("\n%d", k);
		}
	}
	return 0;
}


//获取字符的函数
int getline(char s[], int lim)
{
	int i;
	char c;
	 
	for (i = 0; i < (lim - 1) && (c = getchar()) != EOF ; ++i)		//将错误进行筛选
	{
		s[i] = c;
	}
	if(c=='\n'){			//如果结尾是空行就使那个位置上加入空行
		s[i] = c;
		++i;
	}
	s[i] = '\0';				//并且在结尾输入‘\0’
	return i;
}


//将一个数组内的字符传到另一个字符数组
void copy(char to[], char from[])
{
	int i;
	
	i = 0;											//初始化下标i
	while ((to[i] = from[i]) != '\0')		//识别到字符'\0'则结束传递
		++i;
}


//打印大于80字符的程序

//输入
adsfsdaf sadfsadfsdfsdfsadfsdaf afsdaf sadf sad fsad fsdafsad fsadfsdfsadfasdf dfasd fas fsdf asdfsadfad
^Z
^Z
//输出结果
adsfsdaf sadfsadfsdfsdfsadfsdaf afsdaf sadf sad fsad fsdafsad fsadfsdfsadfasdf dfasd fas fsdf asdfsadfad

105
104
103
102
101
100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 14624) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

 

#include <stdio.h>

#define _MAXLINE 1000		//定义最大长度20

int getline(char line[], int maxline);
void copy(char to[], char from[]);

int main()
{
	int k;											//记录空行的个数
	int len;										//记录线长度
	int max;										//限制最大可复制字符
	char line[_MAXLINE];					//一个存放字符的数组
	char longest[_MAXLINE];			//最大字符长度
	max = 0;
	while((len = getline(line, _MAXLINE))>0)
		if (len > max) {
			k = len;
			max = len;
			copy(longest, line);
		}
	if (max > 0)			//检查max是否大于0否则没有输入就是返回0
	{
		printf("%s", longest);
		//for循环用于打印任意输入行的长度
		for (; k > 0; k--)
		{
			printf("\n%d", k);
		}
	}
	return 0;
}


//获取字符的函数
int getline(char s[], int lim)
{
	int i;
	char c;
	 
	for (i = 0; i < (lim - 1) && (c = getchar()) != EOF ; ++i)		//将错误进行筛选
	{
		if (c == '\n' || c == '\t' || c == ' ')			//删除不必要的制表符,空行空格
		{
			i--;
		}
		else
		s[i] = c;
	}
	if(c=='\n'){			//如果结尾是空行就使那个位置上加入空行
		s[i] = c;
		++i;
	}
	s[i] = '\0';				//并且在结尾输入‘\0’
	return i;
}


//将一个数组内的字符传到另一个字符数组
void copy(char to[], char from[])
{
	int i;
	
	i = 0;											//初始化下标i
	while ((to[i] = from[i]) != '\0')		//识别到字符'\0'则结束传递
		++i;
}



//输入
了了瀚海无处觅,寻      飞扬
波      万枯树  飞云端;

^Z
^Z
//输出结果
了了瀚海无处觅,寻飞扬波万枯树飞云端;
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
D:\System setting\Documents\C#\test1.0\x64\Debug\test1.0.exe (process 16204) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

 

#include <stdio.h>
#include <string.h>

#define _MAXLINE 20		//定义最大长度20

int getline(char line[], int maxline);
void copy(char to[], char from[]);
void change(char s[], char c[]);

int main()
{
	char t[_MAXLINE];						//用于颠倒的数组
	int k;											//记录空行的个数
	int len;										//记录线长度
	int max;										//限制最大可复制字符
	char line[_MAXLINE];					//一个存放字符的数组
	char longest[_MAXLINE];			//最大字符长度
	max = 0;
	
	while((len = getline(line, _MAXLINE))>0)
		if (len > max) {
			k = len;
			max = len;
			change(line, t);
			copy(longest, t);
		}
	if (max > 0)			//检查max是否大于0否则没有输入就是返回0
	{
		printf("%s", longest);
		//for循环用于打印任意输入行的长度
		for (; k > 0; k--)
		{
			printf("\n%d", k);
		}
	}
	return 0;
}


//获取字符的函数
int getline(char s[], int lim)
{
	int i;
	char c;
	 
	for (i = 0; i < (lim - 1) && (c = getchar()) != EOF; ++i)		//将错误进行筛选
	{
		if (c == '\n' || c == '\t' || c == ' ')			//删除不必要的制表符,空行
		{
			i--;
		}
		else
		s[i] = c;
	}
	i++;
	s[i]='\0';
	return i;
}
//颠倒函数
void change(char s[], char c[])
{
	int i =0;
	int len = strlen(s);
		for (i=0; i < len; i++)
		{
			c[i] = s[len-1-i];
		}
		c[len] = '\0';
}

//将一个数组内的字符传到另一个字符数组
void copy(char to[], char from[])
{
	int i;
	
	i = 0;											//初始化下标i
	while ((to[i] = from[i]) != '\0')		//识别到字符'\0'则结束传递
		++i;
}

//输入
我    爱    你,
我    喜欢    你

//输出结果
你喜欢我,你爱我

 没有用到reverse()函数相当于用main重写了一遍。

1.10 外部变量和作用域

        提出问题:

                1,外部变量和作用域的定义。

                 2,课题作业

        解决问题:

                1,外部变量通常指的是在函数或代码块之外声明的变量,它们的生命周期从声明开始到程序结束。这些变量可以在整个程序或文件的范围内被访问,因此有时也被称为全局变量。

                作用域是指一个变量或函数在程序中可以被访问的区域。作用域可以分为全局作用域、局部作用域、函数作用域等。

                2,课题作业

第二章 类型、运算符与表达式

2.1. 变量名

        提问:

                1,内部名与外部名的分类

                2,列程是什么

                3,ANSI标准保存了哪六个字符的唯一性

        2.2. 数据类型及长度

        提问:

                1, signed跟unsigned是限定取值范围的,那么它们各自的公式怎样

                2,练习

2.3. 常量

        提问:

                1,将long类型存入char类型的数组中最后一位还是L嘛???

                2,stelen()中的长度不包括'\0'试一下

                3,单引号跟双引号下的字母有什么不同

                4,几种常量类型?

                5,数据类型的检查是什么,又是如何检查的?

2.4. 声明

       提问:

                1,声明的顺序

                2,什么情况下未初始化的声明数据,不会是无效值?

                3,如果修改const的字符数组编译器会出现什么问题?

2.5. 算术运算符

        提问:

                1,归纳下算术表达符号

                2,查表2-1搬运下,并同时解决问题1

2.6. 关系运算符与逻辑运算符

        提问:

                1,赋值的运算符优先级为什么设置的这么低?

                2,课本练习(用逻辑非运算符便可),条件逻辑递归。

2.7. 关系运算符与逻辑运算符

        提问:

                1,isdigit()与tolower()是两个什么函数

                2,移植性跟signed和unsigned的关系

                3,将短的转换成长的,除了?

                4,doubule与float在实现的具体状况下有什么不同

                5,课本练习

                6,伪随机函数,并没有对数字有所谓的随机化处理,她是怎么做到的?

2.8. 自增运算符与自减运算符

        提问:

                1,C语言的变量定义是什么

                2,语言复杂度的定义是什么,a[++j]比起a[j];j++;更加的简洁,但是是否更加的复杂

                3,课本练习

2.9. 按位运算符

        提问:

                1,整型指的究竟是什么,float不能算是整型数吗?

                2,课本练习

2.10. 赋值运算符与表达式

        提问:

                1,二元赋值运算表达式比起一元赋值运算表达式会有优势?

                2,课本练习

2.11. 条件表达式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值