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

Similar documents
Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Lecture 3. More About C

Dynamic Memory Allocation and Command-line Arguments

Variables and literals

EL2310 Scientific Programming

File Descriptors and Piping

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

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

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

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Introduction to C. Systems Programming Concepts

ECEN 449 Microprocessor System Design. Review of C Programming

CSE 303 Lecture 8. Intro to C programming

Topic 6: A Quick Intro To C

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

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

Computer System and programming in C

CpSc 1111 Lab 4 Formatting and Flow Control

3. Types of Algorithmic and Program Instructions

Basic Assignment and Arithmetic Operators

File Handling in C. EECS 2031 Fall October 27, 2014

Arithmetic type issues

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Reserved Words and Identifiers

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

Programming. Elementary Concepts

C introduction: part 1

These are reserved words of the C language. For example int, float, if, else, for, while etc.

An overview of Java, Data types and variables

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

Functions. Arash Rafiey. September 26, 2017

The C language. Introductory course #1

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Chapter 12 Variables and Operators

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

High Performance Computing

C - Basics, Bitwise Operator. Zhaoguo Wang

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

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Type Conversion. and. Statements

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Basic Types and Formatted I/O

Programming refresher and intro to C programming

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

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

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

BSM540 Basics of C Language

Work relative to other classes

Introduction to C Language

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

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

Final CSE 131B Spring 2004

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Number Systems, Scalar Types, and Input and Output

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

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

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

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Programming. Syntax and Semantics

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

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

Procedures, Parameters, Values and Variables. Steven R. Bagley

COSC2031 Software Tools Introduction to C

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

CSI33 Data Structures

This is CS50. Harvard University Fall Quiz 0 Answer Key

6.096 Introduction to C++ January (IAP) 2009

Syntax and Variables

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

CPSC 213. Introduction to Computer Systems. Numbers and Memory. Unit 1a

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming and Data Structures

C++ Modern and Lucid C++ for Professional Programmers

Fall 2017 CISC124 9/16/2017

Advanced Computer Programming

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

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

1.3b Type Conversion

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

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

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Programing in C. Pierre-Alain FOUQUE

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

C programming basics T3-1 -

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

C Language Summary (Continued)

A brief introduction to C programming for Java programmers

Transcription:

C-Programming CSC209: Software Tools and Systems Programming Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Adapted from Dan Zingaro s 2015 slides. Week 2.0 1 / 19

What is C? 1. Structured, like a high-level language. 2. Grants you machine access, like a low-level language. 3. A small language (e.g. 49-page reference manual), extendable with libraries. 4. Permissive: assumes you know what you re doing. 5. Good: efficient, portable, powerful (data types, operators), and flexible. 6. Bad: easy to make errors, terse code, little support for modularization. 2 / 19

Hello World HelloWorld.c 1 # include <stdio.h> 2 int main () { 3 printf (" Hello, World!\n"); 4 return 0; 5 } 1 $ls 2 HelloWorld. c 3 $gcc -o HelloWorld HelloWorld. c 4 $./ HelloWorld 5 Hello World! 6 $_ 3 / 19

For Loops sandbox.c 1 # include <stdio.h> 2 int main () { 3 for ( int counter = 0; counter <= 10; counter ++) { 4 printf ("%d ", counter ); 5 } 6 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 0 1 2 3 4 5 6 7 8 9 10$_ Note $A && B means: Execute A then if successful execute B. 4 / 19

While 1 while ( condition ) { 2 <code > 3 } Do-While 1 do { 2 <code > 3 } while ( condition ); Note condition is some predicate evaluating to 1 (True) or 0 (False). 5 / 19

If-Then-Else 1 if ( predicate ) { 2 <code > 3 } else ( predicate ) { 4 <code > 5 } else ( predicate ) { 6 <code > 7 }... 8 } else { 9 <code > 10 } 6 / 19

Functions 1 // printf is not include by default 2 # include <stdio.h> 3 4 // function prototypes 5 int gcd ( int x, int y); 6 7 int main ( void ) { 8 // variable declarations 9 int i; 10 for (i = 0; i < 5; i ++) { 11 printf (" GCD of 12 and %d is %d.\n", i, gcd (12, i)); 12 } 13 return 0; 14 } 7 / 19

