31
Practice 1 Practice 1 Compiling and Debugging Compiling and Debugging Using Dev Using Dev - - C++ C++ I. I. Introducing & Using Dev Introducing & Using Dev - - C++ C++ II. II. Compiling and Debugging Compiling and Debugging 09/24/2009 09/24/2009

Practice 1 Compiling and Debugging Using Dev-C++mlli/courses/IntroTo... · 2012. 3. 3. · Practice 1 Compiling and Debugging Using Dev-C++ I. Introducing & Using Dev-C++ II. Compiling

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

  • Practice 1Practice 1

    Compiling and DebuggingCompiling and DebuggingUsing DevUsing Dev--C++C++

    I.I. Introducing & Using DevIntroducing & Using Dev--C++C++II.II. Compiling and DebuggingCompiling and Debugging

    09/24/200909/24/2009

  • TATA’’s Informations Information鄭以成鄭以成 (EECS R705)(EECS R705)

    [email protected]@gmail.com施銘鴻施銘鴻 (EECS R705)(EECS R705)

    [email protected]@oz.nthu.edu.tw曾奕傑曾奕傑 (EECS R705)(EECS R705)

    [email protected]@gmail.com張晉嘉張晉嘉 (EECS R705)(EECS R705)

    [email protected]@oz.nthu.edu.twOffice hour:Office hour:

    Mon. 19:00Mon. 19:00--21:00 and21:00 and Thu. 15:00Thu. 15:00 ––17:0017:00 at EECSat EECSR705R705 or byor by ee--mail,mail, or post your questions on the eor post your questions on the e--learning discussion boardlearning discussion board

  • Practice PolicyPractice Policy

    Practice time slotPractice time slot8:10 a.m. ~ 9:50 a.m.8:10 a.m. ~ 9:50 a.m.

    Please be here on timePlease be here on timeAny special condition (e.g., absent),Any special condition (e.g., absent),

    please discuss with Prof. Li and TAsplease discuss with Prof. Li and TAs ininAdvanceAdvanceNo Credits w/o attendanceNo Credits w/o attendance

  • TodayToday’’s Evaluations EvaluationStandardStandardPractice1.1 20%Practice1.1 20%Practice1.2 25%Practice1.2 25%Practice1.3 25%Practice1.3 25%Practice1.4 30%Practice1.4 30%Note that donNote that don’’t copy the codes from thet copy the codes from the

    slides and paste into Devslides and paste into Dev--C++ directly;C++ directly;otherwise, you have to handle the messotherwise, you have to handle the messyou introduce on your own.you introduce on your own.

  • I. Introducing & UsingI. Introducing & UsingDevDev--C++C++C compilerC compilerGNU GCCGNU GCCMicrosoft Visual C++Microsoft Visual C++Borland C++Borland C++DevDev--C++C++CodeBlocksCodeBlocks

  • DevDev--C++C++

    Why we prefer DevWhy we prefer Dev--C++?C++?FreewareFreewareGCCGCC--based C compilerbased C compilerEasy to useEasy to use

    Where to download it?Where to download it?http://www.bloodshed.net/dev/devcpp.htmlhttp://www.bloodshed.net/dev/devcpp.htmlDocumentation and manual onDocumentation and manual on ““HHelpelp””

  • Start ProgrammingStart Programming

  • #includeintint main(voidmain(void)){{

    printf("Helloprintf("Hello NTHUEE!!NTHUEE!!\\n");n");

    return 0;return 0;}}

  • Writing and Saving CodesWriting and Saving Codes

  • CompilingCompiling

  • RunRun

    Nothing ?! Disappear ?!Nothing ?! Disappear ?!

  • Then TryThen Try

    #includeintint main(voidmain(void)){{

    printf("Helloprintf("Hello NTHUEE!!NTHUEE!!\\n");n");getchargetchar();();return 0;return 0;

    }}

  • Then Try (cont.)Then Try (cont.)

    #include#includeintint main(voidmain(void)){{

    printf("Helloprintf("Hello NTHUEE!!NTHUEE!!\\n");n");getchgetch();();return 0;return 0;

    }}

  • Then Try (cont.)Then Try (cont.)

    #include#include#includeintint main(voidmain(void)){{

    printf("Helloprintf("Hello NTHUEE!!NTHUEE!!\\n");n");system(system(““PAUSEPAUSE””););return 0;return 0;

    }}Command of DOS, Do as its name

  • Practice1.1 (20%)Practice1.1 (20%)Use the DevUse the Dev--C++C++Use one of the above ways to showUse one of the above ways to show““Hello NTHUEE!!Hello NTHUEE!!””on the screenon the screen

  • II. DebuggingII. Debugging

    Syntax (Syntax (語法的語法的) Errors) ErrorsThe compiler will detect

    Semantic (Semantic (語意的語意的) Errors) ErrorsThe compiler does not detect

  • Three Ways to TraceThree Ways to TraceProgram StateProgram State

    HandHand--executingexecuting the program stepthe program step--byby--step yourselfstep yourselfAddingAdding extraextra printfprintf()() statementsstatements

    throughout to monitor the values ofthroughout to monitor the values ofselected variables at key points in theselected variables at key points in theprogramprogramUsing aUsing a debuggerdebugger, a, a programprogram to runto run

    your program stepyour program step--byby--step andstep andexamine the values of your programexamine the values of your program’’ssvariablesvariables

  • Practice1.2 (25%)Practice1.2 (25%)Find the errorsFind the errors#include #include int main(void)int main(void){{

    int n, int n2, int n3int n, int n2, int n3n = 5;n = 5;n2 = n * n;n2 = n * n;n3 = n2 * n2;n3 = n2 * n2;

    /* print out n, n squared, and n cubed/* print out n, n squared, and n cubedprintf("n = %d, n squared = %d, n cubed = %dprintf("n = %d, n squared = %d, n cubed = %d\\n",n,n2,n3);n",n,n2,n3);

    system("PAUSE");system("PAUSE");

    See slide 43 to 48 of Ch2

  • Practice 1.3 (25%)Practice 1.3 (25%) Try to answer the following questions by print the values out wiTry to answer the following questions by print the values out with theth the

    function:function: printfprintf(), and + for add,(), and + for add, -- for subtract, * for multiply, and / forfor subtract, * for multiply, and / fordivide in these statements.divide in these statements.

    (1) What is the answer to this simple calculation?(1) What is the answer to this simple calculation?a=10/3, if a is designated to data typea=10/3, if a is designated to data type intint or floator float

    intint a1 = 10/3; // case (a) , try printf(a1 = 10/3; // case (a) , try printf(““a1 = %da1 = %d \\nn””, a1);, a1);float a2 = 10.0/3.0; // case (b), try printf(float a2 = 10.0/3.0; // case (b), try printf(““a2 = %fa2 = %f \\nn””, a2);, a2);

    //// in C, 3 is different from 3.0in C, 3 is different from 3.0

    (2)(2)intint num = 1.7;num = 1.7; // right assignment?// right assignment?printf("%dprintf("%d\\nn", num); // what will happen?, %d for integer", num); // what will happen?, %d for integerprintf("%fprintf("%f\\nn", num); // what will happen?, %f for floating point numbers", num); // what will happen?, %f for floating point numbersprintf("%fprintf("%f\\nn", (", (float)numfloat)num);); // re// re--castcast

  • Practice 1.3 (Continued)Practice 1.3 (Continued) (3)(3)

    float a = 2.0e20 + 1.0float a = 2.0e20 + 1.0 -- 2.0e20;2.0e20;// What// What’’s the value ofs the value of ““aa””??//Is it as what you expect?//Is it as what you expect?

    float a = 2.0e4 + 1.0float a = 2.0e4 + 1.0 ––2.0e4;2.0e4;// What happen? Correct?// What happen? Correct?

  • Using the DebuggerUsing the Debugger#include intint main(voidmain(void)){{

    intint i=2;i=2;intint j=3;j=3;intint k;k;k = 1+i*j;k = 1+i*j;printfprintf( "i = %d( "i = %d \\n", i );n", i );printfprintf( "j = %d( "j = %d \\n", j );n", j );printfprintf( "k = %d( "k = %d \\n", k );n", k );

    getchargetchar();();return 0;return 0;

    }}

  • Watch variable i j kWatch variable i j k

    Enter i or j or k to check the valueEnter “&i”can see the address of “i”

  • Practice1.4 (30%)Practice1.4 (30%)#include

    // global variables (declared outside main() function)// global variables (declared outside main() function)//// ““globalglobal””means the memory tagged by these variables can be used over allmeans the memory tagged by these variables can be used over all the programthe programintint a = 10; // Whata = 10; // What’’s the memory address (i.e., &a) ofs the memory address (i.e., &a) of ““aa””,, by Add Watch ofby Add Watch of ““&a&a””,, ““&&””derive the addressderive the addressintint b = 20; // Whatb = 20; // What’’s the memory address ofs the memory address of ““bb””intint c = 30; // Whatc = 30; // What’’s the memory address ofs the memory address of ““cc””intint main(voidmain(void)){{

    // local variables (declared inside main() function,// local variables (declared inside main() function,//// ““locallocal””means being restricted in main()means being restricted in main()// means these variables can be used by main()// means these variables can be used by main()// means the memory tagged by these variables// means the memory tagged by these variables is locked to main()is locked to main()intint i=9;i=9; // What// What’’s the value (i.e., i) and memory address (i.e., &i) ofs the value (i.e., i) and memory address (i.e., &i) of ““ii””intint j=10;j=10; // What// What’’s the value and memory address (i.e., &j) ofs the value and memory address (i.e., &j) of ““jj””intint k;k; // What// What’’s the value and memory address ofs the value and memory address of ““kk””? Why this value?? Why this value?intint l;l; // What// What’’s the memory address ofs the memory address of ““ll””

    k = 1k = 122+i*j;+i*j; // What// What’’s the value ofs the value of ““kk””i = 3i = 344+k/i;+k/i; // What// What’’s the value ofs the value of ““ii””??j = 5j = 566+k+k**j;j; // What// What’’s the value ofs the value of ““jj””??k = 7k = 788+j/+j/ii;; // What// What’’s the value ofs the value of ““kk””??

    getchargetchar();();return 0;return 0;

    }}

  • More to Explore withMore to Explore withPractice 1.4Practice 1.4Do you find any addressing rule for localDo you find any addressing rule for local

    variables, from the addresses of the localvariables, from the addresses of the localvariablevariable ““ii””,, ““jj””,, ““kk””,and,and ““ll””??

    Comparing the addresses of local and globalComparing the addresses of local and globalvariables, do you observe thatvariables, do you observe that ““local variableslocal variables””andand ““global variablesglobal variables””seem to use differentseem to use different““blockblock””of memory?of memory?(Concept of memory mapping in C/C++,(Concept of memory mapping in C/C++, googlegoogleit!)it!)