Intermediate Programming, Spring 2017*

Size: px
Start display at page:

Download "Intermediate Programming, Spring 2017*"

Transcription

1 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

2 Outline Preprocessor directives Code correctness

3 Preprocessor directives Lines starting with # indicate preprocessor directives #include includes a file at the current location all the contents of stdio.h go here printf( hello world\n ); printf( hello world\n );

4 Preprocessor directives Lines starting with # indicate preprocessor directives #include #define associate a value to a preprocessor variable (no = sign) or just declare the existence of a preprocessor variable all the contents of stdio.h go here printf( %s: %f\n, , PI ); #define PI #define PI_STR PI #define MISHA_DEBUG printf( %s: %f\n, PI_STR, PI );

5 Preprocessor directives Lines starting with # indicate preprocessor directives #include #define #undef undeclare the existence of a preprocessor variable #undef MISHA_DEBUG printf( %s: %f\n, PI_STR, PI );

6 Preprocessor directives Lines starting with # indicate preprocessor directives #include #define / #undef #if / #elif / #else / #endif Conditionally include the code all the contents of stdio.h go here printf( Using first code\n ); #define CODE 1 #if CODE==1 printf( Using first code\n ); #elif CODE==2 printf( Using second code\n ); #else // CODE!=1 && CODE!=2 fprintf( stderr, No code\n ); #endif // CODE

7 Preprocessor directives Lines starting with # indicate preprocessor directives #include #define / #undef #if / #elif / #else / #endif #ifdef / #else / #endif #ifndef / #else / #endif Conditionally include the code all the contents of stdio.h go here printf( Using first code\n ); #define MISHA_DEBUG #ifdef MISHA_DEBUG printf( debug mode ); #else //!MISHA_DEBUG printf( release mode\n ); #endif // MISHA_DEBUG

8 Preprocessor directives Lines starting with # indicate preprocessor directives #include #define / #undef #if / #elif / #else / #endif #ifdef / #else / #endif #ifndef / #else / #endif #error force a compiler error #undef GOOD_CODE #ifndef GOOD_CODE #error bad code #endif //!GOOD_CODE printf( hello\n ); >> gcc -std=c99 -pedantic -Wall -Wextra foo.c foo.c:6:2: error: #error "bad code\n"; #error "bad code\n"; ^~~~~ >>

9 Preprocessor directives Lines starting with # indicate preprocessor directives #include #define / #undef #if / #elif / #else / #endif #ifdef / #else / #endif #ifndef / #else / #endif #error And many more

10 Preprocessor directives Lines starting with # indicate preprocessor directives You can nest preprocessor directives all the contents of stdio.h go here printf( debugging 1\n ); #define MISHA_DEBUG #define DEBUG_MODE 1 #ifdef MISHA_DEBUG #if DEBUG_MODE==1 printf( debugging 1\n ); #else // DEBUG_MODE!=1 printf( unknown debug mode\n ); #endif #else //!MISHA_DEBUG printf( hello world ); #endif // MISHA_DEBUG

11 Preprocessor directives Lines starting with # indicate preprocessor directives You can nest preprocessor directives Can be very useful while working on code: You can work on new changes without discarding the old ones You can specify that different parts of the code should be activated / deactivated together It makes it hard to read the code Remove all the unnecessary directives (unused code) once you ve got the new version up and running

12 Outline Preprocessor directives Code correctness

13 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Bad user input insufficient arguments int main( int argc, char* argv[] ) if( argc<2 ) fprintf( stderr, ) ; exit( 0 );

14 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Bad user input: insufficient arguments file can t be accessed int main( int argc, char* argv[] ) if( argc<2 ) fprintf( stderr, ) ; exit(0); FILE* fp = fopen( argv[1], r ); if( fp==null ) fprintf( stderr, ), exit( 0 );

15 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Bad user input insufficient arguments file can t be accessed wrong argument type / value int main( int argc, char* argv[] ) if( argc<2 ) fprintf( stderr, ) ; exit(0); int i = atoi( argv[1] ); if( i<=0 ) fprintf( stderr, ) ; exit( 0 );

