cornelledu Lectures MWF 1220110pm HLS 306 O64259ce Hours TBD httpwwwcscornelleducoursescs20222011sp CMS httpscmscsuglabcornelledu Email me your netid if you are not in CMS Introduction to C CS 2022 Spring 2011 Lecture 1 Administrivia Instructor Yin L ID: 6795
Download Pdf The PPT/PDF document "Introduction to C Introduction and Hello..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
AdministriviaIInstructor:YinLou,4144UpsonIEmail:yinlou@cs.cornell.eduILectures:MWF12:20-1:10pm,HLS306IOceHours:TBDIhttp://www.cs.cornell.edu/courses/cs2022/2011sp/ICMS:https://cms.csuglab.cornell.edu IEmailmeyournetidifyouarenotinCMS! IntroductiontoC CS2022,Spring2011,Lecture1 AdministriviaIInstructor:YinLou,4144UpsonIEmail:yinlou@cs.cornell.eduILectures:MWF12:20-1:10pm,HLS306IOceHours:TBDIhttp://www.cs.cornell.edu/courses/cs2022/2011sp/ICMS:https://cms.csuglab.cornell.edu IEmailmeyournetidifyouarenotinCMS! IntroductiontoC CS2022,Spring2011,Lecture1 GoalsICsyntaxIStandardlibrariesIProgrammingforrobustnessandspeedIUnderstandingcompiler IntroductiontoC CS2022,Spring2011,Lecture1 TopicsLect01:IntroductionandHelloWorldLect02:ControlFlowLect03:FunctionsandMakeLect04:PointersandArraysLect05:ComplexTypesLect06:MemoryModelLect07:PerformanceLect08:PreprocessorLect09:StandardInputandOutputLect10:FileandVariable-lengthArgumentListsLect11:ThreadsandSynchronizationLect12:ToBeaMasterProgrammer IntroductiontoC CS2022,Spring2011,Lecture1 EnvironmentIOS:GNU/LinuxIEditor:VimICompiler:gccIDebugger:gdb IntroductiontoC CS2022,Spring2011,Lecture1 IntroductiontoVimI$vimhello.cIi-EntereditingmodeIesc]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;-Enternormalmode MoreNormalModeCommands I:w-Write/SaveI:q-QuitI:q!-QuitwithoutsavingI:wq-WriteandQuite IntroductiontoC CS2022,Spring2011,Lecture1 IntroductiontoVim MoreCommands ISearchI/+pattern]TJ/;༖ ; .96;& T; 7.;݉ ; Td; [00;In-nextsearchmatchIN-previoussearchmatchIGotoI:+lineno]TJ/;༖ ; .96;& T; 7.;݉ ; Td; [00;Igg-GotorstlineIG-GotolastlineIDeleteLineIdd Google\vimcheatsheet"formore! IntroductiontoC CS2022,Spring2011,Lecture1 StructureofaCProgram OverallProgram somepre-processordirectives]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;globaldeclarations]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;globalvariables]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;functions]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0; Functions functionheader]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;localdeclarations]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;statements]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0; IntroductiontoC CS2022,Spring2011,Lecture1 StructureofaCProgram OverallProgram somepre-processordirectives]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;globaldeclarations]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;globalvariables]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;functions]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0; Functions functionheader]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;localdeclarations]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0;statements]TJ/;༖ ;.9; ;Tf 8;.485; 0 T; [0; IntroductiontoC CS2022,Spring2011,Lecture1 hello.c:HelloWorld#includestdi;o.h0;intmain(){printf("HelloWorld\n");return0;} IntroductiontoC CS2022,Spring2011,Lecture1 CompilingandRunningI$gcchello.c-ohelloI$./helloHelloWorld IntroductiontoC CS2022,Spring2011,Lecture1 WhatHappens?I$gcchello.c-ohelloICompile\hello.c"tomachinecodenamed\hello"I\-o"speciestheoutputlename.(Noticeit'scase-sensitive.)I$./helloIExecuteprogram\hello"I\./"isnecessay! hello.c #includestdi;o.h0;//"printf"isdeclaredinthisheaderfile.intmain()//Mainpointofexecution.{printf("HelloWorld\n");//Output"HelloWorld"toconsole.return0;//TellOStheprogramterminatesnormally.} IntroductiontoC CS2022,Spring2011,Lecture1 WhatHappens?I$gcchello.c-ohelloICompile\hello.c"tomachinecodenamed\hello"I\-o"speciestheoutputlename.(Noticeit'scase-sensitive.)I$./helloIExecuteprogram\hello"I\./"isnecessay! hello.c #includestdi;o.h0;//"printf"isdeclaredinthisheaderfile.intmain()//Mainpointofexecution.{printf("HelloWorld\n");//Output"HelloWorld"toconsole.return0;//TellOStheprogramterminatesnormally.} IntroductiontoC CS2022,Spring2011,Lecture1 vars.c:Variables#includestdi;o.h0;intmain(){inta,b,c;a=10;b=20;c=a*b;printf("a=%db=%dc=%d\n",a,b,c);return0;} a=10b=20c=200 IntroductiontoC CS2022,Spring2011,Lecture1 vars.c:Variables#includestdi;o.h0;intmain(){inta,b,c;a=10;b=20;c=a*b;printf("a=%db=%dc=%d\n",a,b,c);return0;} a=10b=20c=200 IntroductiontoC CS2022,Spring2011,Lecture1 cmdarg.c:CommandLineArgs#includestdi;o.h0;#includestdl;ib.h;intmain(intargc,char**argv){intn,m;n=atoi(argv[1]);m=atoi(argv[2]);printf("Argument1:%d\nArgument2:%d\n",n,m);return0;} $./cmdarg1020Argument1:10Argument2:20 IntroductiontoC CS2022,Spring2011,Lecture1 cmdarg.c:CommandLineArgs#includestdi;o.h0;#includestdl;ib.h;intmain(intargc,char**argv){intn,m;n=atoi(argv[1]);m=atoi(argv[2]);printf("Argument1:%d\nArgument2:%d\n",n,m);return0;} $./cmdarg1020Argument1:10Argument2:20 IntroductiontoC CS2022,Spring2011,Lecture1 MoreonprintfIprintf(format string,val1,val2); Iformat stringcanincludeplaceholdersthatspecifyhowtheargumentsval1,val2,etc.shouldbeformattedI%c:formatasacharacterI%d:formatasanintegerI%f:formatasa oating-pointnumberI%%:printa%character Examples floatf=0.95;printf("f=%f%%\n",f*100); f=95.000000% IntroductiontoC CS2022,Spring2011,Lecture1 MoreonprintfIprintf(format string,val1,val2); Iformat stringcanincludeplaceholdersthatspecifyhowtheargumentsval1,val2,etc.shouldbeformattedI%c:formatasacharacterI%d:formatasanintegerI%f:formatasa oating-pointnumberI%%:printa%character Examples floatf=0.95;printf("f=%f%%\n",f*100); f=95.000000% IntroductiontoC CS2022,Spring2011,Lecture1 MoreonprintfIprintf(format string,val1,val2); Iformat stringcanincludeplaceholdersthatspecifyhowtheargumentsval1,val2,etc.shouldbeformattedI%c:formatasacharacterI%d:formatasanintegerI%f:formatasa oating-pointnumberI%%:printa%character Examples floatf=0.95;printf("f=%f%%\n",f*100); f=95.000000% IntroductiontoC CS2022,Spring2011,Lecture1 MoreonprintfIprintf(format string,val1,val2); Iformat stringcanincludeplaceholdersthatspecifyhowtheargumentsval1,val2,etc.shouldbeformattedI%c:formatasacharacterI%d:formatasanintegerI%f:formatasa oating-pointnumberI%%:printa%character Examples floatf=0.95;printf("f=%f%%\n",f*100); f=95.000000% IntroductiontoC CS2022,Spring2011,Lecture1 MoreonprintfIPlaceholderscanalsospecifywidthsandprecisionsI%10d:addspacestotakeupatleast10charactersI%010d:addzerostotakeupatleast10charactersI%.2f:printonly2digitsafterdecimalpointI%5.2f:print1decimaldigit,addspacestotakeup5chars Examples floatf=0.95;printf("f=%.2f%%\n",f*100);//f=95.00%printf("f=%10.2f%%\n",f*100);//f=95.00% IntroductiontoC CS2022,Spring2011,Lecture1 MoreonprintfIPlaceholderscanalsospecifywidthsandprecisionsI%10d:addspacestotakeupatleast10charactersI%010d:addzerostotakeupatleast10charactersI%.2f:printonly2digitsafterdecimalpointI%5.2f:print1decimaldigit,addspacestotakeup5chars Examples floatf=0.95;printf("f=%.2f%%\n",f*100);//f=95.00%printf("f=%10.2f%%\n",f*100);//f=95.00% IntroductiontoC CS2022,Spring2011,Lecture1 WarningaboutprintfIprintfispowerful,butpotentiallydangerous Whatdoesthiscodeoutput? inti=90;floatf=3;printf("f=%fi=%d\n",f);printf("f=%f\n",f,i);printf("i=%df=%f\n",f,i); IntroductiontoC CS2022,Spring2011,Lecture1