SYSC 2006 Lecture 2: Variables, Data Types, and Expressions PDF

Summary

This document is a lecture on imperative programming, specifically variables, data types, and expressions in C programming. The lecture notes cover different data types in Python and C and how they are used in programming to solve problems within imperative programming.

Full Transcript

Copyright © 2024 1 Department of Systems and Computer Engineering, Carleton University SYSC 2006: Foundations of Imperative Programming Lecture 2: Variables, Data Types, and Expressions ...

Copyright © 2024 1 Department of Systems and Computer Engineering, Carleton University SYSC 2006: Foundations of Imperative Programming Lecture 2: Variables, Data Types, and Expressions 2 Previous Lecture Summary ❑Define programming, program ❑Differentiate between the different programming paradigms and name programming languages under those paradigms ❑Understand the fundamental concepts of imperative programming languages ❑Become familiar with basic C syntax and variables ❑Write basic programs in C 3 Objectives ❑Become familiar with basic C syntax and variables ❑Explain and properly use basic C data types ❑Distinguish between static and dynamic typing in programming languages ❑Use C operators to write expressions to implement C programs ❑Understand how operators’ precedence affects the execution of expressions 4 Types and Variables in C 5 Data Types Particular kind of data item. Defined by: The values it can take The programming language used The operations that can be performed on it. Basic data types Types provided by a programming language as a "building block" 6 Basic Data Types in Python Numeric types int: unlimited precision signed integers float: floating-point, usually double precision complex: real and imaginary parts are floating point numbers bool: boolean, a subtype of integers str: immutable character strings 7 Basic Data Types in C char: character (usually an 8-bit byte) int: signed integer Later in the float: single-precision floating-point course! double: double-precision floating-point _Bool: Boolean (added in C99) _Complex: complex numbers (added in C99) C does not provide a character string type Strings are implemented as arrays of characters 8 Data Type Qualifiers in C: signed Data type int: signed integers Typical size 32 bits → −231 to 231 − 1 signed explicitly declares that the integer values are signed These type declarations are equivalent int (signed values are implied) signed int signed (int is implied) 9 Data Type Qualifiers in C: unsigned unsigned declares that integer values are positive or 0 Same size as int. Typical size 32 bits → 0 to 232 −1 These type declarations are equivalent unsigned int unsigned (int is implied) 10 Data Type Qualifiers in C: short, long short and long specify the minimum size of the integer type short int at least 16 bits long int is at least 16 bits long (but 32 bits is common) long int is at least 32 bits long long long int is at least 64 bits long size short int , >=, k Sometimes brackets improve readability _Bool var = (x < i * 2 + 3) && (12.0 / j * 7 > k); 39 Expressions and Type Conversions Operands of a binary operator with different types → converted to a common type before the operation is performed In general, a “narrower” operand is converted to a “wider” operand, so that no information is lost Example: int i; float f; When f + i is evaluated → integer obtained from i is converted to a float before the addition is performed Result: float 40 Expressions and Type Conversions Simplified rules for binary operators, with no unsigned operands: If either operand is a long double, convert the other to long double Otherwise, either operand is a double, convert the other to double Otherwise, if either operand is float, convert the other to float Otherwise, convert char and short to int Then, if either operand is long, convert the other to long Then, perform the operation Conversion rules are more complicated when one or both operands are unsigned Best to consult a C reference manual for these cases 41 Assignment and Type Conversions For a type T T var; var = expr; Expression expr is evaluated Value is converted to a value of type T Then assigned to var 42 Functions and Type Conversions We will see functions later in more detail. For now, think of them as Python functions! Type conversions occur when function parameters are assigned the values of the corresponding function arguments. Example: double pow(double x, double y); pow(2.5, 4) parameter y is assigned 4.0, not 4 43 Recap Learning Outcomes ❑Become familiar with basic C syntax and variables ❑Explain and properly use basic C data types ❑Distinguish between static and dynamic typing in programming languages ❑Use C operators to write expressions to implement C programs ❑Understand how operators’ precedence affects the execution of expressions Be able to write programs in C using these concepts. Copyright © 2024 44 Department of Systems and Computer Engineering, Carleton University SYSC 2006: Foundations of Imperative Programming Lecture 2: Variables, Data Types, and Expressions

Use Quizgecko on...
Browser
Browser