16 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Bad user input insufficient arguments file can t be accessed wrong argument type / value Bad system state insufficient memory int main( int argc, char* argv[] ) if( argc<2 ) fprintf( stderr, ) ; exit(0); int i = atoi( argv[1] ); if( i<=0 ) fprintf( stderr, ) ; exit( 0 ); char* str = malloc( i ); if(!str ) fprintf( stderr, ) ; exit( 0 );

17 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Bad user input Bad system state Print out a meaningful error message and: from main: return with a failure (non-zero) value from a function: exit

18 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Blame yourself: The code didn t do what it should have void sort_ints( int* arr, size_t sz ) int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); if( a[0]>a[1] )

19 Code correctness During execution, your code may end up in a bad state from which you cannot recover Blame the world: The assumptions about the state of the system are false Blame yourself: The code didn t do what it should have Include the assert.h header file assert the validity of a test If the argument is true, nothing happens Otherwise, the code aborts and a core dump file is generated #include <assert.h> void sort_ints( int* arr, size_t sz ) return; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( a[0]<a[1] ); >>./a.out a.out: foo.c:11: main: Assertion `a[0]<a[1]' failed.abort (cored dumped) >> You can use gdb to debug the core dump

20 Code correctness Note: This code tests if the first two elements are sorted #include <assert.h> void sort_ints( int* arr, size_t sz ) return; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( a[0]<a[1] );

21 Code correctness Note: This code tests if the first two elements are sorted Better code tests that all the elements are sorted #include <assert.h> void sort_ints( int* arr, size_t sz ) return; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( a[0]<a[1] ); #include <assert.h> void sort_ints( int* arr, size_t sz ) int issorted( const int * arr, size_t sz ) for( int i=0 ; i<sz-1 ; i++ ) if( arr[i]>arr[i+1] ) return 1; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( issorted( a, sizeof(a) / sizeof(int) );

22 Notes on assert assert is defined as a macro, not a function #include <assert.h> void sort_ints( int* arr, size_t sz ) int issorted( const int * arr, size_t sz ) for( int i=0 ; i<sz-1 ; i++ ) if( arr[i]>arr[i+1] ) return 1; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( issorted( a, sizeof(a) / sizeof(int) );

23 Notes on assert assert is defined as a macro, not a function Once our code is working we can stop the assertion by defining the preprocessor variable NDEBUG #define NDEBUG #include <assert.h> void sort_ints( int* arr, size_t sz ) int issorted( const int * arr, size_t sz ) for( int i=0 ; i<sz-1 ; i++ ) if( arr[i]>arr[i+1] ) return 1; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( issorted( a, sizeof(a) / sizeof(int) );

24 Notes on assert assert is defined as a macro, not a function Once our code is working we can stop the assertion by defining the preprocessor variable NDEBUG This removes the whole assertion clause so that the argument is not evaluated issorted doesn t get called so the code runs more quickly #define NDEBUG #include <assert.h> void sort_ints( int* arr, size_t sz ) int issorted( const int * arr, size_t sz ) for( int i=0 ; i<sz-1 ; i++ ) if( arr[i]>arr[i+1] ) return 1; int a[] = 11, 7, 9, 5, 8, 4, 2 ; sort_ints( a, sizeof(a) / sizeof(int) ); assert( issorted( a, sizeof(a) / sizeof(int) );

25 Notes on assert assert is defined as a macro, not a function Once our code is working we can stop the assertion by defining the preprocessor variable NDEBUG This removes the whole assertion clause so that the argument is not evaluated issorted doesn t get called so the code runs more quickly Beware: The argument of assert should not do something useful! #define NDEBUG #include <assert.h> void sort_ints( int* arr, size_t sz ) int issorted( const int * arr, size_t sz ) for( int i=0 ; i<sz-1 ; i++ ) if( arr[i]>arr[i+1] ) return 1; int sortandtest( int* arr, size_t sz ) sort( arr, sz ); return( issorted( arr, sz ) ); int a[] = 11, 7, 9, 5, 8, 4, 2 ; assert( sortandtest( a, sizeof(a) / sizeof(int) );

