Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Size: px
Start display at page:

Download "Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments"

Transcription

1 Variables, Data Types, and More Introduction In this lesson will introduce and study C annotation and comments C variables Identifiers C data types First thoughts on good coding style Declarations vs. definitions Initializations vs. assignment Implicit and explicit conversion between types Annotation and Comments A First Look Important part of any program Annotation Comments Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments Section may comprise Function Portion of an algorithm Code fragment Etc. Possible things to include Name of function, module, or file Author s name Revision level and / or date Description of the code Description of major functions Date, description of any modifications, and initials of author Annotation also includes some of same language considerations We will discuss under comments We start with several major annotation blocks - 1 of 22 -

2 s Annotation at Start of Files // // Module name: // filename.c // // Description: // Function counts the number of lines in a C source file. // Does not count blank lines or comment lines. // Reports the results to stdout. Also saves the results // in a data file for further use. // // Author: // Your Name // Rev March 2013 // Annotation at the Start of Functions and Methods // // SetRadius // // Purpose: // Sets the radius of the circle. // // Parameters: // [in] double radius - the radius value to set // // Returns: // bool - true if the function succeeds // // Method: // Copies the input value to a local variable // // Author: // Your Name // Rev March 2013 // of 22 -

3 Annotation at the Start of Test Output // // Module Name: // CircleArea // // Description: // Test that area of a circle computed and returned correctly // for several sizes of radius over the permissible range of // values. // // Author: // Your Name // Rev March 2013 // Comment Comment is piece of text accompanying piece of code To provide explanation of Description of line or block of code Purpose of line of code Elaboration on what code is doing Notes to future modifiers of code In general any information designer feels necessary C language supports two styles of comment Single line Code // comment text or All text following // on same line Interpreted as comment Code /* comment text */ All text following opening /* and closing */ Interpreted as comment - 3 of 22 -

4 Multi line Code /* comment text more comment text yet comment text still more comment text enough comment text */ or Code /* */ comment text more comment text yet comment text still more comment text enough comment text All text following opening /* and closing */ Interpreted as comment Caution Be careful with the C style /*. */ comment delimiters What s wrong with the following code fragment Code0 /* comment text more comment text yet comment text still more comment text enough comment text Code1 Code2 /* */ comment text more comment text yet comment text still more comment text enough comment text When comments are used in program Should always be aligned - 4 of 22 -

5 : Poor style int x = 9; // initialize x int y = 91; // initialize z int z = 910; // initialize z Good Style int x = 9; int y = 91; int z = 910; // initialize x // initialize z // initialize z Comments should always be suggestive of their intended purpose. Poor style int x = 9; // set x to 9 Comment provides no information useful information Good style int timer1init = 0x12; // initialize timer1 to 12 ms Better style int timer1init = 12MS; // initialize timer1 to 12 ms Here 12MS was declared earlier either as Const integer Symbolic constant Using earlier #define directive All variable and function names should be suggestive of their intended purpose int x = 9; int timer1init = 0x12; // poor style // good style void f1 ( char* p1 ) void parsecommand ( char* recbufptr ) // poor style // good style - 5 of 22 -

6 Identifiers and Variables Let s now revisit and explore C variables in greater depth You may be familiar with many of these from other studies Have noted programs encapsulate Data and functions In various ways Examine these in greater depth All such called identifiers Identifier is the name of the variable or function Identifiers include Variables These are data that can be changed Constants also called symbolic constants These are data than cannot be changed Sometimes call constants variables as well Strange a variable that is a constant Function Names These operate on data Legal Identifiers Identifier Comprised of string of characters Include Letters Upper case Lower case Digits 0..9 Underscore _ No other characters First Character must be Letter Underscore Typically don t use Reserved for library functions By tradition May have conflict otherwise - 6 of 22 -

7 Cannot be C keyword Case Significant x, X are different names Length ANSI Standard First 31 characters valid or unique - local variables External names Function call First 6 characters guaranteed to be unique myfunction0() and myfunction1() May compile to same function name myfunc() Depends upon compiler Some implementations support more Must be aware of this Affects portability Most systems Accept 31 for external vars as well Selecting Identifiers Rules of thumb When selecting identifiers choose name That is meaningful That is relevant to working context Self explanatory Easy to understand and work with Should never use Names of body parts Names of bodily functions Names of reproductive processes Your opinion of The design Programming language Members of your team - 7 of 22 -

