W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: https://cursussen.sharepoint.hu.nl/fnt/8/TCTI-V2CCPP1-10/default.aspx.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
C Language.
W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Structure of a C program
Overview creating your own functions calling your own functions.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Modules, Hierarchy Charts, and Documentation
C Slides and captured lecture (video and sound) are available at:
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
W 1 L 2 sh 1 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.
Introduction to programming in the Java programming language.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 2 Variables.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
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.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
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)
Chapter 2 Variables.
Working with Java.
A bit of C programming Lecture 3 Uli Raich.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter7 Structure & C++
About the Presentations
Functions Separate Compilation
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables in C Topics Naming Variables Declaring Variables
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Variables in C Declaring , Naming, and Using Variables.
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
C Programming Language
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
Presentation transcript:

W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site:

W 1 L 1 sh 2 Boeken Boeken:  In zee met C L.v.Moergestel  C++ from the Beginning J. Skansholm (2nd Edition) These books are highly recommended, but they are not strictly needed.

W 1 L 1 sh 3 lesprogram Week 1: 1. objects, names 2. statements, layout Week 2: 1. functions, decomposition 2. lists, specifications Week 3: 1. memory, testing 2. Fagan inspection Week 4: 1. class, copy-constructor 2. operators1 Week 5:3. operators2 4. inheritance Week 6: 5. virtual methods 6. exceptions, trial exam C C++ good programming practices

W 1 L 1 sh 4 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2 lesson 2Lists, specification Week 3 lesson 1Memory, testing Week 3 lesson 2Fagan inspection

W 1 L 1 sh 5 Assignments Read the ‘overzicht’ document! Summary:  Individual  Fixed deadline: 24:00 before next week’s lesson  By , fixed header, one file attached  Each assignment is rated  Ratings are averaged  No second chance !

W 1 L 1 sh 6 Code exist only to be run? #define P(X)j=write(1,X,1) #define C 39 int M[5000]={2},*u=M,N[5000],R=22,a[4],l[]={0,-1,C-1,-1},m[]={1,-C,- 1,C},*b=N, *d=N,c,e,f,g,i,j,k,s;main(){for(M[i=C*R- 1]=24;f|d>=b;){c=M[g=i];i=e;for(s=f=0; s =0&&k =16!=M[k]> =16))a[f++ ]=s;if(f){f=M[e=m[s=a[rand()/( /f)]]+g];j=j<f?f:j;f+=c&- 16*!j;M[g]= c|1 b++?b[-1]:e;}P(" ");for(s=C;--s;P("_") )P(" ");for(;P("\n"),R--;P("|"))for(e=C;e--;P("_ "+(*u++/8)%2))P("| "+(*u/4)%2 );}

W 1 L 1 sh 7 Code is there to be read! n Code is n written once, n re-written a few times, n read many many times. n Reading starts immediately after you type in the first version..  Most of the cost is in reading, not in writing.  To lower software cost: make reading easier, event when that takes more effort (cost) in the writing.

W 1 L 1 sh 8 How to make code readable n Names (today) n Names must help the reader to understand the code. n Format (layout) n The layout must match the statement structure. n The layout must be consistent. n Comments n Comments must explain what the reader needs to know but can not easily read from the code. n Structure n The code must be structured to be understandable. n Known concepts n Use concepts and techniques that are familiar (datastructures, patterns, real-world analogues).

W 1 L 1 sh 9 Things in C that have names (identifiers) n Variable n Function n Typedef n Parameter n Struct n Struct element n Macro n Enumerate n (Label) n (File) Compilation only (unique per file) Compile and link (can be shared)

W 1 L 1 sh 10 C compilation process : separate files, separate objects #define MESSAGE ”Hello” int main( void){ printf( "m [%s %X\n", MESSAGE, MESSAGE ); f(); } #define MESSAGE ”Hello” void f( void ){ printf( "f [%s %X\n", MESSAGE, MESSAGE ); } compile link main.c msg.c msg.omain.o stdlib.lib main.exe main.hex main

W 1 L 1 sh 11 C compilation process : use a header file to share text #include ”msg.h” int main( void){ printf( "m [%s %X\n", MESSAGE, MESSAGE ); f(); } #include ”msg.h” void f( void ){ printf( "f [%s %X\n", MESSAGE, MESSAGE ); } compile link main.c msg.c msg.omain.o stdlib.lib main.exe main.hex main #define MESSAGE "Hello" msg.h  Now only one place to maintain!

W 1 L 1 sh 12 The multiple inclusion problem #include ”message.h”... #include ”basics.h”... #include ”basics.h” #include ”message.h”... main.c message.h message.c typedef unsigned char byte;... basics.h

W 1 L 1 sh 13 #ifndef basics_h #define basics_h typedef unsigned char byte;... #endif Avoiding the multiple inclusion problem #include ”message.h”... #include ”basics.h”... #include ”basics.h” #include ”message.h”... main.c message.h message.c basics.h This is an idiom.

W 1 L 1 sh 14 Per-compilation named things n Typedef n Parameter n Struct n Struct element n Macro n Enumerate  In each compilation unit, each such thing must be defined once before it is used.  When you want to share a definition between files you put it in a header file (never cut-n-paste!!).  Header files use the #ifdef/#define/#endif idiom to prevent multiple-inclusion problems.

