1 CSE1301 Computer Programming Lecture 6: Components of a C Program (Part 2)

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
1 CSE1301 Computer Programming Lecture 6 C Primitives 3.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
Introduction to Computers and Programming Lecture 7:
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Introduction to C Programming CE Lecture 2 Basics of C programming.
CS150 Introduction to Computer Science 1
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
String Escape Sequences
The C Programming Lecture 24.
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.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
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.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Chapter 2. Variable and Data type
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
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Topics Type Variables Keywords and Identifiers Assignments Constant Variables.
USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Chapter # 2 Part 2 Programs And data
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
User Interaction and Variables
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
LESSON 3 IO, Variables and Operators
DCP2073 Asas Pengaturcaraan C Lecture 6: Components of a C Program (Part 3) Lecture 6: C Primitives 6.
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

1 CSE1301 Computer Programming Lecture 6: Components of a C Program (Part 2)

2 Topics Type Variables Keywords and Identifiers Assignments Constant Variables

3 Type The kind of a value is its “type” Not all values are of the same kind –For example: can’t logically evaluate 7 + “cat” Value Variable

4 Type Built-in types: char, int, float Type modifiers: long, short, const User-defined types (arrays and records) What about “strings”? –Strings are arrays of char (discussed later)

5 Character Representation Characters are stored as a small integer Each character has a unique integer equivalent specified by its position in the ASCII table (pronounced “ask-key”) –American Standard Code for Information Interchange –see Appendix D of D&D

6

7 Character Representation The ASCII values range from 0 to 127 –value 0: special character ’\0’ (a.k.a. NUL character) –value 127: special character –other special characters: ’\n’ ’\t’ ’\’’ ’\\’ etc. –various “extended” sets from 128 to 255

8 Variable Is a logical name for a container (an actual piece of computer memory for values) Have a type associated with them, which tells the computer how to interpret the bits Must be declared before use: [modifiers] ; [modifiers] = ;

9 Variable Declaration: Examples int myID; myID

10 Variable Declaration: Examples int myID; char my_initial = ’J’; Single “forward quotes” or apostrophe ( ’ ) rather than “back quotes” ( ‘ )

11 Variable Declaration: Examples int myID; char my_initial = ’J’; my_initial

12 Variable Declaration: Examples int myID; char my_initial = ’J’; char my_initial = 74 ; my_initial

13 Variable Declaration: Examples float commission = 0.05; short int myHeight = 165; /* cm */ long int mySalary = ; long float chanceOfADate = 3e-500; double chance_of_a_2nd_date = 1.5e-500;

14 float commission = 0.05; short int myHeight = 165; /* cm */ long int mySalary = ; long float chanceOfADate = 3e-500; double chance_of_a_2nd_date = 1.5e-500; Variable Declaration: Examples “Keywords”

15 Keyword...has a special meaning in C...is “case-sensitive”...cannot be used as variable names Examples: int, char, long, main, float, double, const, while, for, if, else, return, break, case, switch, default, typedef, struct, etc. (see D&D 2/e Appendix A or D&D 3/e page 545, Figure 15.4)

16 Variable Declaration: Examples float commission = 0.05; short int myHeight = 165; /* cm */ long int mySalary = ; long float chanceOfADate = 3e-500; double chance_of_a_2nd_date = 1.5e-500; “Identifiers”

17 Identifier...is a series of characters consisting of letters, digits and underscores ( _ )...cannot begin with a digit...must not be a keyword...is “case-sensitive” Examples: sUmoFA, x1, y2, _my_ID_, Main (careful!)

18 Assignment Puts a specified value into a specified variable Assignment operator: = = ; not to be confused with ==

19 Assignment: Examples float x = 2.5 ; char ch ; int number ; ch = ’\n’ ; number = ; /* current value of number is 9. */ number = number * 2; /* current value of number is now 18. */

20 Assignment Value must have a type assignable to the variable Value may be automatically converted to fit the new container Example: –various.c

21 #include /* Do various assignment statements */ int main() { int integer; char character; float floatingPoint; integer = 33; character = 33; floatingPoint = 33; integer = 'A'; character = 'A'; floatingPoint = 'A'; integer = 33.33; character = 33.33; floatingPoint = 33.33; integer = floatingPoint; floatingPoint = integer; return 0; } various.c

22 Constant Variables...are variables that don’t vary...may not be assigned to....must be initialized const [modifiers] = ;

23 Constant Variables: Examples const int myID = 192; myID = 666; /* Error! */ const int passMark = 80; short char pAsSgRaDe = ’P’; const float e = ; /* oops */ const double golden_ratio = ;

24 Converts an angle from degrees to radians output “Enter angle in degrees” input angleInDegrees angleInRadians =  / 180 * angleInDegrees output angleInRadians #include /* Converts an angle in degrees to radians. */ const float PI = ; int main() { float angleInDegrees; float angleInRadians; printf("Enter angle in degrees: "); scanf("%f", &angleInDegrees); angleInRadians = PI / 180 * angleInDegrees; printf("%f\n", angleInRadians); return 0; } Example: Constants

25 Example: Constants “Global” (constant) variable “Local” variables more on this later... #include /* Converts an angle in degrees to radians. */ const float PI = ; int main() { float angleInDegrees; float angleInRadians; printf("Enter angle in degrees: "); scanf("%f", &angleInDegrees); angleInRadians = PI / 180 * angleInDegrees; printf("%f\n", angleInRadians); return 0; }

26 Readings This Lecture: D&D 2/e: Sections 2.1 to 2.10 D&D 3/e: Sections 2.1 to 2.5 Next Lecture: Booleans D&D 2/e: –Sections 2.12 to 2.13, 3.2 and 4.13 to 4.15 D&D 3/e: Sections 2.6, 3.5, 4.10 and 4.11