Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Variables in C Amir Haider Lecturer.
C++ Basics Variables, Identifiers, Assignments, Input/Output.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
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.
CPS120: Introduction to Computer Science Lecture 8.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
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.
Lecture #5 Introduction to C++
C++ Programming: Basic Elements of C++.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
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.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Introduction to C Programming Language
CMSC 104, Section 4 Richard Chang
Introduction to C Programming
Reserved Words.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
INTRODUCTION TO C.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 2Winter Quarter ENG H192 Course Web Page A web page which contains the course syllabus, updated lecture notes and other useful information may be found at:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 3Winter Quarter C Program Basics C vs. C++ –C is a subset of C++. All of features in C are contained in C++ –C++ adds more libraries with functions for object oriented programming –C++ also adds more keywords and some added features.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 4Winter Quarter Keywords in C and C++ Certain words have a special meaning to the C or C++ compiler. They are called reserved words or keywords. We should not try to use these words as names of variables or function names in a program. The keyword list for C contains 32 words (see text, pg. 545). C++ adds 30 more keywords.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 5Winter Quarter Some Keywords in C and C++ switch template this throw try typedef union unsigned virtual void volatile while new operator private protected public register return short signed sizeof static struct double else enum extern float for friend goto if inline int long asm auto break case catch char class const continue default delete do

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 6Winter Quarter Program Structure in C EACH complete C program is composed of: –Comment statements –Pre-processor directives –Declaration statements –One or more functions –Executable statements

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 7Winter Quarter Program Structure in C EACH complete C program is composed of: –Comment statements –Pre-processor directives –Comment statements –Declaration statements –Comment statements –One or more functions –Comment statements –Executable statements –Comment statements

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 8Winter Quarter Comment Statements Formal Comments: /* Comment ….. */ –Used for detailed description of functions or operations (for our benefit, not compiler’s). –Can take multiple lines in source file. Informal Comments (only in C++, not C): // Comment ….. Ends at the end of line –Used for quick comments like: int temp; // temporary variable for storing // the input value

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 9Winter Quarter Pre-Processor Directives #include -- header files for library functions Example: #include #define -- define constants and macros Examples: #define e #define pi Note Space Note Spaces

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 10Winter Quarter Declarations Declarations tell the compiler what variable names will be used and what type of data each can handle (store). Example declarations: int a, b, c ; float r, p, q ; double x, y, z ; char m, n ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 11Winter Quarter Data Types Integer variables:int a, b ; Integer variables, like a or b, store only whole numbers like 3 or 7, not 3.33 or 7.65, and only up to certain maximum values. Floating point variables:float c, d ; Floating point variables, like c or d, store rational numbers, like , but only a limited number of digits of precision.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 12Winter Quarter Internal Storage Representation Definitions: –Binary digit -- or a "bit", is either a 0 or a 1 –Byte -- usually a collection of 8 bits together –Word -- often a collection of 4 bytes together On the SGI Unix system: –an "int" data type takes up 4 bytes (on some systems, an "int" is only 2 bytes) –a "float" data type takes up 4 bytes –a "double" data type take up 8 bytes –a "char" data type takes up 1 byte

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 13Winter Quarter Programs Have One or More Functions Even the main program is a function. The body of each user-written function is enclosed in braces, { } (or curly brackets) The syntax of a function is: function_name (arg. list) { /* beginning of function */ } /* end of function */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 14Winter Quarter Executable Statements Simple Declaring variables int temp ; char a ; Assigning Values temp = 5 ; Complex, i.e., Calling Functions fubar (x, y) ; Calculations x = (5. / 2 + 6) * 7 ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 15Winter Quarter Arithmetic Operators *multiply+add /divide-subtract %remainder, where: x = 13 % 5 ;/* x will be equal to 3 */ An expression can be used almost anywhere a variable of the same type can be used. Ex. expressions:num + 3, a * d - 5,...

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 16Winter Quarter Mixed Mode Arithmetic When performing arithmetic operations, the "mode" will one of: –Floating point, if both operands are floating point –Integer, if both operands are integer –Mixed, if one operand in integer and the other is floating point -- the result is floating point Integer operations produce integer results (remember how you first learned to to division?)

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 17Winter Quarter Assignment Operators Operator:Example:Meaning: = x = 5 ;x = 5 ; +=x += 5 ;x = x + 5 ; –=x –= 5 ;x = x – 5 ; /=x /= 5 ;x = x / 5 ; *=x *= 5 ;x = x * 5 ; %= x %= 5; x = x % 5 ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 18Winter Quarter Assignment Operators Example of assignment operators: int a = 4, b = 2, c = 36 ; a += b ;/* This adds b to a, a = ? */ c /= a + b ;/* What is value of c now? */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 19Winter Quarter Assignment Operators Example of assignment operators: int a = 4, b = 2, c = 36 ; a += b ;/* This adds b to a, a = ? */ [ Answer: a = a + b, so a = or a = 6 ] c /= a + b ;/* What is value of c now? */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 20Winter Quarter Assignment Operators Example of assignment operators: int a = 4, b = 2, c = 36 ; a += b ;/* This adds b to a, a = ? */ [ Answer: a = a + b, so a = or a = 6 ] c /= a + b ;/* What is value of c now? */ [ Answer: c = c / (a + b), and a = 6 now, so c = 36 / (6 + 2), so c = 36 / 8 or c = 4 ]

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 21Winter Quarter Increment/Decrement Operators Operator: Meaning:When? count++ ;count = count + 1 ;After use ++count ; count = count + 1 ;Before use count-- ;count = count - 1 ;After use --count ; count = count - 1 ;Before use

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 22Winter Quarter Increment/Decrement Operators Examples of increment and decrement operators: int a = 4, b = 2, c; c = ++a + b-- ; /* What are the values of a, b, c now? */ c = b a ; /* What are the values of a, b, c now? */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 23Winter Quarter Increment/Decrement Operators Examples of increment and decrement operators: int a = 4, b = 2, c; c = ++a + b-- ; /* What are the values of a, b, c now? */ (Answers: a = 5, b = 1, c = 7) c = b a ; /* What are the values of a, b, c now? */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 24Winter Quarter Increment/Decrement Operators Examples of increment and decrement operators: int a = 4, b = 2, c; c = ++a + b-- ; /* What are the values of a, b, c now? */ (Answers: a = 5, b = 1, c = 7) c = b a ; /* What are the values of a, b, c now? */ (Answers: a = 6, b = 0, c = -5)

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 25Winter Quarter Relational Operators Operator:Meaning: <Less Than >Greater Than <=Less Than or Equal To >=Greater Than or Equal To ==Exactly Equal To !=Not Equal To

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 26Winter Quarter Relational Operators Used for asking questions like: Is x bigger than 10? In C, the value of 1 stands for true and 0 stands for false. But C will recognize any non zero value as true. NOTE:"==" is NOT same as "="

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 27Winter Quarter Logical Operators ! (not) Ex: a != b is true if a and b are not equal && (and) Ex: 5 4 is true, but 5>6 && 7>4 is not true (i.e., false) || (or) Ex: 5>6 || 7>4 is true 5<6 || 7<4 is also true

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 28Winter Quarter Exponentiation Operations Exponentiation is not written as x**2 or x^2 C does not have an exponentiation operator. You can use the math function pow (a, b) which raises a to the b power. You must put a #include in your source code and must also use the -lm switch in your compile command when on the SGI UNIX system. Ex: >CC -o myprog.out myprog.cpp -lm

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 29Winter Quarter Skeleton Program /* Name: Brutus Buckeye */ /* Seat No. 0, Instr: W. Hayes */ /* Program progname */ #include void main ( ) { statements ; }