Programming I Laboratory - lesson 01

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

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

Understanding main() function Input/Output Streams

Chapter 2, Part I Introduction to C Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

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

Fundamentals of Programming Session 4

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

C: How to Program. Week /Mar/05

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 2 - Introduction to C Programming

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

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Programming for Engineers Introduction to C

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

EL2310 Scientific Programming

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

Chapter 1 & 2 Introduction to C Language

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

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

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

Tutorial No. 2 - Solution (Overview of C)

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

The Parts of a C++ Program

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

C++ Support Classes (Data and Variables)

EL2310 Scientific Programming

4. Structure of a C++ program

Chapter 1 Introduction to Computers and C++ Programming

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

Chapter 1 INTRODUCTION

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

C Introduction Lesson Outline

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

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

LESSON 4. The DATA TYPE char

IS12 - Introduction to Programming. Lecture 7: Introduction to C

Variables and literals

Some Computer Preliminaries

Work relative to other classes

Chapter 2: Programming Concepts

EC 413 Computer Organization

1.1 Introduction to C Language. Department of CSE

EL2310 Scientific Programming

A Fast Review of C Essentials Part I

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Should you know scanf and printf?

COMP 110 Project 1 Programming Project Warm-Up Exercise

C++ Programming Applied to Robotics, Mark Aull Lesson 2: Intro to Visual Studio, debugger Intro to C++, variables, conditionals, loops, strings

CS201 - Introduction to Programming FAQs By

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Programming with C++ as a Second Language

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

My First Command-Line Program

CS Prof J.P.Morrison

Compiler Drivers = GCC

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

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

Technical Questions. Q 1) What are the key features in C programming language?

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

CHAPTER 2 A FIRST LOOK AT C++ SOURCE PROGRAMS

Lab # 2. For today s lab:

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Lab: Supplying Inputs to Programs

AMCAT Automata Coding Sample Questions And Answers

Section 2.2 Your First Program in Java: Printing a Line of Text

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CSCI 2132 Software Development. Lecture 8: Introduction to C

Chapter 2. Lexical Elements & Operators

CS201 Latest Solved MCQs

Fundamental C# Programming

BLM2031 Structured Programming. Zeyneb KURT

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Java Bytecode (binary file)

STUDENT LESSON A12 Iterations

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

UNIT-2 Introduction to C++

Chapter 1 Getting Started Structured Programming 1

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

Lesson 1: Hello, world! Line by line explanation

Chapter 2. Basics of Program Writing

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

C++ PROGRAMMING BASICS

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Tutorial 2: Compiling and Running C++ Source Code

COMP1917: Computing 1 1. Introduction

3. Simple Types, Variables, and Constants

Transcription:

Programming I Laboratory - lesson 0 Introduction in C++ programming environment. The C++ program structure. Computer programs are composed of instructions and data. Instructions tell the computer to do things, such as to add and subtract. Data is what the computer operates on (such as the numbers that are added and subtracted). A solution to a problem is called an algorithm. It describes the sequence of steps to be performed for the problem to be solved. To be intelligible to a computer, an algorithm needs to be expressed in machine language, a language understood by it. Programs expressed in the machine language are said to be executable. A program written in any language needs to be translated to the machine language before it can be executed. The Development Environment The compiler may have its own built-in text editor, or you may be using a commercial text editor or word processor that can produce text files. The important thing is that whatever you write your program in, it must save simple, plain-text files, with no word processing commands embedded in the text. Examples of safe editors include Windows Notepad, etc. The files you create with your editor are called source files, and for C/C++ they typically are named with the extension.cpp,.cp, or.c. (We will name all the source code files with the.cpp extension.) Compiling the Source Code To turn your source code into a program, you use a compiler. How you invoke your compiler, and how you tell it where to find your source code, will vary from compiler to compiler; check your documentation. Compilers may do things slightly differently. After your source code is compiled, an object file is produced. C/C++ programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that were supplied with your compiler, that you purchased separately, or that you created and compiled. All C/C++ compilers come with a library of useful functions (or procedures) and classes that you can include in your program. The steps to create an executable file are:. Create a source code file, with.c or.cpp extension.. Compile the source code into an object file.. Link your OBJ file with any needed libraries to produce a file with the.exe extension that is an executable program. Figure

Programming I Laboratory - lesson 0 A Simple C++ Program Figure Traditional programming books begin by writing the words Hello World to the screen, or a variation on that statement. Preprocessing directive Listing Error! No text of specified style in document.. First.cpp includes an header file that contains definitions for input/output functions Body of function main(). printf("hello World!\n"); Header of function main() Comments: First line uses the preprocessor directive #include to include the contents of the header file stdio.h in the program. stdio.h is a standard C/C++ header file and contains definitions for input and output. Line defines a function called main(). A function may have zero or more parameters; these always appear after the function name, between a pair of parenthesis. All C++ programs must have exactly one main function. When your program starts, main() is called automatically. This function is unique (it cannot be overloaded); main is a reserved word, but we can use as different identifier Main, MAIN, so on (languages C/C++ are case-sensitive); main is the starting point of execution for all C and C++ programs function main can have parameters or not; A program usually stops executing at the end of main if it is not a decision to stop it in other point, e.g. using the statement return or function exit. The word void appearing between the brackets indicates that main has no parameters.