8 Spelling the identifier name Variety of different spelling methods No particular method is right or wrong Which ever you choose be consistent All lowercase int motorspeed = 100; Can be difficult to read Uppercase each major word int motorspeed = 100; Sometimes called camel notation Separate each major word with a dash int motor-speed = 100; Separate each major word with an underscore int motor_speed = 100; In addition to suggestions above Should take several other things into account Unless you are one of the very rare designers The ones whose design works perfectly the first time Will need to have several passes through debug stage Process of debugging typically means frequent searches through the code Consider following declarations int i = 9; int z = 8; Are these two equivalent They seem to be Which one might be worse why? Now consider searching your code for either variable Which will be the easier to find In a typical program How many instances of - 8 of 22 -

9 i might you find z might you find Next consider int motorspeed = 100; int speedofmotorwheninputvoltageatmaximumvalue = 100; Which identifier is easier to work with and why Do you want to try to search for that second name? Data Types Variables Types and Sizes Basic Types - 4 char Single byte - 8 bits Holds one character int integer - typically 32 bits on today s machines Reflects natural size of ints on host machine Depending on the machine maybe 8, 16, 64 float Single precision floating point number - typically 32 bits double Double precision floating point number - typically 64 bits Modifiers Following table identifies Base type Qualifiers that modify that type Early motivation Save memory Memory was limited and expensive Again machine dependent Assuming a 16 bit int here - 9 of 22 -

10 Base Type Modifier Typical Length Expressive Power Declaration int short short int lowerlimit long x 10 9 long int upperlimit (signed by default) signed - 16 bits signed int lowerlimit (msb is sign) - 32 bits 2.2 x 10 9 signed long upperlimit unsigned - 16 bits unsigned int arraysize char short not applicable long not applicable (msb is sign) signed to +127 signed char error = -1 unsigned to 255 unsigned char success = 1 float short not applicable long not applicable (signed by default) signed 32 6 significant digits float pi = 3.14 unsigned not applicable double short not applicable long 128 signed machine dependent 20 significant digits long double pi = (signed by default) signed significant digits double pi = unsigned not applicable Representation - float / double Different from ints 3 parts - typical representation sign bit 0 exponent bits 1-8 fraction bits 9-31 or 9-63 Expressive power ( to ) x x 10 ± 37 Fraction max = Exponent - 10 of 22 -

11 Usually encoded Desire to store as positive number Excess subtracted when retrieved Two files with all ANSI compliant compilers contain <limits.h> Specifications for integral types <float.h> Specifications for floating point types These files include Basic variable sizes Machine and compiler dependent properties Declarations and Definitions All variables Must be declared before use Some variables Must be defined before use Declaration Introduces the variable name to the program Definition Allocates memory to store the variable Declaration specifies Type One or more variables of that type int lower, upper, step; char c; Can also write int lower; int upper; int step; - 11 of 22 -

12 Initialization and Assignment Initialization Variable may be initialized at time it is defined Given an initial value Defined is correct word Must be memory space to hold the initializing value int lower = 3, upper, step; char c = b ; Initialization done only once Start of execution Const Qualifier Some variables must be initialized when they are defined Qualifier const Specifies variables value not to be changed Can be applied to definition of any variable Const qualified variable Must be initialized at time of definition const int lower = 3; int upper = 10; // upper can be changed here int checklimit (const int upper) { Function cannot change the argument upper } const int lower = 3; lower = lower + 1; // an initialization // an attempted assignment Compile error - cannot modify const object - 12 of 22 -

13 const int lower; // variable not initialized has unknown value, not 0 lower = 3; // an attempted assignment Compile error - cannot modify const object Constants Constants are identifiers whose values are not expected to change Typically written upper case Distinguish from variables Distinguish from class names in C++ Typical upper case first letter PI with the value #define PI Now use PI in program Preprocessor will substitute the number Note: An error to try to change a constant Place #define statements before main Constant Types Type Write int #define INT_CONSTANT 1234 long unsigned #define LONG_CONSTANT 1234l or 1234L #define US_CONSTANT 1234u, 1234ul, or 1234uL float #define FLOAT_CONSTANT 1.2f or 1.2F long double #define LDOUBLE_CONSTANT 1.2l or 1.2L decimal point distinguished from long int char #define CHAR_CONSTANT 1 or a value is numeric value of character in the machine s character set string #define STRING_CONSTANT Sept Oct Nov - 13 of 22 -

14 Assignment Variable can be Initialized one time Assigned to multiple times int x = 5; // x is initialized to 5 x = 7; // x is assigned the value 7 Lvalues and Rvalues Terms lvalue and rvalue Qualify which variables can be Initialized Assigned to rvalue Can only appear on the right hand side of an assignment statement lvalue Can appear on the right hand or left hand side of an assignment statement // x is an lvalue and it appears on the left hand side of the assignment int x = 5; // y is an lvalue and it appears on the left hand side of the assignment int y = 8; // x is an lvalue and it appears on the left hand side of the assignment // y is an lvalue but it appears on the right hand side of the assignment x = y; // 3 is an rvalue // it cannot appear on the left hand side of the assignment 3 = x of 22 -

