C语言学习笔记—1
C语言都是从一行“hello,world!”开始。
我们需要将这行代码打印到控制台。
我先将这个段打印代码写出来:
#include<stdio.h>
int main(int argc , char ** argv)
{
printf("hello,world!\n");
return 0;
}
显示的结果是:
hello,world!
现在说下我的一步步操作,充满了坎坷……(现在看起来很菜,但是有提高就行!)
(1)首先我在根目录下建立了一个专门用于学习C的文件夹,起名为c。
wubo@ubuntu:~$ mkdir c
wubo@ubuntu:~$ ls
1.txtcd c Documents examples.desktop Pictures Templates Videos
2.txt Desktop Downloads Music Public user
(2)我在这个c文件夹下使用vi创建并编写了一个hello.c的源文件。
wubo@ubuntu:~$ mkdir c
wubo@ubuntu:~$ ls
1.txtcd c Documents examples.desktop Pictures Templates Videos
2.txt Desktop Downloads Music Public user
wubo@ubuntu:~$ cd c
wubo@ubuntu:~/c$ ls
wubo@ubuntu:~/c$ vi hello.c
wubo@ubuntu:~/c$ ls
hello.c
(3)我要用gcc hello.c命令进行编辑,发现ubuntu自身没有安gcc编辑器,但是给出了提示,我就用sudo apt-get install gcc命令安装了gcc编辑器。
wubo@ubuntu:~/c$ gcc hello.c
Command 'gcc' not found, but can be installed with:
sudo apt install gcc
wubo@ubuntu:~/c$ sudo apt-get install gcc
[sudo] password for wubo:
(4)安装好了gcc编译器,使用gcc hello.c进行了编辑,但是报错了。后来发现是自己在#include<stdio.h>
后面加上了{}。
#include<stdio.h>
{
}
这里是不需要加大括号的,函数下面才需要。
(5)改完后,还是没有达到我要的要求,后来发现这条代码:printf("hello,world!/n")
这个/n的方向写反了,因为在Linux中写绝对路径都是/,所有写成了这种,但是gcc编辑器中换行的符号是\n。
(6)我开始以为是因为这个/n要写到“”外面,我放到外面后发现报错了,后来才发现了应该写在“”内。并且应该使用\n!
这样就完成了这个程序,终于成功了!
现把代码粘出来,以警示:
wubo@ubuntu:~/c$ vi hello.c
wubo@ubuntu:~/c$ gcc hello.c
wubo@ubuntu:~/c$ ls
a.out hello.c
wubo@ubuntu:~/c$ ./a.out
hello,world!/nwubo@ubuntu:~/c$ vi hello.c
wubo@ubuntu:~/c$ gcc hello.c
hello.c: In function ‘main’:
hello.c:4:25: error: ‘n’ undeclared (first use in this function)
printf("hello,world!"/n);
^
hello.c:4:25: note: each undeclared identifier is reported only once for each function it appears in
wubo@ubuntu:~/c$ vi hello.c
wubo@ubuntu:~/c$ gcc hello.c
wubo@ubuntu:~/c$ ls
a.out hello.c
wubo@ubuntu:~/c$ ./a.out
hello,world!
其它基本的概念,列出如下:
(1)源文件
(2)头文件
(3)关键字
(4)注释
(5)符号
(6)变量
(7)函数
这些概念需要自己搞清楚,资料很多,就不在浪费时间。