26 Testing Until now, our program testing has been applied to an entire program Run with prescribed input and check if the output matches what we expect This is called end-to-end testing Verifies that program will run as expected under real-world conditions End-to-end testing is important, but tracking down bugs this way gets more difficult as programs get larger and more complex It doesn t help identify where these are broken when the output is wrong

27 Unit testing IDEA: Test each unit, or module, of a program separately For each function we write, we create another function which tests it A test function is typically fairly simple, and just runs a number of tests on the subject function, one after another Tests typically consist of feeding the subject function a known input, and verifying that it returns the appropriate result Each input/desired output pair is called a test case Want numerous test cases, so passing all test cases is convincing evidence that subject function works correctly Test with a wide variety: good inputs, bad inputs, boundary/corner cases

28 Unit testing It s generally hard/impossible to prove that code is correct. Unit tests help convince us that our functions ( building blocks ) behave correctly, or else they help us locate errors quickly Useful for adapting /modifying code e.g. If function foo1 calls function foo2, we can check that changes to foo2 don t invalidate the functionality of foo1 Writing unit tests shouldn t be an afterthought; resulting code is often better when we test as we go (and it takes less time to write overall) It s turtles all the way down: We re still left with the problem of confirming that our unit tests are correct Hopefully they are simple enough that there is less of an opportunity for error.

29 Coding Session

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSci 4061 Introduction to Operating Systems. Programs in C/Unix CSci 4061 Introduction to Operating Systems Programs in C/Unix Today Basic C programming Follow on to recitation Structure of a C program A C program consists of a collection of C functions, structs, arrays,

More information

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems Deep C Multifile projects Getting it running Data types Typecasting Memory management Pointers Fabián E. Bustamante, Fall 2004 Multifile Projects Give your project a structure Modularized design Reuse

More information

Computer Labs: Debugging

