ENGR101: Lecture 7. C language tools and intro to C programming. March ENGR101: Lecture 7 March / 20

Similar documents
Assignment 3: Binary numbers, data types and C Programs Due 11:59pm Monday 26th March 2018

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

As stated earlier, the declaration

Work relative to other classes

Week 3 Lecture 2. Types Constants and Variables

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

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

CC112 Structured Programming

DECLARAING AND INITIALIZING POINTERS

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

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Chapter 4. Section 4.5 Initialization of Arrays. CS 50 Hathairat Rattanasook

Computer Systems Programming. Practice Midterm. Name:

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

C introduction: part 1

CAAM 420 FALL 2012 Lecture 9. Mishael Owoyemi

Programming in C++ 6. Floating point data types

Arithmetic Expressions in C

C / C++ Coding Rules

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

A Fast Review of C Essentials Part I

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

CSE 303 Lecture 8. Intro to C programming

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Visual C# Instructor s Manual Table of Contents

EL2310 Scientific Programming

The Compilation Process

Preview from Notesale.co.uk Page 6 of 52

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Variables and literals

BSM540 Basics of C Language

Motivation was to facilitate development of systems software, especially OS development.

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

EL2310 Scientific Programming

Full file at

CSCI 2132 Software Development. Lecture 8: Introduction to C

Topic 6: A Quick Intro To C

Introduction to C An overview of the programming language C, syntax, data types and input/output

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

First of all, it is a variable, just like other variables you studied

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Motivation was to facilitate development of systems software, especially OS development.

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Data Types. Data Types. Integer Types. Signed Integers

Creating, Compiling and Executing

Informatica e Sistemi in Tempo Reale

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Fundamentals of Programming

BLM2031 Structured Programming. Zeyneb KURT

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Computer Systems Lecture 9

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

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

C Language, Token, Keywords, Constant, variable

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

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++

COMP s1 Lecture 1

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Introduction to Programming in Turing. Input, Output, and Variables

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

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

C and Programming Basics

C++ Support Classes (Data and Variables)

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Fundamental of Programming (C)

CpSc 1111 Lab 4 Formatting and Flow Control

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

1.00 Lecture 4. Promotion

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Lecture 3. More About C

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS240: Programming in C. Lecture 2: Overview

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

AN INTRODUCTION PROGRAMMING. Simon Long

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Basic Assignment and Arithmetic Operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Programming refresher and intro to C programming

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

CS112 Lecture: Primitive Types, Operators, Strings

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Transcription:

ENGR101: Lecture 7 C language tools and intro to C programming March 2018 ENGR101: Lecture 7 March 2018 1 / 20

Not how to play piano but how to open it... Here is an example of simple (very) C program. # include <stdio.h> int main () { printf (" Hello world \n"); } Listing 1: Prints Hello world For now we will not look at what all these mysterious things mean. Much more about it soon. We will just try to make this program to run. ENGR101: Lecture 7 March 2018 2 / 20

C programming on ECS system There are several ways to write and run C language program on ECS machines. Using Integrated Development Environment (IDE). It is graphical. Using Command Line Interface (CLI) ENGR101: Lecture 7 March 2018 3 / 20

Graphical - Geany IDE(Integrated Development Environment) How to start with Geany: Go to System menu. In Development option find Geany and click it. Geany should open. Start typing your program. Save your program text in file with extension.cpp. It is important that you specify the extension - Geany can work with several languages and decides language tools on base of this extension. ENGR101: Lecture 7 March 2018 4 / 20

Program text to machine codes: Compile, build and run Figure: Compile, build and run in Geany Using Build menu option you can convert text of your program into machine codes (executable file) and run it. Somewhat similar to to RAM in LMC It is two stages process: Compile - convert some program text into object code. Object code can not run(execute). Build (sometimes called link) - takes object codes (usually from several programs) and converts object code into true executable. Execute - it runs the program. ENGR101: Lecture 7 March 2018 5 / 20

Geany messages during compiling Notice that as we click Compile and Build in Geany menu, messages are displayed. In Geany menu click: Build > Compile. Message:g++ -Wall -c m1.cpp. g++ compiler takes m1.cpp file as an input and compiles it (-c). Compiler gives all warnings: -Wall In Geany menu click Build > Build. Message: g++ -Wall -o m1 m1.cpp. Compiler takes object files(-o) for m1.cpp program and produces executable m1(name can be any of your choice) ENGR101: Lecture 7 March 2018 6 / 20

