C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data types and variables
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Input/Output and Variables Ethan Cerami New York
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Objectives You should be able to describe: Data Types
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
2440: 211 Interactive Web Programming Expressions & Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Variables Symbol representing a place to store information
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
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.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
TMF1414 Introduction to Programming
Revision Lecture
Java Programming: From Problem Analysis to Program Design, 4e
By: Syed Shahrukh Haider
Introduction to C Programming
Operators and Expressions
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.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Java Applications
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

C Building Block Chapter 2

Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference. A quantity whose value may change during execution of the program Variable represent the memory location in the computer memory Data store into memory location Name of variable remain fixed Data may change time to time Also known as object in C++ Consists of alphabets and digits

Rules for Variable Names First Character must be alphabetic character Underscore can be used as First Character Blank Space are not allowed Special Characters i.e. # ! are not allowed Reserved words are not allowed Variable name length depend upon compiler of C++ A variable named declared with one data type cannot be used to declare another data type. Case sensitive

Data Types in C++ Five Basic data types Data TypeDescriptionSyntax No of total Bytes occupied in memory 1. CharacterCharacter datachar1 2. IntegerSigned whole Integerint2 3. FloatFloating point numberfloat4 4. Double Double Precision Floating point number double8 5. VoidValuelessvoid0

Data Types TypeSize in BytesData Storage Range int to short int to long int to unsigned int20 to unsigned long int40 to float43.4 x 10 Ʌ - 38 to 3.4 x 10 Ʌ + 38 long float81.7x 10 Ʌ to 1.7 x 10 Ʌ double81.7x 10 Ʌ to 1.7 x 10 Ʌ long double103.4 x 10 Ʌ to 3.4 x 10 Ʌ char1For string, 1 bytes to bytes

Declaration/Initialization of Variables Declaration Sytax – [Type] [list of variables]; – int a, b, c; Initialization - Assign value at the time of declaration – int a= 110, b= 90, c;

Input/Outputs “printf ”– Output Stream – printf(“Output String”,var1[,var2…]); – printf(“Output String”); “scanf ”– Input Stream – scanf(&var1[,&var2…]); #include void main (void) { float years, days; printf ( “ Please type your age in years. “ ); scanf ( “ %f “, &years ); days = years * 365; printf ( “ You are %.1f days old.”, days ); }

Format Specifiers The format specifier in printf() determines the interpretation of a variable’s type, the width of the field, the number of decimal places printed and the justification.

Field Width Specifiers – printf(“Age is %3d”,40); 123 Ageis40

Escape Sequences Special non-printing characters to control printing on output device Combination of backslash ‘\’ and a code

“ getch/getche ” Function get get from outside ch character e echo ( write ) #include void main() { char ch; printf(“Type any character: ”); ch = getche(); printf(“\nCharacter is :%c ”,ch); }

Operators Address Operator (&) Provide the memory address of variable – int i = 2; printf(“value = %d, address = %d”, i,&i); – value = 2, address= 3536 Arithmetic Operators Order of Precedence – Order in which arithmetic expression evaluate e.g. (4-(3*5))+2 – (3*5) – (4-15) – OperatorMeaning +Addition -Subtraction *Multiplication /Division %For remainder

Compound Assignment / Arithmetic Assignment Add, subtract, multiply or divide a value to or from a variable with out writing the variable on either side of assignment operator (=) – xy += 10 ; equal to xy = xy +10;

Increment & Decrement Operators Increment Operator represent by double plus(++) – x++ ; equal to x = x +1; Decrement Operator represent by double minus(--) – x-- ; equal to x = x -1; Prefix / Postfix – Calculation change – x--, --x, x++,++x sum = a+b+ c-- will not be equal to a+b+ --c

Rational Operators OperatorMeanings <Less than >Greater than <=Less than or equal to >=Greater then or equal to ==Equal to !=Not equal to void main (void) { int age ; age = 15; printf ( “ Is age less than 20 ? %d \n “, age < 20 ); age = 30; printf ( “ Is age less than 20 ? %d \n “, age < 20 ); getch ( ); } Output: Is age less than 20 ? 1 Is age less than 20 ? 0

Precedence between Operators Arithmetic Operators have higher precedence i.e. they are evaluated before the Relational Operators. #include void main (void) { printf( “ Answer is %d “, 1 < ); getch( ); } Output: Answer = 5 (Wrong) Answer = 1 ( Right )

Comments Helpful to understand the source code Invisible for compiler /* Initialize the variables*/ Nested comments /* Print/*Result*/Prompt*/ Multiple Line comments /* Print *Result *Prompt */

Lab Work Write Program that print age in minutes. Hint: [1 year = 365 days 1 days = 24 hours 1 hour = 60 min] Write a program to calculate radius of circle. radius = 2πr π = 3.14