Week 3 - Monday.  What did we talk about last time?  Math library  Preprocessor directives  Lab 2.

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Chapter 2 Data Types, Declarations, and Displays
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.
Pointers Applications
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
 Integers and Characters  Character Strings  Input and Output.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Characters and Strings
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C is a high level language (HLL)
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Week 3 - Monday CS222.
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Fundamental of Programming (C)
Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Introduction to C Programming
Lectures on Numerical Methods
Chapter 2: Introduction to C++.
Week 2 - Friday CS222.
Presentation transcript:

Week 3 - Monday

 What did we talk about last time?  Math library  Preprocessor directives  Lab 2

One thing I've noticed with C/C++ programmers, particularly (which is, again, the pool from which most C# programmers will be drawn), is that many of them are convinced that they can handle dangerous techniques which experience shows they can't handle. They say things such as, "I like doing my own memory management, because it gives me more control," but their code continually suffers from memory leaks and other pointer-related problems that show quite clearly that they are not to be trusted with these things that give them "more control." This, in my view, is just one more reason why "unsafe" features should not be built into mass-market languages like C#. Craig Dickson One thing I've noticed with C/C++ programmers, particularly (which is, again, the pool from which most C# programmers will be drawn), is that many of them are convinced that they can handle dangerous techniques which experience shows they can't handle. They say things such as, "I like doing my own memory management, because it gives me more control," but their code continually suffers from memory leaks and other pointer-related problems that show quite clearly that they are not to be trusted with these things that give them "more control." This, in my view, is just one more reason why "unsafe" features should not be built into mass-market languages like C#. Craig Dickson

 We said that the size of int is compiler dependent, right?  How do you know what it is?  sizeof is a built-in operator that will tell you the size of an data type or variable in bytes #include int main() { printf("%d", sizeof(char)); int a = 10; printf("%d", sizeof(a)); double array[100]; printf("%d", sizeof(array)); return 0; } #include int main() { printf("%d", sizeof(char)); int a = 10; printf("%d", sizeof(a)); double array[100]; printf("%d", sizeof(array)); return 0; }

 In Java, constants are specified with the final modifier  In C, you can use the keyword const  Note that const is only a suggestion  The compiler will give you an error if you try to assign things to const values, but there are ways you can even get around that  Arrays have to have constant size in C  Since you can dodge const, it isn't strong enough to be used for array size  That's why #define is more prevalent const double PI = ;

 The header limits.h includes a number of constants useful in C programming  There are some for basic data types  float.h has similar data for floating-point types, but it isn't as useful for us Constant Typical ValueConstantTypical Value SCHAR_MIN–128INT_MIN– SCHAR_MAX127INT_MAX UCHAR_MAX255UINT_MAX CHAR_MIN–128LONG_MIN– CHAR_MAX127LONG_MAX SHRT_MIN–32768ULONG_MAX SHRT_MAX32767CHAR_BIT8 USHRT_MAX65535

 limits.h has other system limits  C and Linux have their roots in old school systems programming  Everything is limited, but the limits are well- defined and accessible  You may need to know:  How many files a program can have open at the same time  How big of an argument list you can send to a program  The maximum length of a pathname  Many other things…

 For system limits, a minimum requirement for the maximum value is defined in limits.h  If you want the true maximum value, you can retrieve it at runtime by calling sysconf() or pathconf() (defined in unistd.h ) with the appropriate constant name #include int main() { long value = sysconf(_SC_LOGIN_NAME_MAX); printf("Maximum login name size: %d\n", value); return 0; } #include int main() { long value = sysconf(_SC_LOGIN_NAME_MAX); printf("Maximum login name size: %d\n", value); return 0; }

limits.h Constant Minimum Value sysconf( ) NameDescription ARG_MAX4096_SC_ARG_MAX Maximum bytes for arguments ( argv ) plus environment ( environ ) that can be supplied to an exec() none _SC_CLK_TCK Unit of measurement for times() LOGIN_NAME_M AX 9 _SC_LOGIN_NAME_ MAX Maximum size of a login name, including terminating null byte OPEN_MAX20_SC_OPEN_MAX Maximum number of file descriptors that a process can have open at one time, and one greater than maximum usable none 1_SC_PAGESIZE Size of a virtual memory page STREAM_MAX8_SC_STREAM_MAX Maximum number of stdio streams that can be open at one time NAME_MAX14_PC_NAME_MAX Maximum number of bytes in a filename, excluding terminating null byte PATH_MAX256_PC_PATH_MAX Maximum number of bytes in a pathname, including terminating null byte

 C uses one byte for a char value  This means that we can represent the 128 ASCII characters without a problem  In many situations, you can use the full 256 extended ASCII sequence  In other cases, the (negative) characters will cause problems  Let's see them!  Beware the ASCII table!  Use it and die!

