CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

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

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

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

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

Fundamentals of Programming

C: How to Program. Week /Mar/05

A Fast Review of C Essentials Part I

Chapter 2 - Introduction to C Programming

Fundamental of Programming (C)

Presented By : Gaurav Juneja

BLM2031 Structured Programming. Zeyneb KURT

ANSI C Programming Simple Programs

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2: Overview of C. Problem Solving & Program Design in C

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Syntax and Variables

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C Language, Token, Keywords, Constant, variable

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

Data Types and Variables in C language

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Data types, variables, constants

Programming in C++ 4. The lexical basis of C++

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

VARIABLES AND CONSTANTS

Chapter 1 & 2 Introduction to C Language

Programming and Data Structures

Procedures, Parameters, Values and Variables. Steven R. Bagley

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Programming. C++ Basics

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

6.096 Introduction to C++ January (IAP) 2009

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

INTRODUCTION 1 AND REVIEW

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Introduction to Computing Lecture 01: Introduction to C

Creating a C++ Program

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

LECTURE 02 INTRODUCTION TO C++

DEPARTMENT OF MATHS, MJ COLLEGE

Basic Elements of C. Staff Incharge: S.Sasirekha

The C++ Language. Arizona State University 1

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

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

ET156 Introduction to C Programming

Basic Types, Variables, Literals, Constants

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Differentiate Between Keywords and Identifiers

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

ET156 Introduction to C Programming

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

CS102: Variables and Expressions

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Introduction to C# Applications

Fundamentals of Programming CS-110. Lecture 2

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CMPE-013/L. Introduction to C Programming

Introduction to Programming EC-105. Lecture 2

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

PROGRAMMAZIONE I A.A. 2018/2019

BASIC ELEMENTS OF A COMPUTER PROGRAM

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

THE FUNDAMENTAL DATA TYPES

2. Numbers In, Numbers Out

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

Lecture 02 C FUNDAMENTALS

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

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

Introduction to the C Programming Language

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

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

Programming for Engineers Introduction to C

Chapter 2: Basic Elements of C++

Tokens, Expressions and Control Structures

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

UEE1302 (1102) F10: Introduction to Computers and Programming

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Fundamentals of Programming Session 4

from Appendix B: Some C Essentials

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

CS242 COMPUTER PROGRAMMING

Data Type Fall 2014 Jinkyu Jeong

Transcription:

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso 1

Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2

a + b Variables are notthe same thing as variables in algebra. A variable is the namethe programmer assigns to a location in memory to store (remember) a value The valueof the variable is the contents of that memory location. 3

a + b When used in a C statement, the values that that exist in that location will be retrieved and loaded into the CPU Any operations that appear in that statement will be done using those values 4

a + b RAM CPU 1-5

We call the name for a variable in C an identifier It identifies a location in memory Naming rules May only consist of letters, digits, and underscores May not begin with a number Do not begin identifiers with underscores either May be as long as you like But it only looks at the first 31 characters May not be a C reserved word (keyword) 6

auto case const default double enum float goto break char continue do else extern for if int register short sizeof struct typedef unsigned volatile long return signed static switch union void while 7

C is case sensitive The case of eachletter matters! Example: area Area AREA ArEa are all seen as different identifiers by the compiler. 8

Legalidentifiersrefer to the restrictions C places on naming identifiers Rules you haveto follow Naming conventionsrefer to the standards you must follow for this course Rules you should follow 9

conventions for naming variables. Begin variable names with lowercase letters Use meaningful identifiers (names) Should describe what that variable is used for Separate words within identifiers with underscores or mixed upper and lower case. surfacearea surface_area surface_area Be consistent! 10

AREA lucky*** Last-Chance x_yt3 num$ area_under_the_curve 3D num45 #values pi %done 11

Area Last_Chance x_yt3 finaltotal area_under_the_curve person1 values pi numchildren 12

Before using a variable, you must declareit. gives the compiler some information about the variable and how much memory to set aside for it The declaration statementincludes the data type of the variable. Needs to know what kindof data it will hold. Examples of variable declarations: int meatballs ; float area ; 13

When we declare a variable Space is set aside in memory to hold a value of the specified data type That space is associated with the variable name That space has a unique address Visualization of the declaration int meatballs ; type name meatballs FE07 garbage address 14

C has three basic predefined data types: Integers (whole numbers) int, long int, short int, unsigned int Floating point (numbers with a decimal point) float, double Characters char At this point, you need only be concerned with the data types that are bolded. 15

int Integer number that between -32767 and 32767 float, double Real number with an integral part and a fractional part Can be written in C-style scientific notation The letter e or E means times 10 to the power 11,000 1.1e4 0.00123 1.23e-3 char A single character (letter, number, special character) Value must be wrapped by single quotes ( )