W 1 L 1 sh 15 C compilation process: sharing a run-time object extern char *msg; int main( void ){ printf( ”%s\n”, msg ); } char *msg = ”Helllo world”; compile link main.c msg.c msg.omain.ostdlib.lib main.exe main.hex Hello world main

W 1 L 1 sh 16 C compilation process: sharing a run-time object  extern char *msg; int main( void ){ printf( ”%s\n”, msg ); } int msg = 0x1234; compile link main.c msg.c msg.omain.ostdlib.lib main.exe main.hex main To the linker a name is just an address. 

W 1 L 1 sh 17 C run-time named things n Variable n Function Can be n Declared : state that it exist (provide the external properties: type, prototype) n Defined : declare and create it (provide code, room, initial data, etc) n Used

W 1 L 1 sh 18 C run-time named things Can be n Declared : state that it exist n Defined : declare and create it n Used Rules: 1. In each compilation unit, each object must be declared before it is used. 2. In each application, each object that is used must be defined once. 3. Declarations must be in header files, header files must use the #ifndef idiom.

W 1 L 1 sh 19 Variable n Variable n Global, outside a function, or n Local, within a function n By default it is a definition, extern makes it a declaration n Function n Typedef n Parameter n Struct n Struct element n Macro int saved_x; extern int x_saved_counter; void save_x( int a ){ saved_x = a; x_saved_counter++; } unsigned int power_of_two( unsigned int pow ){ unsigned int i, result = 1; for( i = 0; i < pow; i++ ){ result = 2 * result; } return result; }

W 1 L 1 sh 20 Function n Variable n Function n With a body it is a definition n Header-only is a declaration n Typedef n Parameter n Struct n Struct element n Macro unsigned int strlen( const char * p ){... } unsigned int power_of_two( unsigned int pow ); void f( void ){ int n, x; n = strlen( ”Hello world” ); x = power_of_two( n ); }

W 1 L 1 sh 21 Reading a small function void f( int a, int b ){ g( a ); while( b-- ){ (void) h(); } n What does this function do? n Why does it what it does? n Is it correct? n When and why should you call it? n What parameter values should you pass to it?

W 1 L 1 sh 22 Now try again void ps2_command_send_replies_ignore( int command unsigned int n_replies ){ ps2_byte_send( command ); while( n_replies-- ){ (void) ps2_byte_receive(); } n What does this function do? n Why does it what it does? n Is it correct? n When and why should you call it? n What parameter values should you pass to it?

W 1 L 1 sh 23 Choosing names n Old code (and old interfaces!) often used very short names. You know why? (NOT because they liked short names) n Names must help the reader to understand the code. n (note: the programer himself is the first reader!) What's in a name? that which we call a rose By any other name would smell as sweet;

W 1 L 1 sh 24 Guiding principles n Use english. n A thing must be a noun. n A pure function (no side effects) must be a noun. n A function that achieves something (side effects) must be a verb. Predicates must be constin ‘is’ ( is_even(), is_authorized(), is_active_alarm(), string_is_valid_integer() ). n A name must document its interface (use), not its internals (implementation). n Multi-word names: n The words must be separated. n The order of the words is larger-thing-first.

W 1 L 1 sh 25 Choosing a name When applicable and/or needed: n Tell what it is or does. (print, count, select) n Tell what it returns. (sum, average, first, key, password) n Tell what it requires (parameters: width, xy, name, list) n Tell what type it returns (char, short, int, long) n Tell what types it needs (char, string)

W 1 L 1 sh 26 What is wrong? x = lowerchar( y ); n Lower and char are not separated n Name does not contain a verb What does this function do, 1. Test whether y is a lowercase character? 2. Converter y to a lowercase character? Q: What would be a good name in each of the two cases?

W 1 L 1 sh 27 Separating the words in a name n Most programming languages do not allow spaces in identifiers.  n Justputitalltogetherwithoutseparatorsisnotagoodidea n INTHEBEGINNINGPRINTERSHADNOLOWERCASE n camelCaseIsNamedAfterTheHumpsOfACamel n PascalCaseAlsoCapitalizesTheFirstLetter n Adding_Underscores_Makes_Reading_Easier n but_with_underscores_who_needs_the_captials n Use capitals (only) when needed: TCP_checksum_calculate()  Use lowercase and underscores, uppercase only when needed.  Don’t use a leading underscore, this is often used to distinguish a ‘special name’.

W 1 L 1 sh 28 Libnds keys input functions Comments please! ; select “ Keypad and Touch pad ”

W 1 L 1 sh 29 ‘errors’ n camelCase. n Keys... except for scanKeys (but touchRead does have the verb last!) n keysDownRepeat should not contain a verb! n keysSetRepeat set should be last uint32 keys_current( void ); uint32 keys_down( void ); uint32 keys_down_or_repeating( void ); uint32 keys_held( void ); void keys_repeat_set_delay_interval( u8 delay, u8 interval ); uint32 keys_up( void ); void keys_scan( void ); void touch_read( touch_position *position ); n Do you see what those int32’s mean?? n The explanations are not realy helpful 

W 1 L 1 sh 30 Read Book n Chapter 4 n Chapter 11 n Paragraph 17.5

W 1 L 1 sh 31 Assignment Go to Make a report (in word) with a three-column table of all function in the following sections. Column one contains the original function name, column two contains the name you would give the functuion, according to the naming conventions exmplaing in the lesson, column three contains your motivation for choosing that name. You might need to check the documentation of a function to find out what it realy does. Sections: 1. Special Controllers 2. Key input system 3. Sound and MP3 functions