Intermediate Programming, Spring 2017*
|
|
- Arron Butler
- 6 years ago
- Views:
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*
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 informationIntermediate 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 informationIntermediate 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 informationcs3157: 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 informationTopic 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 informationCSci 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 informationDeep 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 informationComputer 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 informationPrinceton 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 informationBasic 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 informationIntermediate 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 informationIntermediate 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 informationKurt 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 informationCSCI-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 informationHello, 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 informationBasic 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 informationIntermediate 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 informationC 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 informationPRINCIPLES 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 informationCS3157: 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 informationProgramming 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 informationOBJECT 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 informationA 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 informationLab 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 informationFormal 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 informationChapter 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 informationEXTERNAL 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 informationIntermediate 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 informationRobust 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 informationTopic 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 informationIntermediate 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 informationFinal 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 informationMidterm 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 informationTutorial 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 informationHigh-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 informationCompiler 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 informationSlide 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 informationCS 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 informationCSCI-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 informationCSE 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 informationCOMsW 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 informationRicardo 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 informationCSC209H 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 informationIntermediate 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 informationConditional 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 informationNEXT 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 information1. 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 informationAgenda. 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 informationPractical 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 informationCOMP322 - 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 informationIntroduction 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 informationCS 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 informationIntermediate 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 informationThe 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 informationRule 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 informationG52CPP 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 informationCommon 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 informationIntermediate 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: 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 informationCSE 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; }
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 informationFor 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 informationUnit 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 informationOutline. 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 informationFinal 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 informationLecture 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 informationOutline. 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 informationProgramming. 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 informationFriday, 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 informationPrinceton 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 informationDynamic 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 informationG52CPP 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 informationDynamic 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 informationmith 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 informationCS 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 informationCS 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 informationmith 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 informationGabriel 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 informationCS113: 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 informationComputer 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 informationCSE 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 informationFrom 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 informationRecitation 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 informationTesting 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 informationCourse 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 informationLectures 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 informationBinghamton 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 informationFile 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 informationC++ 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 informationProgram 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 informationCS 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 informationCSE 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 informationCSE 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 informationProgramming 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 informationcmps104a 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 informationLectures 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 informationCOSC 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 informationLecture 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 informationProgramming 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 informationTesting! 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