Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 2 Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Input, Output, and Processing
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Using Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Chapter 2 Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Introduction to C Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
Chapter 2 Variables.
Lexical Elements & Operators
Introduction to C Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to C Programming
Presentation transcript:

Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld

Introduction to Computers and Programming - Class 22 For Those Who Missed it…  Course home page is at  Syllabus is accessible from the home page  There will be seven homeworks, two midterms and a final  There will be OPTIONAL homeworks given more frequently  Office hours are on Wednesday, 8:30-9:30 A.M., room 419 CIWW, and by appointment

Introduction to Computers and Programming - Class 23 Style vs. Syntax  Syntax – elements that are needed Grammar  Style – elements that improve human comprehension Comments, indenting, and other elements

Introduction to Computers and Programming - Class 24 A Sample Program /* our first program in C */ #include /* start of the main program */ int main() { printf( “ Hello World!\n ” ); return 0; } /* end program */

Introduction to Computers and Programming - Class 25 Escape Characters  The backslash (\) is called an escape character  Indicates that printf is supposed to do something unusual  When encountering a backslash, printf looks to the next character and combines it with the backslash to form an escape sequence

Introduction to Computers and Programming - Class 26 Other escape sequences \t horizontal tab \a alert- will make the computer beep \\ prints a backslash character in a printf statement \” p rints a double quote character in a printf statement

Introduction to Computers and Programming - Class 27 Printing Several Lines  One printf statement can print several lines by using newline characters. e.g. printf( “ Welcome\nto\nC!\n ” );  But that’s a stylistic horror! A better way: printf( “ Welcome \n ” “ to \n ” “ C!\n ” );

Introduction to Computers and Programming - Class 28 Printing Long Lines  Can use two or more printf statements (note the first has no newline) printf( “ The quick brown fox jumped ” ); printf( “ over the lazy dog.\n ” );  This will print one line like so: The quick brown fox jumped over the lazy dog.

Introduction to Computers and Programming - Class 29 Why does this work like this?  Because C ignores most white space characters in your editor (spaces, tabs, carriage-returns (“enters”), etc.)  C is looking for the \n character to tell it when to go to the next line

Introduction to Computers and Programming - Class 210 Declaration  Creating an identifier and associating it with a type and (behind the scenes) a location in memory. A couple of examples: int integer1; int integer1, integer2, sum;  integer1, integer2 and sum are all variables

Introduction to Computers and Programming - Class 211 Variables  Variables hold data values which can change  They must be declared with a data type and a name, immediately after a left brace, before they can be used  A variable name in C is any valid identifier

Introduction to Computers and Programming - Class 212 Data Types  C (as well as many other programming languages) are very sensitive to data type.  The int family short, long  The float family Double  Characters (strings)

Introduction to Computers and Programming - Class 213 Identifier  An identifier is a series of characters consisting of letters, digits and underscores “_” that does not begin with a digit or include several special characters such as math signs, $, and others  Can be any length but only the first 31 characters are required to be recognized by ANSI C compilers  Keep identifiers 31 characters or less for portability and fewer problems

Introduction to Computers and Programming - Class 214 C Is Case Sensitive  Upper case and lower case letters are different in C  E.g., lower case a1 and capital A1 are different identifiers

Introduction to Computers and Programming - Class 215 Good Programming Practices  Choose meaningful variable names to help make a program self-documenting (fewer comments will be needed).  First letter of an identifier used as a variable name should be a lower case letter.  Multiple-word variable names can help make a program more readable.  Use mixed-cases to help make the word stand out. E.G. totalCommissions.

Introduction to Computers and Programming - Class 216 Printf Example #3 #include int main() { int num1 = 3, num2 = 2; printf("%d plus %d equals %d\n", num1, num2, num1+num2); return 0; }

Introduction to Computers and Programming - Class 217 Conversion Specifiers  %d – integer  %f – float  %c – character  %s - string

Introduction to Computers and Programming - Class 218 Arithmetic in C  The C arithmetic operators are + for addition - for subtraction * for multiplication / for division, and % for modulus

Introduction to Computers and Programming - Class 219 Binary Operators  Operators that take two operands e.g. “+” is a binary operator and “a + b” has two operands (a and b)  Note integer division will yield an integer result e.g. 5 / 2 = 2 (not 2 1/2 ) and 17 / 5 = 3  modulus is the remainder after an integer division e.g. 5 % 2 is 1 and 17 % 5 is 2

Introduction to Computers and Programming - Class 220 Big (Very Common) Error  Divide by Zero e.g. x = y / 0 Normally undefined by computer systems and generally results in a fatal error Usually shows up at run time

Introduction to Computers and Programming - Class 221 Rules of Operator Precedence  C evaluates arithmetic expressions in a precise sequence determined by the rules of operative precedence  Expressions within parentheses are evaluated first (highest level of precedence) For nested or embedded parentheses, the expression in the innermost pair is evaluated first

Introduction to Computers and Programming - Class 222 Rules of Operator Precedence cont’d  Multiplication, division and modulus operations are evaluated next If more than one evaluated from left to right  Addition and subtraction operations are evaluated last If more than one evaluated from left to right

Introduction to Computers and Programming - Class 223 Parentheses  Are your friends  Are your really good friends  Because with them you can ensure expressions are evaluated as you expect  Can avoid mistakes with operator precedence (one less thing to think about) e.g. y = m * x + b ; y = (m * x) + b; e.g. y = a * b * b + c * b – d; y = ((((a * b) * b) + (c * b)) – d);