W 3 L 2 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Week 3, les 2 : The preprocessor and other odds and ends (assignment: fagan inspection session)

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Variables in C Amir Haider Lecturer.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
C Intro.
Kernighan/Ritchie: Kelley/Pohl:
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
Structure of a C program
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
C Hints and Tips The preprocessor and other fun toys.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
W 3 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Week 3, les 1 : Inspection (Fagan style)
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Programming in C Project Organization Compiler Directives.
13 C Preprocessor.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
User Interaction and Variables
Chapter 7: User-Defined Functions II
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
Chapter 3 Assignment Statement
Programmazione I a.a. 2017/2018.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Variables in C Topics Naming Variables Declaring Variables
C Preprocessor(CPP).
Govt. Polytechnic,Dhangar
Programming in C Miscellaneous Topics.
UMBC CMSC 104 – Section 01, Fall 2016
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
C Programming Language
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Week 2 - Friday CS222.
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
Presentation transcript:

W 3 L 2 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Week 3, les 2 : The preprocessor and other odds and ends (assignment: fagan inspection session)

W 3 L 2 sh 2 C compilation process #define MESSAGE ”Hello” int main( void){ printf( "m [%s %X\n", MESSAGE, MESSAGE ); f(); } void f( void ){ printf( "f [%s %X\n", ”Hello”, ”Hello” ); } preprocessor compiler  Separate step  Can be used by other languages  A powerful but potentially dangerous tool Why that empty line?

W 3 L 2 sh 3 Some preprocessor directives #include ”file” - #include #define #ifdef - #ifndef - #elif - #else - #endif #if - #elif - #else - #endif

W 3 L 2 sh 4 Some useful predefined macro’s __FILE__ __LINE__ Note: 2 underscores! if( size < 0 ){ printf( ”Fatal error: size below 0 in %s at line %d\n”, __FILE__, __LINE__ ); exit(); }

W 3 L 2 sh 5 A macro with parameters #define name( arg1, arg2, … ) expansion #define int_swap( x, y ){ int temp = x; x = y; y = temp; } int a = 1, b = 2; printf( ”a=%d b=%d\n”, a, b ); int_swap( a, b ); printf( ”a=%d b=%d\n”, a, b );

W 3 L 2 sh 6 Multiple expansion  #define square( x ) ( x * x ) int a = 1; printf( ”a=%d square(a)=%d\n”, a, square(a) ); int b = 1; printf( ”square(b++)=”, square(b++) ); Printf( ”b=%d\n”, b ); n This is a dirty macro. If you want to use it at all, make clear to the reader that it is a macro! #define SQUARE( x ) ( x * x )

W 3 L 2 sh 7 More parameter trouble  #define double( x ) ( x * 2 ) int a = 1; printf( ”a=%d double(a)=%d\n”, a, double(a) ); int b = 1; printf( ”double(b + 1)=”, double( b + 1 ) ); n In the expansion, ALWAYS put the macro arguments in parenthesis: #define double( x ) ( (x) * 2 )

W 3 L 2 sh 8 Multi-line expansion #define TRACE if( trace_enable ) { \ printf( \ ”%s:%d\n”, \ __FILE__, \ __LINE__ \ ); \ } int trace_enable = 0; void f( void ){ trace_enabled = 1; TRACE; printf( ”Hello year %s\n”, 2010 ); TRACE; } n Why no \ after the last } ?? n Note that the \ must be the very last character: even a space after it will cause an error!

W 3 L 2 sh 9 An assert macro #define assert( condition ) if( ! (condition) ){ \ printf( \ ”%s:%d Fatal error: assert failed\n”, \ __FILE__, \ __LINE__ \ ); \ exit(); \ } void root( int x ){ assert( x >= 0 );... }

W 3 L 2 sh 10 Casts (int) (char *) Tells the compiler that the type of an expression must be converted. int n = 4, sum = 3; float average = sum / n; printf( ”average is %f”, average ); int n = 4, sum = 3; float average = sum / (float) n; printf( ”average is %f”, average );

W 3 L 2 sh 11 Casts typedef unsigned int size_t; Void * malloc( size_t size ); char * message = malloc( 132 ); Malloc allocates memory and returns a (void!) pointer to the allocated memory. char * message = (char *) malloc( 132 ); Tell the compiler that it must ‘convert’ the void * to a char *.

W 3 L 2 sh 12 sizeof sizeof( type ) This is a macro-like C construct that is replaced (by the C compiler) by the size of the type printf( ”char is %2d bytes\n”, sizeof( char ) ); printf( ”short is %2d bytes\n”, sizeof( short int ) ); printf( ”int is %2d bytes\n”, sizeof( int ) ); printf( ”long is %2d bytes\n”, sizeof( long int ) ); printf( ”long long is %2d bytes\n”, sizeof( long long int ) );

W 3 L 2 sh 13 A memory allocation macro What did we do wrong? typedef char big_buffer[ 1000 ]; typedef char small_buffer[ 10 ]; big_buffer * buf = ( big_buffer *) malloc( sizeof( small_buffer )); int i; for( i = 0; i < 1000; i++ ){ buf[ i ] = 0; } #define allocate( type ) (( type * ) malloc( sizeof( type ))) big_buffer * buf = malloc( sizeof( small_buffer ));

W 3 L 2 sh 14 Macro guidelines Macros are fun, but can also be very dangerous. Do not use a macro when another (C-language level) construct would be equally effective. Obey the basic safe-use rules: parameters in (), statements in {}. Macro’s that fully behave as variables, constants, or functions can be named as such. Macro’s that require the reader to be aware of their nature must be in UPPERCASE.

W 3 L 2 sh 15 Fagan assignment 2 - session The moderators will each old an inspection session with their inspectors (in the practicum room or near to it). The allotted time is the 90 minutes for the assignment. In the session the document is scanned and the defects are mentioned - to the level that is deemed practical by the moderator (µ’s are never mentioned). The scanner role (the inspector that reads identifies the next defect) is rotated. The inspectors cross out any (above µ) duplicates.

W 3 L 2 sh 16 Fagan assignment 2 - moderator The moderators  Is the chairman.  Makes sure that the inspectors get equal ‘speaking time’.  Kills any discussions about whether a defect is right or the document or is correct. The aim of the session is that it gets clear what an inspector means, not whether he is right.  Keeps an eye on the allotted time. Use the total document must be handled.

W 3 L 2 sh 17 Fagan assignment 2 - output The inspectors count the number of duplicate defects and note the number of remaining defects on their defect list. The defect lists and the annotated documents are handed over to the moderators. The moderators report to the teacher (by , word template is on sharepoint):  The original number of defects (minor and major separately) found by each inspector (mention the inspectors name and role!).  The total number of defects found, and the total number remaining after duplicates are removed (minor and major separately).  Any other interesting observations, including defects in the input documents.

W 3 L 2 sh 18 Fagan assignment 2 – follow-up This is outside the scope of V2CCPP1, but I strongly suggest that:  Each group reworks its document according to the inspection results, before it is submitted for the project.  The moderator does no rework work himself, but checks the rework (at least for the major defects).  When a major defect is unclear (even to the moderator) you can contact the inspector.