本人本科两年,摆了两年,什么都没学,今年大三才认识到自己再这样下去了就完了,大三第一学期断断续续的学了一学期,才发现前面基础没学好后面什么都不会,啥都看不懂,今天放假第一天,从零开始学C语言,我是经朋友推荐在B站看鱼C-小甲鱼视屏学的,今天学了第一个程序和打印,下面是课堂练习以及课后测试
第一个程序
#include <stdio.h>
int main()
{
printf("Hello world!\n");
return 0;
}
课后作业
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(const char *path);
int countLines(const char *filename)
{
FILE *fp;
int count = 0;
int temp;
if ((fp = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "Can not open the file:%s\n", filename);
return 0;
}
while ((temp = fgetc(fp)) != EOF)
{
if (temp == '\n')
{
count++;
}
}
fclose(fp);
return count;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath[MAX], target[MAX];
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
{
do
{
sprintf(target, "%s/%s", path, fa.name);
total += countLines(target);
}while (_findnext(handle, &fa) == 0);
}
_findclose(handle);
}
void findALLDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath[MAX];
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
{
fprintf(stderr, "The path %s is wrong!\n",path);
return;
}
do
{
if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
continue;
if( fa.attrib == _A_SUBDIR)
{
sprintf(thePath, "%s/%s", path, fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
}
}while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path[MAX] = ".";
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %ld 行代码!\n\n", total);
system("pause");
return 0;
}
这个打了好长时间
总结:CPU的唯一认识的语言是机器语言;
C语言源代码转换为汇编语言的过程称为编译 。编译是将高级编程语言的源代码翻译成汇编语言或直接转换为机器语言的过程;
编译型语言的关键特点是代码在运行前经过编译成机器码或字节码,因此执行速度较快,但在不同平台上需要重新编译为适应该平台的可执行文件。
解释型语言在运行时直接解释执行源代码,不需要生成独立的可执行文件,具有跨平台的优势,但通常执行速度较慢。
打印
printf()中()里面的字符串就是要编译运行打印的东西,字符串分为可见字符和转义字符。
下面是常用的转义字符:
转义字符
\a 响铃(BEL)
\b 退格(BS),将当前位置移到前一列
\f 换页(FF),将当前位置移到下页开头
\n 换行(LF),将当前位置移到下一行开头
\r 回车(CR),将当前位置移到本行开头
\t 水平制表 (HT),跳到下一个TAB位置
\v 垂直制表(VT)
\\ 表示反斜杠本身(\)
\' 表示单引号(')
\" 表示双引号(")
\? 表示问号(?)
\0 表示空字符(NULL)
\ddd 1到3位八进制数所代表的任意字待
\xhh 1到2位十六进制所代表的任意字符
课堂作业:
#include <stdio.h>
int main()
{
printf(" * \n");
printf(" / \ \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * * * * * \n");
printf(" * * * * * * * * \n");
printf(" * * * * * * * * * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * * * \n");
printf(" * * * * * * \n");
return 0;
}