Programming I Laboratory - lesson 0 A function may also have a return type; this always appears before the function name. The return type for main is int (an integer number). In line, the brace marks the beginning of main body. Line is a statement. A statement is a computation step which may produce a value. It consist in the call of the function printf(), defined in stdio.h. The end of a statement is always marked with a semicolon (;). The printf() function causes the string"hello World\n" to be sent to the output stream (stdout - standard output usually means your computer monitor screen). A string is any sequence of characters enclosed in double-quotes, like Hello world. The last character in this string (\n) is a newline character (is similar to a carriage return on a type writer). In line, the brace marks the end of the body of main. The preprocessor runs before your compiler each time the compiler is invoked. The preprocessor translates any line that begins with a pound symbol (#) into a special command, getting your code file ready for the compiler. Try running FIRST.EXE; it should produce the dialog : Dialog Since C program execution happen in milliseconds, we cannot see the output ( Hellow World ) on the screen. That s because Program output window closes immediately after execution. Listing. First.cpp - modified 8 9 #include <conio.h> printf("hello World!\n"); getch(); Contains definition of printf() Contains definition of getch() So we need a way to stop the program before the output window closing. We can use a function named getch() that will wait for key press in our program execution and hence, we can see the result Hellow World. After viewing the output we can close the output by pressing any key in the keyboard. Comments: To use getch() we need to include the header file conio.h that contains the definition of this function.

Programming I Laboratory - lesson 0 Compiler Errors Unfortunately, almost every program, no matter how trivial, can have errors, or bugs. Some bugs will cause the compile to fail, some will cause the link to fail, and some will only show up when you run the program. Whatever type of error or bug you find, you must fix it, and that involves editing your source code, re-compiling and re-linking, and then re-running the program. Compilers will tell you what you did wrong (they show error or warning code and its description), they'll point you to the exact place in your code where you made the mistake, even suggest a remedy. Listing is a demonstration of compiler error: Listing. Syntax error example printf("hello World!\n") Recompile your program and you should see in Message window an error that looks similar to the Dialog. Messages are written with the message class first, followed by the source file name and line number where the error was detected, and finally with the text of the message itself. Those messages are very useful tools to debug the program. Dialog. Error messages Compiling LISTING.CPP: C:\Test\listing.cpp() : error C: syntax error : missing ';' before 'return' C:\Test\listing.cpp() : fatal error C00: unexpected end of file found Error executing listing.exe. listing.exe - error(s), 0 warning(s) Compiler Errors and Warnings: Compile-time error messages indicate errors in program syntax, command-line errors, or errors in accessing a files or memory. When compile-time errors occur, the compiler completes the current phase of the compilation and stops. When fatal compile-time errors happen, compilation stops completely. If an error occurs, you must fix the error and restart compilation. Run-time errors occur after the program has successfully compiled and is running. Run-time errors are usually caused by logic errors in program code. Warnings indicate that conditions which are suspicious but legitimate exist. Warnings do not stop compilation. They must be attentively analyzed to decide if it is necessary to eliminate them or not. Linker Errors do not make the linker stop or cause.exe files to be deleted. When such errors happen, fix the error and re-link. A fatal linker error stops the linker immediately. In such a case, the.exe file is deleted.

Programming I Laboratory - lesson 0 Using Comments A source code can include text that is ignored by the compiler. This is called comment. Comments can come in two forms: multi-line comments (C style) and single-line comments (C++ style). C-style comments are framed between characters slash-star (/*) and star-slash (*/).The slash-star comment mark tells the compiler to ignore everything that follows until it finds a starslash (*/) comment mark. Every (/*) must be matched with a closing (*/). C++-style commentsuses the double-slash (//) characters and tells the compiler to ignore everything that follows this comment, until the end of the line. Many C++ programmers use the C++-style comment most of the time, and reserve C-style comments for blocking out large blocks of a program. The comments provide an easy way to set off specific parts of code, insert explanations about the code, as well as marking various parts of your code. As a general rule, the overall program should have comments at the beginning, telling you what the program does. Each function should also have comments explaining what the function does and what values it returns. Any statement in your program that is obscure or less than obvious should be commented as well. Listing demonstrates the use of comments, showing that they do not affect the processing of the program or its output. Listing. Using comments 8 9 0 8 9 0 /******************************************************** * Comments.CPP * * This is my program that demonstrates * * the use of comments. * ********************************************************/ int main (void) /* this is a comment on many lines and it extends until the closing star-slash comment mark */ printf("hello World\n"); //it prints on screen a message // this comment ends at the end of the line printf("hello World, again\n"); /*it prints on screen a message */ // this comment ends at the end of the line Comments: The comments on lines through, through are completely ignored by the compiler; similar are the comments on lines,, 9, 0 and. So, execution of this program produces Dialog. Dialog Hello World Hello World, again