Lecture 3. More About C

Similar documents
Operators & Expressions

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 2. Lexical Elements & Operators

Computer System and programming in C

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

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

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

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

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

C: How to Program. Week /Mar/05

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

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

Work relative to other classes

Programming in C++ 5. Integral data types

Arithmetic Operators. Portability: Printing Numbers

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 12 Variables and Operators

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Programming and Data Structures

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Variables and literals

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Declaration and Memory

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Operators and Expressions:

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

Basics of Programming

Lexical Considerations

Expression and Operator

Chapter 2 - Introduction to C Programming

Operators And Expressions

Chapter 2: Using Data

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Chapter 4: Basic C Operators

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

The Design of C: A Rational Reconstruction (cont.)

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

EL2310 Scientific Programming

Basics of Java Programming

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Introduction to C. Systems Programming Concepts

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

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

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

1 Lexical Considerations

Arithmetic type issues

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

The C language. Introductory course #1

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CMPT 125: Lecture 3 Data and Expressions

LECTURE 3 C++ Basics Part 2

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

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Expressions and Precedence. Last updated 12/10/18

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Programming for Engineers Iteration

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Fundamental of Programming (C)

Lexical Considerations

UNIT- 3 Introduction to C++

Language Reference Manual simplicity

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

Lesson 3 Introduction to Programming in C

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Operators in C. Staff Incharge: S.Sasirekha

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

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

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

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

COMP Primitive and Class Types. Yi Hong May 14, 2015

QUIZ: What value is stored in a after this

Lecture 3 Tao Wang 1

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

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

JAVA OPERATORS GENERAL

Chapter 2: Basic Elements of C++

Engineering Computing I

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

Course Outline Introduction to C-Programming

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

Chapter 3: Operators, Expressions and Type Conversion

Introduction to C Language

Reserved Words and Identifiers

SECTION II: LANGUAGE BASICS

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

Transcription:

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants are values of basic types 0, 123.6, "Hello" Variables name locations that hold values i, sum Expressions compute values/change variables sum = sum + i Statements control a program s flow of control while, for, if-else Functions encapsulate statements main Modules collections of related variables & functions a.k.a. compilation units Programming environment Text editor (emacs, vi, sam) Compiler (lcc, cc, gcc) Linker/loader (ld); used rarely, because lcc runs it Debugger (gdb)