Computer Labs: Debugging Computer Labs: Debugging 2 o MIEIC Pedro F. Souto (pfs@fe.up.pt) October 29, 2012 Bugs and Debugging Problem To err is human This is specially true when the human is a programmer :( Solution There is none.

More information

Princeton University. Testing. Computer Science 217: Introduction to Programming Systems

Princeton University. Testing. Computer Science 217: Introduction to Programming Systems Princeton University Computer Science 217: Introduction to Programming Systems Testing On two occasions I have been asked [by members of Parliament!], Pray, Mr. Babbage, if you put into the machine wrong

More information

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Announcements Exam 1 (20%): Feb. 27 (Tuesday) Tentative Proposal Deadline:

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3 Hello, World! in C Johann Myrkraverk Oskarsson October 23, 2018 Contents 1 The Quintessential Example Program 1 I Printing Text 2 II The Main Function 3 III The Header Files 4 IV Compiling and Running

More information

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #8 Feb 27 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline More c Preprocessor Bitwise operations Character handling Math/random Review for midterm Reading: k&r ch

More information

Programming for Engineers C Preprocessor

Programming for Engineers C Preprocessor Programming for Engineers C Preprocessor ICEN 200 Spring 2018 Prof. Dola Saha 1 C Preprocessor The C preprocessor executes before a program is compiled. Some actions it performs are the inclusion of other

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Chapter 17 - The Preprocessor Outline 17.1 Introduction 17.2 The #include Preprocessor Directive 17.3 The #define Preprocessor Directive: Symbolic Constants 17.4 The

More information

A Fast Review of C Essentials Part II

A Fast Review of C Essentials Part II A Fast Review of C Essentials Part II Structural Programming by Z. Cihan TAYSI Outline Macro processing Macro substitution Removing a macro definition Macros vs. functions Built-in macros Conditional compilation

More information

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function CMPT 127 Spring 2019 Grade: / 20 First name: Last name: Student Number: Lab Exam 1 D400 1. [1 mark] Give an example of a sample input which would make the function scanf( "%f", &f ) return -1? Answer:

More information

Formal Methods for C

Formal Methods for C Formal Methods for C Seminar Summer Semester 2014 Daniel Dietsch, Sergio Feo Arenis, Marius Greitschus, Bernd Westphal 2014-04 main Content Brief history Comments Declarations and Scopes Variables Expressions

More information

Chapter 7: Preprocessing Directives

Chapter 7: Preprocessing Directives Chapter 7: Preprocessing Directives Outline We will only cover these topics in Chapter 7, the remain ones are optional. Introduction Symbolic Constants and Macros Source File Inclusion Conditional Compilation

More information

EXTERNAL TESTING. Princeton University Computer Science 217: Introduction to Programming Systems. Why Test? Testing. Why Test? Why Test?

EXTERNAL TESTING. Princeton University Computer Science 217: Introduction to Programming Systems. Why Test? Testing. Why Test? Why Test? Princeton University Computer Science 217: Introduction to Programming Systems Testing Why Test? It's hard to know if a (large) program works properly Ideally: Automatically prove that a program is correct

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Robust Programming + Testing, Profiling, & Instrumentation

Robust Programming + Testing, Profiling, & Instrumentation Robust Programming + Testing, Profiling, & Instrumentation CS 217 Program Errors Programs encounter errors Good programmers handle them gracefully Types of errors Compile-time errors Link-time errors Run-time

More information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Final C Details. CSE 333 Autumn 2018

Final C Details. CSE 333 Autumn 2018 Final C Details CSE 333 Autumn 2018 Instructor: Hal Perkins Teaching Assistants: Tarkan Al-Kazily Renshu Gu Trais McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu Administriia Today:

More information

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee. Midterm Exam Nov 8th, 2012 COMS W3157 Advanced Programming Columbia University Fall 2012 Instructor: Jae Woo Lee About this exam: - There are 4 problems totaling 100 points: problem 1: 30 points problem

More information

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( ) Systems Group Department of Computer Science ETH Zürich Tutorial 1: Introduction to C Computer Architecture and Systems Programming (252-0061-00) Herbstsemester 2012 Goal Quick introduction to C Enough

More information

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy

More information

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32

More information

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include

More information

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam Sample Solution 7/28/14 Question 1. (20 points) C programming. For this question implement a C function contains that returns 1 (true) if a given C string appears as a substring of another C string starting at a given position.

More information

COMsW Introduction to Computer Programming in C

COMsW Introduction to Computer Programming in C OMsW 1003-1 Introduction to omputer Programming in Lecture 9 Spring 2011 Instructor: Michele Merler http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html 1 Are omputers Smarter than Humans? Link http://latimesblogs.latimes.com/technology/2011/02/ibms-watson-on-jeopardy-computer-takes-big-leadover-humans-in-round-2.html

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSC209H Lecture 3. Dan Zingaro. January 21, 2015 CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without

More information

Intermediate Programming, Spring Misha Kazhdan

Intermediate Programming, Spring Misha Kazhdan 600.120 Intermediate Programming, Spring 2017 Misha Kazhdan Outline Unix/Linux command line Basics of the Emacs editor Compiling and running a simple C program Cloning a repository Connecting to ugrad

More information

Conditional Compilation

Conditional Compilation Conditional Compilation printf() statements cab be inserted code for the purpose of displaying debug information during program testing. Once the program is debugged and accepted as "working'', it is desirable

More information

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313. NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Programming in C! Advanced Pointers! Reminder! You can t use a pointer until it points

More information

1. We have a code sequence that is potentially executed twice.

1. We have a code sequence that is potentially executed twice. Long Jump Long Jump Local Jumps In the following example: 1. We have a code sequence that is potentially executed twice. 2. A flag is used to determine whether the sequence is being executed the first

More information

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

More information

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017 Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles CS449 Fall 2017 Multi-file Development Multi-file Development Why break code into multiple source files? Parallel development

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #54 Organizing Code in multiple files (Refer Slide Time: 00:09) In this lecture, let us look at one particular

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

The C standard library

The C standard library C introduction The C standard library The C standard library 1 / 12 Contents Do not reinvent the wheel Useful headers Man page The C standard library 2 / 12 The Hitchhiker s Guide to the standard library

More information

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead. Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public

More information

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

G52CPP C++ Programming Lecture 6. Dr Jason Atkin G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1 Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2 Visibility is different from lifetime Just

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

#include. Practical C Issues: #define. #define Macros. Example. #if

#include. Practical C Issues: #define. #define Macros. Example. #if #include Practical C Issues: Preprocessor Directives, Typedefs, Multi-file Development, and Makefiles Jonathan Misurda jmisurda@cs.pitt.edu Copies the contents of the specified file into the current file

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Winter 2009 Lecture 7 Introduction to C: The C-Level of Abstraction CSE 303 Winter 2009, Lecture 7 1 Welcome to C Compared to Java, in rough

More information

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

#include <stdio.h> int main() { printf (hello class\n); return 0; } C #include int main() printf ("hello class\n"); return 0; Working environment Linux, gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul

More information

For Your Amusement. Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth. Program Verification

For Your Amusement. Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth. Program Verification Princeton University Computer Science 217: Introduction to Programming Systems Testing For Your Amusement On two occasions I have been asked [by members of Parliament!], Pray, Mr. Babbage, if you put into

More information

Unit 4 Preprocessor Directives

Unit 4 Preprocessor Directives 1 What is pre-processor? The job of C preprocessor is to process the source code before it is passed to the compiler. Source Code (test.c) Pre-Processor Intermediate Code (test.i) Compiler The pre-processor

More information

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging Outline Computer programming Debugging Hints Gathering evidence Common C errors "Education is a progressive discovery of our own ignorance." Will Durant T.U. Cluj-Napoca - Computer Programming - lecture

More information

Final C Details. CSE 333 Winter Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht

Final C Details. CSE 333 Winter Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht Final C Details CSE 333 Winter 2019 Instructor: Hal Perkins Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti Daid Porter Forrest Timour Soumya Vasisht Yifan Xu Sujie Zhou Administriia Today: C

More information

Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types Lecture 03 Bits, Bytes and Data Types Computer Languages A computer language is a language that is used to communicate with a machine. Like all languages, computer languages have syntax (form) and semantics

More information

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison

More information

Programming. Projects with Multiple Files

Programming. Projects with Multiple Files Programming Projects with Multiple Files Summary } GCC } Multiple source files 2 Source Code with Multiple Files } Make multiple, separate source code files which can be combined into one executable }

