CMSC 1041 Variables in C Declaring, Naming, Using Variables.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
Structure of a C program
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Chapter # 2 Part 2 Programs And data
User Interaction and Variables
Basics (Variables, Assignments, I/O)
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
LESSON 3 IO, Variables and Operators
Variables and Arithmetic Operators in JavaScript
CMSC 104, Section 4 Richard Chang
Introduction to C Programming
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
IDENTIFIERS CSC 111.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Basics (Variables, Assignments, I/O)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
Variables and Arithmetic Operators in JavaScript
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Chapter 2: Java Fundamentals
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Variables in C Declaring , Naming, and Using Variables.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Chap 2. Identifiers, Keywords, and Types
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

CMSC 1041 Variables in C Declaring, Naming, Using Variables

CMSC 1042 What is a “Variable” in C? l A variable is a storage location in memory. l It is not: oa number oa value l “X = X+1” means “Add 1 to the contents of X and then store the result in X” not “X is a number such that X = X + 1.”

CMSC 1043 Using Variables l You must declare variables in C so the compiler can reserve space. l The declaration includes the type of the variable. l Examples of variable declarations: int meatballs ; /* 32 bit fixed */ float area ; /* decimal pt */ long salary;/* 64 bit fixed */ (“fixed” numbers have no decimal point.)

CMSC 1044 Declaring Variables l When we declare a variable: o space in memory is set aside to hold that data type oThat space is associated with the variable name oVisualization of the declaration int meatballs ; meatballs FE07

CMSC 1045 Declaring Variables l Compiler Table: Name Spade Mango Quinn Shannon Oteri Update Address 0x44A3 0x432D 0x432E 0X434F 0x433A 0x4217 Type int float long char int char Location b a 98

CMSC 1046 Legal Variable Names l Variable names in C must be valid identifiers oConsists of letters, digits and underscores. oMay be as long as you like, but only the first 31 characters are significant. oMay NOT begin with a number oMay not be a C keyword l Restriction is to simplify compiler design

CMSC 1047 Naming Conventions l Variable names are for people to read - make them understandable! oPick meaningful names: “taxableIncome” not “I” “numberOfDependents” not “n” or “d” or “nd” “altitudeInMeters” not “altitude” Use single-letter variables (I,j,k,m,n,x,y, etc) only for temporary “throwaway” results y = yearsOfExperience + yearsOfSchool; printf(“Total equivalent years equals: “ %d, y); l Begin variable names with lowercase letters l Separate “words” within identifiers with underscores or mixed upper and lower case. l Example: surfaceArea surface_Area or surface_area l Be consistent !!

CMSC 1048 Naming Conventions (Continued) l Begin variable names with lowercase letters: oheight oseparation l Separate “words” within identifiers with underscores or mixed upper and lower case. opatientHeight ochannelSeparationInDb osurfaceArea, surface_Area, surface_area l Be consistent !! (or have a reason for inconsistency)

CMSC 1049 Naming Conventions (continued) l Use all uppercase for symbolic constants #define PI #define FUN_COURSE CMSC104 l Use quotes to enclose spaces or punctuation #define PGM_ID “CMSC 104 Prj. 1: P.C.Olsen 701” #define IDENT “$$NITF v l Function names follow the same rules as variables

CMSC Naming Conventions (continued) l Function names follow the same rules as variables: o“calculateArea(float radius)” o“degreesKelvin(float degreesRoentgen)” o“predictVelocity(int time, float acceleration) l Compare to ocA(float r) oK(float R) opredV(int t, float a)

CMSC Case Sensitive l C is case-sensitive Lowercase letters and uppercase letters are different: area is different than Area which is different than AREA

CMSC Predefined Types of Variables l Integers oint, long int, short int l Floating point ofloat, double l Characters ochar

CMSC Initializing Variables l Variables MUST be initialized before they are used: owhen they are declared: int x = 7;/* default hat size */ float y = 5.9;/* standard picnic table radius */ char c = ‘A’;/* expected grade */ obefore first use: x = 7; /* default hat size */ y = 5.9; /* standard picnic table radius */ c = ‘A’ /* expected grade */

CMSC Initializing Variables (continued) l Do not “hide” the initialization oput initialized variables on a separate line oalways comment oExamples: int y = 6; /* feet per fathom */ float initialVelocity = 0.0; /* in m/s before start */ oNOT int x, y = 6, z;

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

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

CMSC Declarations and assignments wreck.c #include main ( ) { int inches, feet, fathoms ; fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; } inches feet fathoms 7 feet inches

CMSC wreck.c (cont’d) main ( ) { printf (“Its depth at sea: \n”) ; printf (“ %d fathoms \n”, fathoms) ; printf (“ %d feet \n”, feet); printf (“ %d inches \n”, inches); } %d is a place holder - indicates that the value of the integer variable is to be printed in decimal form (rather than binary or hex) at that location.

CMSC Floating point numbers l Can contain decimal points. l What if the depth were really 5.75 fathoms ?... Our program, as it is, couldn’t handle it. l We can declare floating point variables like this : float fathoms ; float feet ;

CMSC Floating point version of wreck.c (works for any depth shipwreck) #include main ( ) { float fathoms, feet; printf (“Enter the depth in fathoms : ”); scanf (“%f”, &fathoms); feet = 6.0 * fathoms; printf (“She’s %f feet down.\n”, feet); } (“%f” syntax does the same thing for floats as %d does for ints --- prints them out in decimal form.)