1 CSC103: Introduction to Computer and Programming Lecture No 6.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Computer Programming w/ Eng. Applications
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to C Programming
Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:1 INTRODUCTION TO ‘C’ LANGUAGE Chapter 2.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Five Tips to Success. Work hard Try more exercises and more practice.
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?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
What is C? C is a programming language. It was developed in 1972 USA. It was designed and written by a man named dennis ritchie. C is the base for all.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
C BASICS QUIZ (DATA TYPES & OPERATORS). C language has been developed by (1) Ken Thompson (2) Dennis Ritchie (3) Peter Norton (4) Martin Richards.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 02 (Part II) Introduction to C++ Programming.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
INTRODUCTION TO C LANGUAGE
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Revision Lecture
Chapter 2 - Introduction to C Programming
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.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
CS111 Computer Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Lecture3.
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 6

2 Previous lecture Escape sequences C compiler Directives Common errors

3 Today’s lecture outline C character set Basic constants and variables Output function – printf Input function – scanf C instructions Integer to float conversion Hierarchy of operator Control instructions

4 Learning C/C++

5 C Character Set

6 Constants An entity that doesn’t change

7 Integer Constants Must have at least one digit It must not have a decimal point It can be either positive or negative If there is no sign an integer constant is assumed to be positive No commas or blanks are allowed within an integer constant Examples: 135, -67, 3401, -5670

8 Real Constants Also called Floating Point constants A real constant must have at least one digit It must have a decimal point It could be either positive or negative Default sign is positive No commas or blanks are allowed within a real constant Examples: , 426.0, ,

9 Character Constants A character constant is a – single alphabet – a single digit – or a single special symbol Enclosed within single inverted commas Both the inverted commas should point to the left The maximum length can be 1 character Examples: ’A’, ‘I’, ‘5’, ‘=‘

10 Variables An entity that may vary during program execution Names given to locations in memory

11 Variables.. Series of characters (letters, digits, underscores) Must begin with a letter or underscore Case sensitive Meaningful naming scheme

12 Variables.. No commas or blanks are allowed within a variable name. No Special symbol are used in name. Examples: Interger1, Sum, _FirstNum Invalid variable names – #sum, 12x, first name

13 Keywords Meaning already explained to compiler Cannot be used as variable name

14 Writing C/C++ Programs Instructions as statements Order of statements Each statement must end with a ; Blanks for readability or clarity Case-Sensitive

15 Gross salary C Program Go to program

16 Variable declaration Any variable used in the program must be declared first before using it.

17 Output function printf ( " ", ) ; can contain, – %f for printing real values – %d for printing integer values – %c for printing character values printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d ) ;

18 Input To make gross salary calculation program general, the program ask the user to input value of hours and payrate during execution scanf function is used to input value from user during program execution

19 Gross salary C Program Go to program

20 Input function syntax Ampersand (&) before the variables in the scanf( ) function is a must. & is an ‘Address of’ operator It gives the location number used by the variable in memory

21 C Instructions Type Declaration Instruction – To declare the type of variables used in a C program. Arithmetic Instruction – To perform arithmetic operations between constants and variables Control Instruction – To control the sequence of execution of various statements in a C program.

22 Type Declaration Instruction This instruction is used to declare the type of variables being used in the program. The type declaration statement is written at the beginning of main( ) function. int hours, payrate, grosspay; float salary; char name;

23 Variations of type declaration instruction Initializing at declare time – int x =10, y = 20; – float a = 1.25, b = 1.99* ; Order of variable decleration – int i = 10, j = 25 ; is same as – int j = 25, j = 10 ; However, – float a = 1.5, b = a ; is alright, but – float b = a + 3.1, a = 1.5 ;

24 Cont. The following statements would work – int a, b, c, d ; – a = b = c = 10 ; However, the following statement would not work – int a = b = c = d = 10 ;

25 Arithmetic Instruction C arithmetic instruction consists of – variable name on the left hand side of = – and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /.

26 Example int ad ; float kot, deta, alpha, beta, gamma ; ad = 3200 ; kot = ; deta = alpha * beta / gamma * 2 / 5 ; The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’

27 Types of arithmetic statement Integer mode arithmetic statement – This is an arithmetic statement in which all operands are either integer variables or integer constants. Example: int i, king, issac, noteit ; i = i + 1 ; king = issac * noteit ;

28 Cont. Real mode arithmetic statement – This is an arithmetic statement in which all operands are either real constants or real variables. Example. float x, y, si, prin, anoy, roi ; x = y / 4.5 * ; si = prin * anoy * roi / ;

29 Cont.. Mixed mode arithmetic statement – This is an arithmetic statement in which some of the operands are integers and some of the operands are real. Example. float si, prin, anoy, roi, avg ; int a, b, c, num ; si = prin * anoy * roi / ; avg = ( a + b + c + num ) / 4 ;

30 Arithmetic instructions C allows only one variable on left-hand side Modular operator – This operator returns the remainder on dividing one integer with another – Expression 10 / 2 yields 5 whereas, 10 % 2 yields 0 – Expression 10 / 3 yields 3 whereas 10 % 3 yields 1 An arithmetic instruction is often used for storing character constants in character variables. char a, b, ; a = 'F' ; b = 'G' ;

31 Cont. Arithmetic operations can be performed on ints, floats and chars. char x, y ; int z ; x = 'a' ; y = 'b' ; z = x + y ;

32 Cont.. No operator is assumed to be present. It must be written explicitly. a = c.d.b(xy) b = c * d * b * ( x * y )

33 Integer and float conversions An arithmetic operation between an integer and integer always yields an integer result An operation between a real and real always yields a real result An operation between an integer and real always yields a real result

34 Type Conversion in Assignments k is int and a is float

35 Hierarchy of Operations

36 Example i = 2 * 3 / / / 8 i = 6 / / / 8 operation: * i = / / 8 operation: / i = / 8 operation: / i = operation: / i = operation: + i = operation: + i = 8 + 0operation : - i = 8 operation: +

37 Association of Operators When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators It can be of two types – Left to right – Right to left

38 Example Consider the expression a = 3 / 2 * 5 ; Left to right: a = 3 / 2 * 5 a = 1 * 5 a = 5 Right to left : a = 3 / 2 * 5 a = 3 / 10 a = 0

39 Control Instructions in C Enable us to specify the order in which the various instructions in a program are to be executed by the computer. Sequence Control Instruction Selection or Decision Control Instruction Repetition or Loop Control Instruction Case Control Instruction

40 Cont. Sequence Control Instruction – The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program.

41 Cont.. Selection or Decision Control Instruction – Decision instructions allow the computer to take a decision as to which instruction is to be executed next. Repetition or Loop Control Instruction – The Loop control instruction helps computer to execute a group of statements repeatedly.

42 Cont… Case Control Instruction – same as decision control instruction.

43 Which are invalid variables BASICSALARY _basic basic-hra #MEAN group. 422 population in 2006 over time

44 Point out the errors int = * 150 ; name = ‘Ajay’ ; varchar = ‘3’ ; 3.14 * r * r * h = vol_of_cyl ; k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; m_inst = rate of interest * amount in rs ; count = count + 1 ;

45 Summary The three primary constants and variable types in C are integer, float and character A variable name can be of maximum 31 characters Do not use a keyword as a variable name An expression may contain any sequence of constants, variables and operators Operators having equal precedence are evaluated using associativity Input/output in C can be achieved using scanf( ) and printf( ) functions.