More information

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week Friday, September 16, 2016 Lab Notes Topics for today Redirection of input and output Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week 1. Redirection

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Testing

Princeton University Computer Science 217: Introduction to Programming Systems. Testing Princeton University Computer Science 217: Introduction to Programming Systems Testing Fall 2017 1 Software engineering method 1 1. Write program 2. Upload program to customer 3.... program has bugs, useless...

More information

Dynamic memory allocation (malloc)

Dynamic memory allocation (malloc) 1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration

More information

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

G52CPP C++ Programming Lecture 8. Dr Jason Atkin G52CPP C++ Programming Lecture 8 Dr Jason Atkin 1 Last lecture Dynamic memory allocation Memory re-allocation to grow arrays Linked lists Use -> rather than. pcurrent = pcurrent -> pnext; 2 Aside: do not

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut mith College CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 2 Hours D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

CS 0449 Sample Midterm

CS 0449 Sample Midterm Name: CS 0449 Sample Midterm Multiple Choice 1.) Given char *a = Hello ; char *b = World;, which of the following would result in an error? A) strlen(a) B) strcpy(a, b) C) strcmp(a, b) D) strstr(a, b)

More information

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut mith College CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 4 Hours! D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 CMPE 013/L Pre Processor Commands Gabriel Hugh Elkaim Spring 2013 3 Introduction Preprocessing Affect program preprocessing and execution Capabilities Inclusion of additional C source files Definition

More information

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as

More information

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss Computer Programming The greatest gift you can give another is the purity of your attention. Richard Moss Outline Modular programming Modularity Header file Code file Debugging Hints Examples T.U. Cluj-Napoca

More information

CSE 333 Lecture 6 - data structures