Variables may be begiven initial values, or initialized, when declared. Examples: length intlength = 7 ; 7 float diameter = 5.9 ; char initial = A ; diameter initial 5.9 A 17

Do not hide the initialization put initialized variables on a separate line a comment is always a good idea Example: int height ; /* rectangle height */ intwidth = 6 ; /* rectangle width */ intarea ; /* rectangle area */ NOT intheight, width = 6, area ; 18

x = a + b The equal signin C is notthe same thing as equal sign in algebra = is the assignment operator It computes the value of whatever is to the right of the = Stores that value in the location named on the left hand side of the = There may only be onevariable on the left side of the = 19

x = a + b a + b x =

a = a + b a + b a = 1-21

Data Output getting data from our application (in memory) out to the user or another application printfwill be used to print to the command line Data Input getting data from outside our application into it scanfwill be used to read from the command line

mad libs 1. Define some text with some blanks Each blank specifies what kind of word to use 2. Come up with words to fill the blanks Give me 2 nouns 1 syllable each 2 verbs that rhyme-2 syllables each

There once was a from Id Who stuck its in a crib It started to And then it And that was the last thing it did 24

Print text to the command line Example: intnum_children= 2; printf( I have %d children, num_children); The %d is called a placeholder It indicates that it is expecting an integer It will be replaced in the string with the integer value that was passed to printf

Supported placeholders for printf %c %d %f char int float Variable types must be compatible with placeholder types Can pass multiple placeholders to printf printf( Their ages are %d and %f \n, 4, 1.5);

Reads user input from the command line Parameters to scanf are: A string containing placeholders One or more pointers to variables Example char firstinit; printf( Enter first initial> ); scanf( %c, &firstinit); printf( You entered: %c\n, firstinit);

You can prompt for more than one input at a time Example char firstinit; char middleinit; printf( Enter 2 initials> ); scanf( %c %c,&firstinit,&middleinit); When typed in, the values can be separated by space, newline, or nothing

1-29

char c1, c2, c3; scanf( %c %c %c, &c1, &c2, &c3);

chars One and only one text character Enclosed in single quotes a strings One or more characters Enclosed in double quotes I am a string 31

1. #include <stdio.h> 2. int main( ) 3. { 4. int inches, feet, fathoms ; 5. fathoms = 7 ; 6. feet = 6 * fathoms ; 7. inches = 12 * feet ; inches garbage feet garbage fathoms garbage fathoms 7 feet 42 inches 504 32

8. printf( Its depth at sea: \n ); 9. printf( %d fathoms \n, fathoms) ; 10. printf( %dfeet\n, feet) ; 11. printf( %d inches \n, inches) ; 12. return 0 ; 13. } 33

What if the depth were really 5.75 fathoms? Our program, as it is, couldn t handle it. Unlike integers, floating point numbers can contain decimal portions. So, let s use floating point, rather than integer. Let s also ask the user to enter the number of fathoms, rather than hard-coding it in. 34

1. #include <stdio.h> 2. int main ( ) { 3. float inches, feet, fathoms ; 4. printf ( Enter the depth in fathoms : ) ; 5. scanf ( %f, &fathoms) ; 6. feet = 6 * fathoms ; 7. inches = 12 * feet ; 8. printf ( Its depth at sea: \n ) ; 9. printf ( %f fathoms \n, fathoms) ; 10. printf ( %f feet \n, feet) ; 11. printf ( %f inches \n, inches) ; 12. return 0 ; 13. } NOTE: This program does not adhere to the CMSC104 coding standards 35

1. #include <stdio.h> 2. 3. int main( ) 4. { 5. float inches ; /* number of inches deep */ 6. float feet ; /* number of feet deep */ 7. float fathoms ; /* number of fathoms deep */ 8. 9. /* Get the depth in fathoms from the user */ 10. printf ( Enter the depth in fathoms : ) ; 11. scanf ( %f, &fathoms) ; 36

12. /* Convert the depth to inches */ 13. feet = 6 * fathoms ; 14. inches = 12 * feet ; 15. 16. /* Display the results */ 17. printf ( Its depth at sea: \n ) ; 18. printf ( %f fathoms \n, fathoms) ; 19. printf ( %f feet \n, feet); 20. printf ( %f inches \n, inches); 21. 22. return 0 ; 23. } 37

Place a comment before each logical chunk of code describing what it does. Do not place a comment on the same line as code with the exception of variable declarations Use spaces around all arithmetic and assignment operators. Use blank lines to enhance readability. 38

Place a blank line between the last variable declaration and the first executable statement of the program. Indent the body of the program 3 to 4 spaces -- be consistent! 39