Functions 1. The printf function is not built-in to C #include: add declarations of external functions 2. main(void) returns an int, the exit status. 3. Functions must be: declared (which tells compiler how to use function), and defined (which creates the function). 4. Functions should be declared (not necessarily defined) before they are called. 8 / 19

Defaults Uninitialized variables do not initialize to nice values like 0 or "" rather to whatever happens to be in memory. sandbox.c 1 # include <stdio.h> 2 int main ( void ) { 3 int i; 4 printf ("%d\n", i); 5 return 0; 6 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 121069622 3 $_ 9 / 19

Warnings Use -Wall when compiling to get all the help you can get. The option -g includes symbols for debuggers. 1 $gcc - Wall -g -o sandbox sandbox. c 2 HelloWorld. c :5:18: warning : variable 'i' is uninitialized when used here [- Wuninitialized ] 3 printf ("%d\n", i); 4 ^ 5 1 warning generated. 6 $_ 10 / 19

Integers 1 # include <stdio.h> 2 int main ( void ) { 3 int i = 38; long el = 38 L; 4 int hex = 0 x2a ; int oct = 033; 5 6 printf ("i = %d, el = %ld, hex = %d, oct = %d\n", i, el, hex, oct ); 7 8 return 0; 9 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 i = 38, el = 38, hex = 42, oct = 27 3 $ 11 / 19

Doubles 1 # include <stdio.h> 2 int main ( void ) { 3 double d1 = 0.3, d2 = 3.0, d3 = 6.02 e23 ; 4 5 printf ("d1 = %f, d2 = %f, d3 = %e\n", d1, d2, d3); 6 printf ("d1 = %f, d2 = %e, d3 = %f\n", d1, d2, d3); 7 return 0; 8 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 d1 = 0.300000, d2 = 3.000000, d3 = 6. 020000 e +23 3 d1 = 0.300000, d2 = 3. 000000 e +00, d3 = 601999999999999995805696. 000000 12 / 19

Literals and Types Literal Value Type 38 38 int 38L 38 long int 0x2a (hex) 42 int 033 (octal) 27 int 38.0 38.0 double 38.0f 38.0 float 13 / 19

More about ints Many C values are really ints: 1. Boolean values are ints (0 means false, nonzero means true). 2. Characters are ints (ASCII codes) (e.g. 'a' has value 97, '\n' has value 10) 3. signed vs. unsigned types are different forms of ints. 4. char, int, long, etc. are just different sizes of ints. 14 / 19

Data Type Conversion In an assignment statement, the expression on the right side is converted to the type of the variable on the left 1 char c; 2 int i = c; /* c is converted to int */ 3 double d = i; /* i is converted to double */ provided the variable s type is at least as wide as the expression s type 1 char c = 500; /* compiler warning */ 2 double d1 = 4.5; 3 int k = d1; 4 printf ("c = %c, k = %d\n", c, k); 5 /* c is a strange symbol, k = 4*/ 15 / 19

What happens when the following code is executed? 1 # include <stdio.h> 2 int main ( void ) { 3 char c = 127; 4 int d; 5 printf ("c = %d\n", c); 6 c ++; 7 d = 512 / c; 8 printf ("c = %d, d = %d\n", c, d); 9 return 0; 10 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 c = 127 3 c = -128, d = -4 16 / 19

Results of Operations Operations on values of the same type produce results of that type. For instance, if we divide two integers, we perform integer division (dropping the fraction): 10 / 3 = 3 10.0 / 3.0 = 3.333333... Whereas if we perform an operation with one integer and one floating-point number, the result is a floating-point number: 4.3 + 5 = 9.3 17 / 19

Casting A cast is used to change the type of a value. For instance, (int)2.95 converts the floating-point number 2.95 to the int 2. The cast operator has a higher precedence than the mathematical operators (int)(5.7) + 8.9 = 5+8.9 = 13.9 and not (int)(5.7 + 8.9) = (int)14.6 = 14. 18 / 19

Halftime 1. Worksheet 1. 2. Arrays. 19 / 19