CSE 333 Lecture 6 - data structures CSE 333 Lecture 6 - data structures Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia Exercises: - ex5 is out: clean up the code from section yesterday, split

More information

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor

More information

Recitation 2/18/2012

Recitation 2/18/2012 15-213 Recitation 2/18/2012 Announcements Buflab due tomorrow Cachelab out tomorrow Any questions? Outline Cachelab preview Useful C functions for cachelab Cachelab Part 1: you have to create a cache simulator

More information

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science Testing and Debugging C Programming and Software Tools N.C. State Department of Computer Science Introduction Majority of software development is testing, debugging, and bug fixing The best software developers

More information

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4) Course organization 1 Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4) C Unix/Linux Chapter 2: Types,

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text C Debugging Basics No relevant text First Computer Bug 2 The printf debugger Insert printf statements to print debug information Build/Run Modify to print new information Advantages Simple Complete Available

More information

File I/O. Preprocessor Macros

File I/O. Preprocessor Macros Computer Programming File I/O. Preprocessor Macros Marius Minea marius@cs.upt.ro 4 December 2017 Files and streams A file is a data resource on persistent storage (e.g. disk). File contents are typically

More information

C++ Tutorial AM 225. Dan Fortunato

C++ Tutorial AM 225. Dan Fortunato C++ Tutorial AM 225 Dan Fortunato Anatomy of a C++ program A program begins execution in the main() function, which is called automatically when the program is run. Code from external libraries can be

More information

Program Verification! Goals of this Lecture! Words from the Wise! Testing!

Program Verification! Goals of this Lecture! Words from the Wise! Testing! Words from the Wise Testing On two occasions I have been asked [by members of Parliament], Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out? I am not able rightly

More information

CS 261 Fall Mike Lam, Professor. Structs and I/O

CS 261 Fall Mike Lam, Professor. Structs and I/O CS 261 Fall 2018 Mike Lam, Professor Structs and I/O Typedefs A typedef is a way to create a new type name Basically a synonym for another type Useful for shortening long types or providing more meaningful

More information

CSE 333 Midterm Exam 2/12/16. Name UW ID#

CSE 333 Midterm Exam 2/12/16. Name UW ID# Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

CSE 374 Final Exam 3/15/17. Name UW ID#

CSE 374 Final Exam 3/15/17. Name UW ID# Name UW ID# There are 10 questions worth a total of 110 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

Programming in C First meeting Tiina Niklander

Programming in C First meeting Tiina Niklander Programming in C First meeting 5.9.2016 Tiina Niklander Faculty of Science Department of Computer Science www.cs.helsinki.fi 5.9.2018 1 Learning goal objectives Language structures, data structures, modules,

More information

cmps104a 2002q4 Assignment 2 Lexical Analyzer page 1

cmps104a 2002q4 Assignment 2 Lexical Analyzer page 1 cmps104a 2002q4 Assignment 2 Lexical Analyzer page 1 $Id: asg2-scanner.mm,v 327.1 2002-10-03 13:20:15-07 - - $ 1. The Scanner (Lexical Analyzer) Write a main program, string table manager, and lexical

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

COSC Software Engineering. Lecture 16: Managing Memory Managers

COSC Software Engineering. Lecture 16: Managing Memory Managers COSC345 2013 Software Engineering Lecture 16: Managing Memory Managers Outline Typical problems (from previous lectures) Memory leaks aren t just for (Objective) C Tracking malloc() calls Catching calls

More information

Lecture 07 Debugging Programs with GDB

Lecture 07 Debugging Programs with GDB Lecture 07 Debugging Programs with GDB In this lecture What is debugging Most Common Type of errors Process of debugging Examples Further readings Exercises What is Debugging Debugging is the process of

More information

Programming in C week 1 meeting Tiina Niklander

Programming in C week 1 meeting Tiina Niklander Programming in C week 1 meeting 2.9.2015 Tiina Niklander Faculty of Science Department of Computer Science 3.9.2015 1 Course structure Based on C programming course in Aalto, but with some exercises created

More information

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6! Testing The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 6 1 Words from the Wise On two occasions I have been asked [by members of Parliament],

More information