15 Magic Numbers Variable definitions and initializations of form int motorspeed = 100; // set motorspeed to 100 said to be using magic numbers In this case value 100 Definition above has several style problems Uses magic number as initializer Comment provides no useful information not already contained in definition Preferred method for definition Change magic number to symbolic constant Provide useful information in comment #define MAXSPEED 100 int motorspeed = MAXSPEED; // set motor to full speed In this case does not provide significantly more information Than can be seen by self-documenting definition Type Conversions We have learned that variable s type Provides various kinds of useful and necessary information about the variable Its size number of bits Signed or unsigned Simple or complex Such information helps to ensure that we are using Proper kind of data for our statement or application int rather than pointer to int char rather than int float rather than char In our statements, expressions, algorithms Normally want to ensure All variables of same type - 15 of 22 -

16 Are times when need to be able to convert From one type to another Why.. Consider simple problem of adding $5.25 and $2 First number is of type float and second is of type int These are different but we typically don t think about it Should be able to easily add the two amounts however and we do without thinking To perform addition Mentally convert $2 to $2.00 Convert second number from int type to float type Perform addition Such an operation called implicit conversion C language supports two kinds of conversion Implicit As we ve just seen Done secretly by compiler Explicit We as programmer tell compiler What kind of conversion we want Let s examine each of these See how they work We ll begin with discussion Implicit type conversions In contrast to explicit conversions Implicit Type Conversions An implicit type conversion occurs When the compiler needs to convert data From one intrinsic type To data of another intrinsic type Implicit conversions that preserve values Commonly called promotions Before an arithmetic operation is performed - 16 of 22 -

17 Integral promotion is used to create ints Out of shorter integral types and Floating-point promotion to make doubles Out of floats Promotions will not promote To long or to long double The integral promotions are char, signed char, unsigned char, short int, or unsigned short int Convert to int bool converts to int false becomes 0 and true becomes 1 Let s look at several conversions Integral Promotions Let s look at the following simple program implicitconvert0.c #include <stdio.h> int main(void) { // declare some variables short myshort = 2; int myint = 3; myint = myshort; printf( %d \n, myint); // prints 2 myint = 3; myshort = myint; printf( %d \n, myint); // prints 3 myint = 32767; myshort = myint; printf( %d \n, myshort); // prints of 22 -

18 myint = ; myshort = myint; printf( %d \n, myshort); // prints return 0; } The assignment of myshort to myint is safe myint = myshort Because a short Typically (16 bits) on 32 bit machine Can fit inside an int 32 bits on 32 bit machine The first assignment of myint to myshort is somewhat safe myshort = myint For this particular assignment Why Will probably generate a warning when you compile In this example Value of myint is 3 Can be expressed in fewer than 16 bits In fact could have int as large as Will still work Since 2 16 is What will display if we assign value to the int Then assign the int to the short What will display if we assign value to the int Then assign the int to the short What are the dangers of this implicit conversion Floating Point Conversions Floating-point type conversions Cannot be guaranteed to preserve value So they are not called promotions Converting a float to a double is safe But converting a double to a float is only safe - 18 of 22 -

19 If the value in the double will fit in the float If it won't the results are not defined As with all implicit conversions Your compiler is not obligated to warn you Floating point conversion to integer works fine As long as the integer variable is large enough To hold the resulting value Note: Fractional portion of the floating point number is truncated Only if the truncated value is Too large to be represented by the integer type Is the conversion undefined Side Effect Cast Implicit conversions Often referred to as side effect cast Meaning Conversion takes place As result or part of performing some other operation Such casts can be very dangerous Why Explicit Type Conversions We ve seen that type conversions Can occur automatically or implicitly casting is a way to explicitly convert data From one type to another Note: Value and type of original variable remain unchanged Casting Creates temp variable of specified type Assigns value of original variable to temp - 19 of 22 -

20 Suppose encounter the following code char data = 'M'; printf( %c \n, data); printf() Senses the char data type Switches to character mode M appears on the screen If we really wanted the number corresponding to data We can rewrite these lines as char data = 'M'; int displaydata; displaydata = data; // is this a good idea printf( %d \n, displaydata); Now printf() Senses the int data type Switches to integer mode We see 77 on the screen We see we had to add an int variable Solely for the purpose of displaying data That variable will continue to exist until the functions complete Seems like a waste What is needed Way to tell the compiler Convert the char type to an int type on the fly Here is the explicit cast char data = 'M'; printf( %d \n, (int)data); The cast (int)data in the code above Tells the compiler - 20 of 22 -

