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

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

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

Fundamentals of Programming

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

Fundamental of Programming (C)

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

Presented By : Gaurav Juneja

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

A Fast Review of C Essentials Part I

C: How to Program. Week /Mar/05

Data Types and Variables in C language

Programming. C++ Basics

Programming and Data Structures

VARIABLES AND CONSTANTS

Data types, variables, constants

C Language, Token, Keywords, Constant, variable

Syntax and Variables

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

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

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

ANSI C Programming Simple Programs

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

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

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

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

Chapter 2 - Introduction to C Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

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

Creating a C++ Program

BLM2031 Structured Programming. Zeyneb KURT

ET156 Introduction to C Programming

6.096 Introduction to C++ January (IAP) 2009

Introduction to Computing Lecture 01: Introduction to C

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

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

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

INTRODUCTION 1 AND REVIEW

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

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

Tokens, Expressions and Control Structures

The C++ Language. Arizona State University 1

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

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

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

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

LECTURE 02 INTRODUCTION TO C++

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

ET156 Introduction to C Programming

Informática Ingeniería en Electrónica y Automática Industrial

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

Data Type Fall 2014 Jinkyu Jeong

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.

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

Differentiate Between Keywords and Identifiers

PROGRAMMAZIONE I A.A. 2018/2019

CMPE-013/L. Introduction to C Programming

Introduction to Programming

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

Chapter 1 & 2 Introduction to C Language

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Introduction to C# Applications

Basic Types, Variables, Literals, Constants

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

from Appendix B: Some C Essentials

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

2. Numbers In, Numbers Out

BTE2313. Chapter 2: Introduction to C++ Programming

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Creating, Compiling and Executing

Lecture 02 C FUNDAMENTALS

Chapter 2: Introduction to C++

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Variables. Data Types.

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

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

3. Java - Language Constructs I

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

Slides adopted from T. Ferguson Spring 2016

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter 11 Introduction to Programming in C

THE FUNDAMENTAL DATA TYPES

UEE1302 (1102) F10: Introduction to Computers and Programming

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Chapter 11 Introduction to Programming in C

Computer Components. Software{ User Programs. Operating System. Hardware

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Review of the C Programming Language for Principles of Operating Systems


Introduction to C++ with content from

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

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

CMSC 246 Systems Programming

Transcription:

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

Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2

What Are Variables in C? Variables in C have a similar meaning as variables in algebra. That is, they represent some unknown, or variable, value. x = a + b z + 2 = 3(y - 5) Variables in algebra are typically represented by a single alphabetic character. 3

Legal Identifiers in C Variables in C are also called identifiers. Variables in C may be given names containing multiple characters. Legal variable names in C May only consist of letters, digits, and underscores May be as long as you like, but only the first 31 characters are significant May not begin with a number May not be a C reserved word (keyword) 4

Reserved Words (Keywords) in C 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 5

Naming Conventions C programmers generally agree on the following conventions for naming variables. Use meaningful identifiers (names) Separate words within identifiers with underscores or mixed upper and lower case. Examples: surfacearea surface_area surface_area Be consistent! 6

Case Sensitivity C is case sensitive It matters whether an identifier, such as a variable name, is uppercase or lowercase. Example: area Area AREA ArEa are all seen as different variables by the compiler. 7

Legal Identifiers vs. Naming Conventions Legal identifiers refer to the restrictions C places on naming identifiers, i.e. variable names cannot begin with a number. Naming conventions refer to the standards typically followed by programmers, i.e. separating words with mixed case or underscores. 8

Which Are Legal Identifiers? AREA lucky*** Last-Chance x_yt3 num$ area_under_the_curve 3D num45 #values pi %done 9

Which follow the Naming Conventions? Area Last_Chance x_yt3 finaltotal person1 values pi numchildren area_under_the_curve 10

Declaring Variables Before using a variable, you must give the compiler some information about the variable; i.e., you must declare it. The declaration statement includes the data type of the variable. Examples of variable declarations: int meatballs; float area; 11

Declaring Variables (con t) 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 is associated with a unique address Visualization of the declaration int meatballs; meatballs type name FE07 garbage address 12

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

Using Variables: Initialization Variables may be be given initial values, or initialized, when declared. Examples: int length = 7 ; length 7 float diameter = 5.9 ; diameter 5.9 char initial = A ; initial A 14

Using Variables: Initialization (con t) Do not hide the initialization Put initialized variables on a separate line A comment is always a good idea Example: int height; /* rectangle height */ int width = 6; /* rectangle width */ int area; /* rectangle area */ C will let you do the following: int height, width=6, area; but it s harder to read! 15

Using Variables: Assignment Variables may have values assigned to them through the use of an assignment statement. Such a statement uses the assignment operator = This operator does not denote equality. It assigns the value of the righthand side of the statement (the expression) to the variable on the lefthand side. Examples: diameter = 5.9 ; area = length * width ; Note that only single variables may appear on the lefthand side of the assignment operator. 16

Example: Declarations and Assignments 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 17

Example: Declarations and Assignments (cont d) 8. printf ( Its depth at sea: \n ) ; 9. printf ( %d fathoms \n, fathoms) ; 10. printf ( %d feet \n, feet) ; 11. printf ( %d inches \n, inches) ; 12. return 0 ; 13. } 18

Enhancing Our Example What if the depth were really 5.75 fathoms? Our program, as it is, couldn t handle it. Floating point numbers can contain decimal portions. We can also ask the user to enter the number of fathoms, rather than hard-coding it in. 19

Enhanced Program 1. #include <stdio.h> 2. int main ( ) 3. { 4. float inches, feet, fathoms; 5. printf("enter the depth in fathoms: "); 6. scanf("%f", &fathoms); 7. feet = 6 * fathoms; 8. inches = 12 * feet; 9. printf ("Its depth at sea:\n"); 10. printf (" %f fathoms\n", fathoms); 11. printf (" %f feet\n", feet); 12. printf (" %f inches\n", inches); 13. return 0; 14. } 20

Final Clean Program 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. 10. /* Get the depth in fathoms from the user */ printf("enter the depth in fathoms: "); 11. scanf("%f", &fathoms); 21

Final Clean Program (con t) 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. } 22

Good Programming Practices 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. 23

Good Programming Practices (con t) 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! 24