Learning to Program with Haiku

Similar documents
Learning to Program with Haiku

1 Getting used to Python

An Introduction to Python (TEJ3M & TEJ4M)

printf( Please enter another number: ); scanf( %d, &num2);

Chapter 6: The C Preprocessor

Use of scanf. scanf("%d", &number);

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Chapter 15 Debugging

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Appendix: Common Errors

Lecture 4 CSE July 1992

Arithmetic Expressions in C

Programming in ROBOTC ROBOTC Rules

Have examined process Creating program Have developed program Written in C Source code

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

Topic 6: A Quick Intro To C

Learning to Program with Haiku

Lecture 2: C Programming Basic

Chapter 2. Lexical Elements & Operators

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Chapter 11 Introduction to Programming in C

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

GCC: the GNU Compiler Collection

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Troubleshooting Maple Worksheets: Common Problems

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Probably the best way to start learning a programming language is with a program. So here is our first program:

COSC121: Computer Systems: Runtime Stack

UNIT-2 Introduction to C++

Computer Programming : C++

Code Convention and version control help us to success in Botball

Introduction to C++ CS 1: Problem Solving & Program Design Using C++

Chapter 11 Introduction to Programming in C

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

M2 Instruction Set Architecture

Chapter 11 Introduction to Programming in C

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Lab # 2. For today s lab:

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Learning to Program with Haiku

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

GCC: the GNU Compiler Collection

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Tutorial No. 2 - Solution (Overview of C)

Shorthand for values: variables

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Full file at C How to Program, 6/e Multiple Choice Test Bank

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

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

Compiling Regular Expressions COMP360

The for Loop. Lesson 11

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Programming I Laboratory - lesson 01

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Chapter 2, Part I Introduction to C Programming

COMP 202 Java in one week

QUIZ. What are 3 differences between C and C++ const variables?

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

The development of a CreditCard class

Project Data: Manipulating Bits

C / C++ Coding Rules

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

The C standard library

QUIZ. Source:

How to approach a computational problem

CS Software Engineering for Scientific Computing. Lecture 5: More C++, more tools.

Lecture 5: C programming

Chris' Makefile Tutorial

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Shut the Box. By the time you are done with this activity, you and your team should be able to:

Java/RealJ Troubleshooting Guide

Programming in C in a UNIX environment

Programming with Haiku

Here's how you declare a function that returns a pointer to a character:

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

The semicolon [ ; ] is a powerful mark of punctuation with three uses.

Transcription:

Learning to Program with Haiku Lesson 2 Written by DarkWyrm All material 2010 DarkWyrm

Our first couple of programs weren't all that useful, but as we go along, you'll find that you can do more and more with your programs. This time around we're going to look at two main concepts: comments, and the different stages the compiler goes through to generate your program, and we'll also learn a little about debugging your code. Comments are notes that you put into the code. They have many uses, such as clarifying a certain section of code, providing warnings, or temporarily disabling a section of code. See the below example for how they can be used. Inherited from C is the multiline comment. They start with /* and end with */ in a way similar to parentheses or curly braces with one difference: you can't put a multiline comment inside another one. /*----------------------------------------------------------------------- RedButton.cpp This code is an example of how the compiler will complain if you use a function that it doesn't recognize. Now we're going to take a step back and look at the process the compiler goes through when building your program. This is important to understand because there are different kinds of errors that can be made when writing code and knowing something about the process will help you find them more easily. When a program is built from source code, there are four tools that operate on it before it is an executable: the preprocessor, the compiler, the assembler, and the linker. Stage 1: The Preprocessor The preprocessor accepts raw source code as input and does a little massaging before the code is sent to the compiler. It removes comments and handles #include directives by inserting the contents of the included header file into the code. There are other directives which it handles that will be discussed later.

Stage 2: The Compiler The compiler translates the C++ code into Assembly language. Assembly is much, much closer to the instructions which the computer understands while still being human-readable. It is also much harder to write programs and is specific to the processor for which it is written. Stage 3: The Assembler The assembler creates object code from the Assembly code created by the compiler and places them in object files which have a.o extension. Object code is the actual machine-executable instructions used by the computer to run your program. It's not quite ready to run, however. In this state the object files used to make your program are a lot like a set of puzzle pieces that are ready to be put together. Stage 4: The Linker The linker pieces together your object files along with any libraries that they use into an executable program. Debugging By nature of programmers being human, they make mistakes and lots of them. Writing code and debugging go hand-in-hand and often done at the same time. As such, we will be learning about how to debug programs as we learn about writing them. Bugs come in two types: syntactic and semantic. Syntactic bugs are easy to find because the compiler finds them for us. These are problems like capitalization errors, missing or extra parentheses, and mistyped function names. Semantic errors are often harder to find because they are errors in the logic of perfectly legal code. A semantic error in English would be The oxygen censor on my car needed to be replaced the sentence is perfectly legal and correctly constructed, but the word needed is sensor, not censor. Examples of semantic errors are extra semicolons in certain places, adding the wrong amount to a number, and making assumptions about the return value of a function. Let's take a look at a few simplified examples of common errors: Example 1 #include <stdio.h> foo.cpp:6: error: expected declaration before token

What we have here is an extra curly brace. gcc has given us a fairly cryptic error, as usual, but it has also given us two clues: the file name and the line number. The line number given by gcc and the actual location of the error are not always the same, but in this case they are. At this point, you might be wondering, What in the world is a token, genius? A token is any language element. Just as a regular written language has words and punctuation, so do computer languages. Just as two commas in a row are a punctuation error in the English language, having an extra curly brace is a C++ punctuation error. Example 2 /*----------------------------------------------------------------------- RedButton.cpp /*This code is an example of how the compiler will complain if you use a function that it doesn't recognize.*/ foo.cpp:7: error: expected unqualified-id before -- token This is an example of how the line number for the error is not the same place as the actual error. The complaint is about the dashes at the end of the multiline comment at the top. It is caused, however by nesting a multiline comment inside another one. The preprocessor removes all comments, so what the compiler sees is this: The compiler doesn't know what to do with the dashed line and complains.

Example 3 int Main(void) /usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start': /build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.s:115: undefined reference to `main' /tmp/ccv39cuo.o:(.eh_frame+0x12): undefined reference to ` gxx_personality_v0' collect2: ld returned 1 exit status This is an error of a different kind. Remember that main() needs to be defined in every program? We didn't we defined Main(). The program is otherwise valid, so it compiled just fine, but when the linker attempted to piece it all together, it couldn't find the one required function and threw a hissyfit. Any time you see an error containing undefined reference to, you have a linker error. Resolving undefined reference linker errors isn't generally difficult. It usually means one of two things: you forgot to link in a library that you used, or a source code file was accidentally left out when your program was built.