Another way to compile, build and run If you type in all these commands in Command Line windows you achieve same result as clicking Compile and Build buttons in Geany. To run the program executable type executable name preceded by./(way to explain to the system that it is custom-made program). ENGR101: Lecture 7 March 2018 7 / 20

What is happening when we compile? Roughly equivalent to clicking To RAM LMC button. Source code #include <stdio.h> int main() { printf("hello world\n"); int a; } Object file Mix of text and binary In any text editor - mostly giberish. Executable Processor codes Compile Build At compile stage text file is converted into mix of text and machine codes. Program text is checked for simple errors. Object file is produced - contains objects of the program. At build stage object file is converted into machine code. Processor can execute machine code. ENGR101: Lecture 7 March 2018 8 / 20

Let us step back and try to compile and run empty file. Compile - OK Build - (.text+0x20): undefined reference to main It needs main() - which is entry point of the program (like memory slot 0 in LMC). It is a function - block of code which starts with opening curly bracket and finishes with closing one (more on it later). OK, let us five it main()... main () { } Listing 2: Caption Still complaining: me1.cpp:2:6: warning: ISO C++ forbids declaration of main with no type [-Wreturn-type] ENGR101: Lecture 7 March 2018 9 / 20

Return type It is called a function because it gives some number back (remember calculus functions?). This number should be of some type. Convention for main is that it returns int. Usually if program finishes OK it should return 0. -1,-2 etc. indicate errors (file not found, no network, etc) ENGR101: Lecture 7 March 2018 10 / 20

Minimum program text which compiles and runs int main () { } Listing 3: Caption Not that it does much... ENGR101: Lecture 7 March 2018 11 / 20

Quiz time... Can I have two main() functions? Let s vote... 1 Yes 2 No Let s check... ENGR101: Lecture 7 March 2018 12 / 20

How to print something on the screen? print() seems to be reasonble guess.. Geany suggests printf instead of print. Listing 4: Caption int main () { printf (" Hello everybody "); } Not much luck... print1.cpp:4:26: error: printf was not declared in this scope It is because almost all C functionality is defined in additional programs, called libraries. Even such simple thing as printing requires library. For printing we need library stdio - standard input-output (more on libraries later). ENGR101: Lecture 7 March 2018 13 / 20

printf() f in printf stands for formatted. 1 printf - print formatted text. You should specify how you want to display number. Options are: %d - prints decimal integer %f - prints float number %c - prints character %s - prints string These are called format specifiers. 2 It prints string before comma replacing all format specifiers with values which come after comma. printf("somethin %d, more %f",12,7.6); ENGR101: Lecture 7 March 2018 14 / 20

Quiz? We need to display decimal number and string. What is right format? 1 Option 1: Listing 5: Caption printf ("v1 =%f v2 =%c", 12, " ddff "); 2 Option 2: Listing 6: Caption printf ("v1 =%d v2 =%s", 12, " ddff "); 3 Option 3: Listing 7: Caption printf ("v1 =%f v2 =%s", 12, " ddff "); ENGR101: Lecture 7 March 2018 15 / 20

Variables Variables store values which can change (unlike variables in calculus). Program is not an equation, it is sequence of actions. One line after another (remember LMC?) program flow a = 1;...... a = 5; a becomes 1 a becomes 5 OK, let us try that... ENGR101: Lecture 7 March 2018 16 / 20

Variable types error: a was not declared in this scope a is not even recognized as a variable. C is strongly typed language. All variables should be of certain type. int a = 0;, for example, works just fine. int is a type. What are types? ENGR101: Lecture 7 March 2018 17 / 20

Types All variables are stored in memory (as binary numbers). Type of the variable determines how many bits (Bytes) this variable takes in memory. Memory to store variable a Memory a b One Byte ENGR101: Lecture 7 March 2018 18 / 20

Variable types Type specifies how many bits particular number should fit in. There are many types in C. Common types: char - 8 bits (1 Byte). Maximum value is 1111 1111 (binary). int - number of Bytes depends upon hardware and operating system. Usually 4. float - usually 4 Bytes. Some bytes store fraction (part after.) and exponent takes rest of 32 bits. ENGR101: Lecture 7 March 2018 19 / 20

Questions? ENGR101: Lecture 7 March 2018 20 / 20