271K - views

Introduction to C Introduction and Hello World Instructor Yin Lou Introduction to C CS Spring Lecture Administrivia Instructor Yin Lou Upson Email yinloucs

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

Embed :
Pdf Download Link

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.

Introduction to C Introduction and Hello World Instructor Yin Lou Introduction to C CS Spring Lecture Administrivia Instructor Yin Lou Upson Email yinloucs






Presentation on theme: "Introduction to C Introduction and Hello World Instructor Yin Lou Introduction to C CS Spring Lecture Administrivia Instructor Yin Lou Upson Email yinloucs"— Presentation transcript:

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&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;-Enternormalmode MoreNormalModeCommands I:w-Write/SaveI:q-QuitI:q!-QuitwithoutsavingI:wq-WriteandQuite IntroductiontoC CS2022,Spring2011,Lecture1 IntroductiontoVim MoreCommands ISearchI/+pattern&#x]TJ/;༖ ; .96;& T; 7.;݉ ;� Td;&#x [00;In-nextsearchmatchIN-previoussearchmatchIGotoI:+lineno&#x]TJ/;༖ ; .96;& T; 7.;݉ ;� Td;&#x [00;Igg-Goto rstlineIG-GotolastlineIDeleteLineIdd Google\vimcheatsheet"formore! IntroductiontoC CS2022,Spring2011,Lecture1 StructureofaCProgram OverallProgram somepre-processordirectives&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;globaldeclarations&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;globalvariables&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;functions&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0; Functions functionheader&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;localdeclarations&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;statements&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0; IntroductiontoC CS2022,Spring2011,Lecture1 StructureofaCProgram OverallProgram somepre-processordirectives&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;globaldeclarations&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;globalvariables&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;functions&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0; Functions functionheader&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;localdeclarations&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0;statements&#x]TJ/;༖ ;.9;‘ ;&#xTf 8;&#x.485;&#x 0 T; [0; IntroductiontoC CS2022,Spring2011,Lecture1 hello.c:HelloWorld#include&#xstdi;&#xo.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"speci estheoutput lename.(Noticeit'scase-sensitive.)I$./helloIExecuteprogram\hello"I\./"isnecessay! hello.c #include&#xstdi;&#xo.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"speci estheoutput lename.(Noticeit'scase-sensitive.)I$./helloIExecuteprogram\hello"I\./"isnecessay! hello.c #include&#xstdi;&#xo.h0;//"printf"isdeclaredinthisheaderfile.intmain()//Mainpointofexecution.{printf("HelloWorld\n");//Output"HelloWorld"toconsole.return0;//TellOStheprogramterminatesnormally.} IntroductiontoC CS2022,Spring2011,Lecture1 vars.c:Variables#include&#xstdi;&#xo.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#include&#xstdi;&#xo.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#include&#xstdi;&#xo.h0;#include&#xstdl;&#xib.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#include&#xstdi;&#xo.h0;#include&#xstdl;&#xib.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