If you ever put one of these codes in your program, you deserve a zero.

#include int main() { char c; for (c = 1; c != 0; c++) printf("%c\n", c); return 0; } #include int main() { char c; for (c = 1; c != 0; c++) printf("%c\n", c); return 0; }

 There is nothing type safe in C  What happens when you call printf() with the wrong specifiers?  Either the wrong types or the wrong number of arguments printf("%d\n", 13.7); printf("%x\n", 13.7); printf("%c\n", 13.7); printf("%d\n"); printf("%d\n", 13.7); printf("%x\n", 13.7); printf("%c\n", 13.7); printf("%d\n");

 What's the difference between %x and %X ?  How do you specify the minimum width of an output number?  Why would you want to do that?  How do you specify a set number of places after the decimal point for floating-point values?  What does the following format string say?  "%6d 0x%04X\n"

 Now that we have a deep understanding of how the data is stored in the computer, there are operators we can use to manipulate those representations  These are:  & Bitwise AND  | Bitwise OR  ~ Bitwise NOT  ^ Bitwise XOR  << Left shift  >> Right shift

 The bitwise AND operator ( & ) takes:  Integer representations a and b  It produces an integer representation c  Its bits are the logical AND of the corresponding bits in a and b  Example using 8-bit char values: a & b c char a = 46; char b = 77; char c = a & b; //12 char a = 46; char b = 77; char c = a & b; //12

 The bitwise OR operator ( | ) takes:  Integer representations a and b  It produces an integer representation c  Its bits are the logical OR of the corresponding bits in a and b  Example using 8-bit char values: char a = 46; char b = 77; char c = a | b; //111 char a = 46; char b = 77; char c = a | b; // a | b c

 The bitwise NOT operator ( ~ ) takes:  An integer representation a  It produces an integer representation b  Its bits are the logical NOT of the corresponding bits in a  Example using 8-bit char values: ~ a b char a = 46; char b = ~a; // -47 char a = 46; char b = ~a; // -47

 The bitwise XOR operator ( ^ ) takes:  Integer representations a and b  It produces an integer representation c  Its bits are the logical XOR of the corresponding bits in a and b  Example using 8-bit char values: char a = 46; char b = 77; char c = a ^ b; //99 char a = 46; char b = 77; char c = a ^ b; // a ^ b c

 It is possible to use bitwise XOR to swap two integer values without using a temporary variable  Behold!  Why does it work?  Be careful: If x and y have the same location in memory, it doesn't work  It is faster in some cases, in some implementations, but should not generally be used x = x ^ y; y = x ^ y; x = x ^ y; y = x ^ y; x = x ^ y;

 The << operator shifts the representation of a number to the left by the specified number of bits  The >> operator shifts the representation of the number to the right by the specified number of bits  Ignoring underflow and overflow, left shifting is like multiplying by powers of two and right shifting is like dividing by powers of two char a = 46; char b = a << 2; //-72 char a = 46; char b = a << 2; //-72 char a = 46; char b = a >> 3; //5 char a = 46; char b = a >> 3; //5

 Things smaller than int will be promoted to int  What are the following?  4 & 113  15 | 39  31 << 4  108 >> 5  ~80

 The computer uses bitwise operations for many things  These operations are available for our use and are very fast  Shifting is faster than multiplying or dividing by powers of 2  You can keep a bitmask to keep track of 32 different conditions  That's quite a lot of functionality for 4 bytes!

 Operators in every programming language have precedence  Some of them are evaluated before others  Just like order of operations in math  * and / have higher precedence than + and –  = has a very lowest precedence  I don't expect you to memorize them all, but  Know where to look them up  Don't write confusing code

Type OperatorsAssociativity Primary Expression () []. -> expr ++ expr -- Left to right Unary * & + - ! ~ ++ expr -- expr ( typecast ) sizeof Right to left Binary * / % Left to right + - >> << = == != & ^ | && || Ternary ?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left Comma, Left to right

 What happens here?  x++ >> 5 == 4 % 12 & 3  It's also worth noting that precedence doesn't tell the whole story  What about multiple assignments in a single line of code?  C doesn't give you guarantees about what happens when  The following could have different results on different compilers: printf("%d %d", x++, (x + 5)); a[x] = x++; x = x++; printf("%d %d", x++, (x + 5)); a[x] = x++; x = x++;

 Control flow  Selection  Loops

 Read K&R chapter 3  Keep working on Project 1  Due Friday by midnight