1第一个c语言的hello world1.1include头文件包含

头文件包含,写法#include<文件名>,

1.2main函数

这个就是C语言程序的入口,所有的C程序都是从main开始执行,一个C的源程序必须有一个main函数,也只能有一个main函数

1.3注释

//注释一行

/* */代表块注释,可以注释多行代码

1.4{}括号和代码块

代表一个代码单元

1.5声明

C语言规定,所有的变量和函数必须先声明,然后才能使用.

1.6C语言自定义名字的要求

可以使用大小写字母,下划线,数字,但第一个字母必须是字母或者下划线

字母区分大小写

变量名最好用英文,而且要有所含义,通过变量的名称就能猜测变量的意思。

1.7return语句

在C语言当中任何函数遇到return代表这个函数停止,当main函数遇到return,代表整个程序退出

return代表函数的返回值,如果返回类型是void,可以直接写return,而不需要返回任何值

2C语言的编译2.1编译过程


2.2gcc编译选项

-o代表指定输出文件名

-E代表预编译

预编译处理include的本质就是简单的将include中的文件替换到c文件中

如果include包含的头文件在系统目录下,那么就用#include <>,如果包含的文件在当前目录下,那么用#inlclude“”

-S代表汇编

-c代表编译

Linux C 学习

1,编写一个helloworld 程序

vimhello.c#include"stdio.h"intmain(){printf("HelloWorld!\n\n");return0;}


一步到位编译执行

chunli@pc0003:/tmp/C$gcchelloworld.Cchunli@pc0003:/tmp/C$./a.outHelloWorld!


C编译过程

chunli@pc0003:/tmp/C$gcc--helpUsage:gcc[options]file...Options:-pass-exit-codesExitwithhighesterrorcodefromaphase--helpDisplaythisinformation--target-helpDisplaytargetspecificcommandlineoptions--help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]Displayspecifictypesofcommandlineoptions(Use'-v--help'todisplaycommandlineoptionsofsub-processes)--versionDisplaycompilerversioninformation-dumpspecsDisplayallofthebuiltinspecstrings-dumpversionDisplaytheversionofthecompiler-dumpmachineDisplaythecompiler'stargetprocessor-print-search-dirsDisplaythedirectoriesinthecompiler'ssearchpath-print-libgcc-file-nameDisplaythenameofthecompiler'scompanionlibrary-print-file-name=<lib>Displaythefullpathtolibrary<lib>-print-prog-name=<prog>Displaythefullpathtocompilercomponent<prog>-print-multiarchDisplaythetarget'snormalizedGNUtriplet,usedasacomponentinthelibrarypath-print-multi-directoryDisplaytherootdirectoryforversionsoflibgcc-print-multi-libDisplaythemappingbetweencommandlineoptionsandmultiplelibrarysearchdirectories-print-multi-os-directoryDisplaytherelativepathtoOSlibraries-print-sysrootDisplaythetargetlibrariesdirectory-print-sysroot-headers-suffixDisplaythesysrootsuffixusedtofindheaders-Wa,<options>Passcomma-separated<options>ontotheassembler-Wp,<options>Passcomma-separated<options>ontothepreprocessor-Wl,<options>Passcomma-separated<options>ontothelinker-Xassembler<arg>Pass<arg>ontotheassembler-Xpreprocessor<arg>Pass<arg>ontothepreprocessor-Xlinker<arg>Pass<arg>ontothelinker-save-tempsDonotdeleteintermediatefiles-save-temps=<arg>Donotdeleteintermediatefiles-no-canonical-prefixesDonotcanonicalizepathswhenbuildingrelativeprefixestoothergcccomponents-pipeUsepipesratherthanintermediatefiles-timeTimetheexecutionofeachsubprocess-specs=<file>Overridebuilt-inspecswiththecontentsof<file>-std=<standard>Assumethattheinputsourcesarefor<standard>--sysroot=<directory>Use<directory>astherootdirectoryforheadersandlibraries-B<directory>Add<directory>tothecompiler'ssearchpaths-vDisplaytheprogramsinvokedbythecompiler-###Like-vbutoptionsquotedandcommandsnotexecuted-EPreprocessonly;donotcompile,assembleorlink-SCompileonly;donotassembleorlink-cCompileandassemble,butdonotlink-o<file>Placetheoutputinto<file>-pieCreateapositionindependentexecutable-sharedCreateasharedlibrary-x<language>SpecifythelanguageofthefollowinginputfilesPermissiblelanguagesinclude:cc++assemblernone'none'meansreverttothedefaultbehaviorofguessingthelanguagebasedonthefile'sextensionOptionsstartingwith-g,-f,-m,-O,-W,or--paramareautomaticallypassedontothevarioussub-processesinvokedbygcc.Inordertopassotheroptionsontotheseprocessesthe-W<letter>optionsmustbeused.Forbugreportinginstructions,pleasesee:<file:///usr/share/doc/gcc-4.8/README.Bugs>.


源C代码程序 hello.c

第一步:预编译,把include文件的内容原封不动的放到源代码中 gcc -o hello.i hello.c

第二步:汇编,把预编译的结果变成汇编代码

第三步:编译,把汇编的结果变成二进制文件

第四步: 链接,把编译的二进制文件与系统库连接起来

chunli@pc0003:/tmp/C$gcc-ohello.i-Ehello.cchunli@pc0003:/tmp/C$gcc-ohello.s-Shello.cchunli@pc0003:/tmp/C$gcc-ohello.o-chello.schunli@pc0003:/tmp/C$gcc-ohellohello.ochunli@pc0003:/tmp/C$./helloHelloWorld!


查看链接的库

chunli@pc0003:/tmp/C$lddhellolinux-vdso.so.1=>(0x00007fff217f8000)libc.so.6=>/lib/x86_64-linux-gnu/libc.so.6(0x00007f5340914000)/lib64/ld-linux-x86-64.so.2(0x00005654d9706000)


调用系统的程序

vimhello.c#include"stdio.h"#include"stdlib.h"intmain(){system("cathello.c");printf("HelloWorld!\n\n");return0;}


编译gcc hello.c

执行 ./a.out

输出:

#include"stdio.h"#include"stdlib.h"intmain(){system("cathello.c");printf("HelloWorld!\n\n");return0;}HelloWorld!


2.3printf执行原理


向屏幕输出的其他方式:

chunli@pc0003:/tmp/C$catmy_printf.c#include<stdio.h>#include<stdlib.h>#include<unistd.h>intmain(){inti=99;printf("i=%d\n",i/0x10);fwrite("abc\n",1,2,stdout);//write("abc\n",4,STDOUT_FILENO,"abc");return;}



C 语言版计算器

#include<stdio.h>#include<stdlib.h>intmain(intargc,char*args[]){if(argc<3)printf("请输入两个整数!\n");else{inta=atoi(args[1]);intb=atoi(args[2]);intc=a+b;printf("两个数的和是%d\n",c);}return0;}


使用方法

chunli@pc0003:/tmp/C$!gccgcccalc.cchunli@pc0003:/tmp/C$./a.out33两个数的和是6chunli@pc0003:/t