21 Make a copy of the data and Make that copy an int When data is to be sent to printf() Copy is sent instead printf() now sees an int and displays 77 The reasons for casting are varied Here are two 1. When we add floating-point numbers Decimal portions of the numbers are part of the sum If we Cast the floating-point numbers to integers Then add them Decimal portions have all been chopped off Result is the sum of the integer portions only Cast below rounds a floating point sum to the nearest integer value float cost = (int) ( ); 2. We might cast one type to another To produce particular results As in earlier example When we cast the character 'M' to integer To produce the integer value 77 on the output Good Style In general casting can lead to unanticipated side effects and should be avoided If you must cause a cast make it explicit - 21 of 22 -

22 Summary In this lesson we studied Several fundamental aspects of C programs Basic mechanics of constructing C programs Some of the basic terminology First thoughts on C annotation, comments, and good coding style C variables and Identifiers C data types Implicit and Explicit conversion between types Concept of type promotions - 22 of 22 -

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. 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 a sequence of letters and digits, but must start with a letter.

More information

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5 C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Programming in C++ 6. Floating point data types

Programming in C++ 6. Floating point data types Programming in C++ 6. Floating point data types! Introduction! Type double! Type float! Changing types! Type promotion & conversion! Casts! Initialization! Assignment operators! Summary 1 Introduction

More information

Chapter 7. Basic Types

