Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Windows Programming Lecture 03. Pointers and Arrays.
Rational Expressions relational operators logical operators order of precedence.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
The Machine Model Memory
Chapter 12 Variables and Operators
ECE Application Programming
INC 161 , CPE 100 Computer Programming
Rational Expressions. relational operators. logical operators
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Revision Lecture
INC 161 , CPE 100 Computer Programming
Chapter 12 Variables and Operators
Data Types, Identifiers, and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C Programming
Differences between Java and C
Introduction to Programming – 4 Operators
Expressions.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming Languages and Paradigms
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
EECE.2160 ECE Application Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
EECE.2160 ECE Application Programming
OPERATORS in C Programming
EECE.2160 ECE Application Programming
Chapter 12 Variables and Operators
Presentation transcript:

Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators operands precedence types.

Department of Electronic & Electrical Engineering Reading data... Simple IO... scanf #include { int x; scanf("%d",&x); } "%d" is a control string %d is a slot for an integer & is the address of operator &x is the memory location to place the integer (were x is stored). scanf("%d",x); common mistake

Department of Electronic & Electrical Engineering Format string (scanf)

Department of Electronic & Electrical Engineering scanf ---- WARNING --- scanf can be confusing to use especially if you do not enter your data correctly. scanf reads your input until it has filled all the slots (%s). If you have extra input characters that are not read by a scanf they will be read by the next scanf !

Department of Electronic & Electrical Engineering Example using scanf float x,y; char c; scanf("%f %c %f",&x,&c,&y); printf("%f %c %f",x,c,y); There is a slot for each parameter The number and type of parameters must match the slots

Department of Electronic & Electrical Engineering DEMO: Dr Leonard writes a program to read in some numbers and print a result. Illustrating the use of scanf and printf.

Department of Electronic & Electrical Engineering Writing data ( printf ) %f --- float e.g 34.1 %e --- with exponent 3.41e1 %d --- decimal (for int type) %c --- single character %s --- string (array of char) %p --- address / pointer We can also do: %10.4f --- print least 10 characters (maybe spaces) and print out 4 digits after the decimal point.

Department of Electronic & Electrical Engineering Summary of topics covered in this lecture. Blocks { {} {} } Statements and semicolons Variables: Names Declaring Scope. Types Addresses, memory pointers. IO. Reading and writing variables. printf scanf Format Strings (for IO e.g. " %d" ).

Department of Electronic & Electrical Engineering Expressions An expression is comprises: variables constants function calls (must return a value) operators (unary or binary) operands

Department of Electronic & Electrical Engineering Example: 2.0*x*sin(z*y) constant variable another expression! function 2.0  *x  * ( z*y  sin ) order of evaluation

Department of Electronic & Electrical Engineering Arithmetic Operators Arithmetic operators can be used with any of the fundamental types ( int char float ).

Department of Electronic & Electrical Engineering Precedence Expressions are usually evaluated from left to right (associativity) If an expression has different operators then precedence rules are applied Brackets can be used to form sub expressions (override precedence) For example arithmetic operators the * / and % operators will be evaluated before + or – e.g. 4+6/2 is equivalent to 4+ (6/2) (4-6)+2 4/6 - 2 (4/6)-2

Department of Electronic & Electrical Engineering Combining different types. Both operands the same type  results is the same. (2/3  0) integer and floating point type t  result will be floating point. e.g. 3.0/2 -> 1.5 When different sizes of the same type are used the result will be the type of the longest operand. These rules are applied to each step as an expression is evaluated e.g. 3/4*4.0 will give the result zero. For the above you could change the order of evaluation e.g. 4.0*3/4 When in doubt use brackets ! e.g. (4.0*3)/4

Department of Electronic & Electrical Engineering Examples 4* /8*34 x*y*func(4,5) 5%2 (78+84)*(34-45) int func(int a,int b) { return a*b-7; }

Department of Electronic & Electrical Engineering C and boolean variables (TRUE and FALSE) unlike some other languages C does not have a Boolean type. C uses int any non zero value is interpreted as TRUE. zero is interpreted as FALSE.

Department of Electronic & Electrical Engineering Relational (comparator) operators e.g. 5.0 > 4.0  1 for TRUE 6.0==7.0  0 for FALSE int result

Department of Electronic & Electrical Engineering Logical Operators (combining boolean values)

Department of Electronic & Electrical Engineering Logical examples. int x=5,y=6,z=7; /* we can declare and initialize at the same time ! */ int result; /* C does not have a boolean type. We use int */ result = x>y; /* result should be 0 which means FALSE */ result = x<y; /* result should be 1 which means TRUE */ result = (x > y) && ( ! z < x) ; /* Hmmm this is a bit tricky */ result = (x > y) && ( ! (z < x)) ; /* I think this is what I meant to write */

Department of Electronic & Electrical Engineering Operator Precedence Higher precedence at the top of the table

Department of Electronic & Electrical Engineering Associativity Consider If associativity is left to right this is (8-4)+2  6 right to left 8-(4+2)  2

Department of Electronic & Electrical Engineering Bitwise operators int a=5; /* 101 */ int b=3; /* 011 */ int c; c= a & b ; /* bitwise AND c = 001 */ c= a | b; /* bitwise OR c = 111 */ c= a^b; /* XOR c = 110 */ c= ~a; /* complement c = */ c= a << 2 /* left shift by 2 c = */ c= a >> 2 /* right shift 2 c = 001 */

Department of Electronic & Electrical Engineering char x=2; /* */ char y=3; /* */ /* logically AND x and y both are TRUE so z is 1 */ char z=x && y; /* bitwise AND x and y result is = 2 ! */ char w=x & y; printf(" %d %d \n",z,w); Example bit wise operations