Types A type determines a set of values, and what operations can be performed on those values Scalar types char a character ; typically a byte 8 bits int a signed integer; typically values from 2147483648 to 2147483647 unsigned an unsigned integer; typically values from 0 to 4294967295 float double single-precision floating point double-precision floating point Pointer types: much more later Aggregate types: values that have elements or fields, e.g., arrays, structures Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-2

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-3 Constant values of the scalar types Constants char a character constant (use single quotes) \035 character code 35 octal, or base 8 \x29 character code 29 hexadecimal, or base 16 \t tab ( \011, do man ascii for details) \n newline ( \012 ) \\ backslash \ single quote \b backspace ( \010 ) \0 null character; i.e., the character with code 0 int 156 decimal (base 10) constant 0234 octal (base 8) 0x9c hexadecimal (base 16) unsigned 156U decimal 0234U octal 0x9cU hexadecimal float 15.6F 1.56e1F double 15.6 plain floating point constants are doubles 1.56E1L

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-4 Variables A variable is the name of a location in memory that can hold values int i, sum; float average; unsigned count; i = 8; sum = -456; count = 101U; average = 34.5; A variable has a type; it can hold only values of that type Assignments change the values of variables sum = sum + i; changes the value of sum to -448 Variables must be initialized before they are used #include <stdio.h> int main(void) { int x; } printf("x = %d\n", x); return 0; output is undefined! 8 i -456 sum 101 count 34.5 average

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-5 Expressions Expressions use the values of variables and constants to compute new values Binary arithmetic operators take two operands produce one result + - addition, subtraction * / multiplication, division % remainder (a.k.a. modulus) Type of result depends on type of operands int i; unsigned u; float f; + i u f i int unsigned float u? unsigned float f?? float i + i specifies int addition and yields an int result int and unsigned division truncate: 7/2 is 3, but 7.0/2 is 3.5 Unary operators take one operand and produce one result - + negation, affirmation (just returns its operand s value)

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-6 Precedence and Associativity Operator precedence and associativity dictate the order of expression evaluation Precedence dictates which subexpressions get evaluated first highest unary - + binary * / % lowest binary + - -2*a + b is evaluated as if written as (((-2)*a) + b) Associativity dictates the evaluation order for expressions with several operators of the same precedence all arithmetic operators have left-to-right associativity a + b + c is evaluated as if written as ((a + b) + c) Use parentheses to force a specific order of evaluation -2*(a + b) computes -2 a + b the product of these two values

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-7 Assignments Assignment expressions store values in variables variable = expression the type of expression must be the same as the type of variable convertible to the type of variable int i; unsigned u; float f; = i u f i int int int u unsigned unsigned unsigned f float float float Augmented assignments combine a binary operator with assignment variable += expression variable -= expression sum += i is the same as sum = sum + i

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-8 Increment/Decrement Prefix and postfix operators ++ -- increment and decrement operand by 1 ++n adds 1 to n --n subtracts 1 from n Prefix operator increments operand before returning the new value n = 5; x = ++n; x is 6, n is 6 Postfix operator increments operand after returning the old value n = 5; x = n++; x is 5, n is 6 Operands of ++ and -- must be variables ++1 2 + 3++ are illegal

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-9 Idiomatic C sum.c (in sum2.c) rewritten using common idioms involving += and ++ /* Compute the sum of the integers from 1 to n, for a given n. */ #include <stdio.h> int main(void) { int i, n, sum = 0; } printf("enter n:\n"); scanf("%d", &n); for (i = 1; i <= n; i++) sum += i; printf("sum from 1 to %d = %d\n", n, sum); return 0; scanf is a form of assignment; it changes n

Statements Expression statements expression opt ; sum += i; printf("sum from 1 to %d = %d\n", n, sum); Selection statements if ( conditional ) statement if ( conditional ) statement else statement if (x > max) max = x; if (bit == 0) printf(" "); else printf("*"); switch ( expression ) { case constant : statement default : statement } Iteration statements (loops) while ( conditional ) statement while (i <= n) { sum += i; i++; } for ( expression opt ; conditional opt ; expression opt ) statement do statement while ( expression ) ; for (i = 1; i <= n; i++) sum += i; for (;;) printf("help! I m looping\n"); do { sum += i; ++i; } while (i <= n); Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-10

Statements, cont d Compound statements Others { declaration opt statement } for (j = 0; j < n; j = j + 1) { int bit = (rand()>>14)%2; if (bit == 0) printf(" "); else printf("*"); } return expression opt ; return; return 0; return -2*(a + b); break ; continue ; Keywords (if else while do for switch case ) cannot be used as variables Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-11

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-12 Conditional Expressions A conditional expression is any expression that evaluates to zero or nonzero There is no Boolean type; nonzero is true, zero is false Relational operators compare two arithmetic values (or pointers) and yield 0 or 1 < <= less than, less than or equal to ==!= equal to, not equal to > >= greater than, greater than or equal to Logical connectives conditional 1 && conditional 2 1 if both conditionals are nonzero; 0 otherwise conditional 1 conditional 2 1 if either conditional is nonzero; 0 otherwise conditionals are evaluated left-to-right only as far as is necessary : && stops when the outcome is known to be zero stops when the outcome is known to be nonzero Associativity: left to right; precedence: below the arithmetic operators highest arithmetic operators < <= >= > a + b < max max == 0 && a == b ==!= is interpreted as if written && ((a + b) < max) (max == 0 && (a == b)) lowest