Chapter 7. Basic Types Chapter 7 Basic Types Dr. D. J. Jackson Lecture 7-1 Basic Types C s basic (built-in) types: Integer types, including long integers, short integers, and unsigned integers Floating types (float, double,

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

INTRODUCTION 1 AND REVIEW

INTRODUCTION 1 AND REVIEW INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

1 class Lecture2 { 2 3 Elementray Programming / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 41 / 68 Example Given the radius

More information

Basic Types and Formatted I/O

Basic Types and Formatted I/O Basic Types and Formatted I/O C Variables Names (1) Variable Names Names may contain letters, digits and underscores The first character must be a letter or an underscore. the underscore can be used but

More information

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1 CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

Variables and Constants

Variables and Constants HOUR 3 Variables and Constants Programs need a way to store the data they use. Variables and constants offer various ways to work with numbers and other values. In this hour you learn: How to declare and

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1 Chapter 2 Input, Processing, and Output Fall 2016, CSUS Designing a Program Chapter 2.1 1 Algorithms They are the logic on how to do something how to compute the value of Pi how to delete a file how to

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

CMPE-013/L. Introduction to C Programming

CMPE-013/L. Introduction to C Programming CMPE-013/L Introduction to C Programming Bryant Wenborg Mairs Spring 2014 What we will cover in 13/L Embedded C on a microcontroller Specific issues with microcontrollers Peripheral usage Reading documentation

More information

Approximately a Final Exam CPSC 206

Approximately a Final Exam CPSC 206 Approximately a Final Exam CPSC 206 Sometime in History based on Kelley & Pohl Last name, First Name Last 4 digits of ID Write your section number: All parts of this exam are required unless plainly and

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

THE FUNDAMENTAL DATA TYPES

THE FUNDAMENTAL DATA TYPES THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include

More information

CS Programming In C

CS Programming In C CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Types, Operators and Expressions

Types, Operators and Expressions Types, Operators and Expressions EECS 2031 18 September 2017 1 Variable Names (2.1) l Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a keyword.

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Variables and Literals

Variables and Literals C++ By 4 EXAMPLE Variables and Literals Garbage in, garbage out! To understand data processing with C++, you must understand how C++ creates, stores, and manipulates data. This chapter teaches you how

More information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab. University of Technology Laser & Optoelectronics Engineering Department C++ Lab. Second week Variables Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable.

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual: Java Programming, Eighth Edition 2-1 Chapter 2 Using Data A Guide to this Instructor s Manual: We have designed this Instructor s Manual to supplement and enhance your teaching experience through classroom

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

Computers Programming Course 5. Iulian Năstac

Computers Programming Course 5. Iulian Năstac Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction

More information

Data Types. Data Types. Integer Types. Signed Integers

Data Types. Data Types. Integer Types. Signed Integers Data Types Data Types Dr. TGI Fernando 1 2 The fundamental building blocks of any programming language. What is a data type? A data type is a set of values and a set of operations define on these values.

More information

CS110: PROGRAMMING LANGUAGE I

CS110: PROGRAMMING LANGUAGE I CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 4: Java Basics (II) A java Program 1-2 Class in file.java class keyword braces {, } delimit a class body main Method // indicates a comment.

More information

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ... Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

File Handling in C. EECS 2031 Fall October 27, 2014

File Handling in C. EECS 2031 Fall October 27, 2014 File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018 Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory

More information

3. Simple Types, Variables, and Constants

3. Simple Types, Variables, and Constants 3. Simple Types, Variables, and Constants This section of the lectures will look at simple containers in which you can storing single values in the programming language C++. You might find it interesting

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Types, Operators and Expressions

Types, Operators and Expressions Types, Operators and Expressions CSE 2031 Fall 2011 9/11/2011 5:24 PM 1 Variable Names (2.1) Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a

More information

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents: Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language Dr. Amal Khalifa Lecture Contents: Introduction to C++ Origins Object-Oriented Programming, Terms Libraries and Namespaces

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Data Type Fall 2014 Jinkyu Jeong

Data Type Fall 2014 Jinkyu Jeong Data Type Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Syntax Rules Recap. keywords break double if sizeof void case else int static... Identifiers not#me scanf 123th printf _id so_am_i gedd007 Constants

More information

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu Physics 2660: Fundamentals of Scientific Computing Lecture 3 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Announcements Weekly readings will be assigned and available through the class wiki home

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

COP 3275: Chapter 09. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

COP 3275: Chapter 09. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA COP 3275: Chapter 09 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA Introduction A function is a series of statements that have been grouped together and given a name. Each function

More information

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park Variables in C CMSC 104, Fall 2012 John Y. Park 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What Are Variables in C? Variables in C have the

More information

BLM2031 Structured Programming. Zeyneb KURT

BLM2031 Structured Programming. Zeyneb KURT BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help

More information

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 1 C++ Basics Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

More information

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter-8 DATA TYPES. Introduction. Variable: Chapter-8 DATA TYPES Introduction To understand any programming languages we need to first understand the elementary concepts which form the building block of that program. The basic building blocks include

More information

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Lecture 4 CSE July 1992

Lecture 4 CSE July 1992 Lecture 4 CSE 110 6 July 1992 1 More Operators C has many operators. Some of them, like +, are binary, which means that they require two operands, as in 4 + 5. Others are unary, which means they require

More information

Basic data types. Building blocks of computation

Basic data types. Building blocks of computation Basic data types Building blocks of computation Goals By the end of this lesson you will be able to: Understand the commonly used basic data types of C++ including Characters Integers Floating-point values

More information

Week 3 Lecture 2. Types Constants and Variables

Week 3 Lecture 2. Types Constants and Variables Lecture 2 Types Constants and Variables Types Computers store bits: strings of 0s and 1s Types define how bits are interpreted They can be integers (whole numbers): 1, 2, 3 They can be characters 'a',

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

This watermark does not appear in the registered version - Slide 1

This watermark does not appear in the registered version -   Slide 1 Slide 1 Chapter 1 C++ Basics Slide 2 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output Program Style

More information

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;

More information

Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O Overview of C Basic Data Types Constants Variables Identifiers Keywords Basic I/O NOTE: There are six classes of tokens: identifiers, keywords, constants, string literals, operators, and other separators.

More information

ANSI C Programming Simple Programs

ANSI C Programming Simple Programs ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

More information

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming Fundamentals of Programming Lecture 3: Introduction to C Programming Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department Outline A Simple C

More information

Simple Data Types in C. Alan L. Cox

Simple Data Types in C. Alan L. Cox Simple Data Types in C Alan L. Cox alc@rice.edu Objectives Be able to explain to others what a data type is Be able to use basic data types in C programs Be able to see the inaccuracies and limitations

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

5.Coding for 64-Bit Programs

5.Coding for 64-Bit Programs Chapter 5 5.Coding for 64-Bit Programs This chapter provides information about ways to write/update your code so that you can take advantage of the Silicon Graphics implementation of the IRIX 64-bit operating

More information

Basics of Programming

Basics of Programming Unit 2 Basics of Programming Problem Analysis When we are going to develop any solution to the problem, we must fully understand the nature of the problem and what we want the program to do. Without the

More information

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays Special PRG Lecture No. 2 Professor David Brailsford (dfb@cs.nott.ac.uk) School of Computer Science University of Nottingham Special PRG Lecture: Arrays Page 1 The need for arrays Suppose we want to write

More information

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

VARIABLES AND TYPES CITS1001

VARIABLES AND TYPES CITS1001 VARIABLES AND TYPES CITS1001 Scope of this lecture Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Primitive types Every piece of data

More information