C精华学习教程.pptx
《C精华学习教程.pptx》由会员分享,可在线阅读,更多相关《C精华学习教程.pptx(54页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、How to write a programFind the whole steps in the real modelUse some graph or nature language to describeRealize with computer language第1页/共54页workflow第2页/共54页Pseudo CodeA first idea:int main()variableswhile(condition)analyze the expressionevaluate the expressionprint the result3 3第3页/共54页main Funct
2、ionint main(void)return 0;cout“Hello,Tom!”;int x=5;int y=8;int z=x+y;coutx“+”y“=“zendl;4第4页/共54页/2.1.cpp#include using namespace std;/*A simple program for demonstrating the basics of a C+project.*It does a good job of demonstrating C+fundamentals,but a*terrible job with the poetry.*/int main()cout
3、Dont you just feel like a louse;cout endl;cout To learn that your new theorem was proved by Gauss?;cout endl;return 0;5第5页/共54页cout expressionscout“hello,world!”;cout 5;cout x;cout x+5;cout x“+5=”x+5;”n”,“t”,“,endl,setw(n)setw(n):#include cout6第6页/共54页C+Tokens A token is the smallest element of a C+
4、program that is meaningful to the compiler.Kinds of tokens:identifiers,keywords,literals,operators,punctuators,and other separators.Tokens are usually separated by white space.White space can be one or more blanks,horizontal or vertical tabs,new lines,form feeds or comments.7第7页/共54页C+Keywords auto
5、const double float int short struct unsigned unsignedbreak break continue elsefor long switch void case sizeoftypedef char do if returnstatic union while,ETC.8第8页/共54页Commenting/*name of program*information of author*function of program*/a sampleint main()/*this is in the comment this is also in the
6、 comment*/.9第9页/共54页Constants 1,2,31.2,4.50“name”,“your_phonenumber”ture,false0 x121,A,$,xhh,ddd#define PI 3.141592#define PRICE 100const int pi=3.141592;10第10页/共54页/2.2.cpp#include using namespace std;int main()int x;int y;x=3;y=4;cout x+y endl;return 0;11第11页/共54页Variables TypesBuilt-in typesBoole
7、an typebool 1byteCharacter typeschar 1byteInteger typesint 2-4bytes(2)-3276832767short(2)&long(4)-21474836482147483647Floating-point typesdouble 8bytesfloat 4bytes121 byte(8 bits)2 bytes(16 bits)4 bytes(32 bits)8 bytes(64 bits)16 bytes(128 bits)shortcharboolintfloatlongdoublelong double第12页/共54页Vari
8、ables LiteralsBoolean literals:bool t;true,falseCharacter literals:char c;a,x,4,n,$Integer literals:int x;0,1,123,-6,0 x34,0 xa3Floating point literals:double d;float f;1.2,13.345,.3,-0.54,1.2e3,.3F,.3FString literals:string s;asdf,Howdy,all yall!”13第13页/共54页Variables NamesChoose meaningful namescon
9、fusemtbf,TLA,myw,nbvShort names can be meaningfulx is a local variablei is a loop indexDont use long namesOk:partial_sum,element_count,staple_partitionToo long:the_number_of_elementsremaining_free_slots_in_the_symbol_table14第14页/共54页Not Variables NamesA name in a C+programStarts with a letter,contai
10、ns letters,digits,and underscores(only)x,number_of_elements,Fourier_transform,z2Not names:12x,time$to$market,main lineNot start names with underscores:_fooNot use keywordsintifwhile15第15页/共54页Declaration and initializationint a=7;int b=9;char c=a;double x=1.2;string s1=Hello,world;string s2=1.2;169a
11、1.2 13 Hello,world4|1.2b:c:x:s1:s2:7a:|第16页/共54页Constant variablesconst int i=5;i=6;/error17第17页/共54页Think about:int a,b,c=2;int x,y,z,10;int m=2;int n=3;long int sum=0,add;long hello;char a=m;char b,c,d;char m=65,n=a+1;float a,b,ccc=3.1415;float sum=0.0;double f1,f2=1.414e1218第18页/共54页Assignment an
12、d incrementint a=7;a=9;a=a+a;a+=2;+a;1979182021a:第19页/共54页Think aboutint a=10,b;b=a;float x;int k=300;x=k;float x=3.14;int n;n=x+6;float x=3.14;int n;n=3;cout x+n;3.0/9 or (float)3/9第20页/共54页/2.3.cpp A program to illustrate integer overflow#include using namespace std;/*A program to illustrate what
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 精华 学习 教程
限制150内