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.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
True or false A variable of type char can hold the value 301. ( F )
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Numeric Types, Expressions, and Output ROBERT REAVES.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
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.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
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.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
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
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.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Numbers in ‘C’ Two general categories: Integers Floats
BASIC ELEMENTS OF A COMPUTER PROGRAM
Revision Lecture
C Short Overview Lembit Jürimägi.
Computing with C# and the .NET Framework
Introduction to C Programming Language
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
Operators and Expressions
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.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
DATA TYPES There are four basic data types associated with variables:
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

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 only an integer constant. ○ A real variable can hold a real constant. & o A character variable can hold a character constant. 1

Rules for Constructing Variable Names:  A variable name may consist of letters, digits, and the underscore ( _ )  The first character of variable name may be an alphabetic character or an underscore ( _ )  The first character of variable name cannot be a digit.  Blank spaces are not allowed in a variable name. 2

 Special characters such as arithmetic operators, #, >, cannot be used in a variable name.  Reserved C words cannot be used as variable names.  The maximum length of a variable name is up to 31 characters.  VAR1 & var1 are two different variables. 3

C keywords:  Keywords are the words whose meaning has already been explained to the C compiler.  The keywords are also called as ‘Reserved words’.  Keywords cannot be used as variable names.  There are 32 keywords known to C. 4

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

The first C program:  For example: write a program to add two numbers. 6

Compilation and execution of C program:  To type your C program you need another program called Editor.  once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler.  Integrated development environment (IDE): consists of an Editor as well as the compiler. E.g. Turbo C++, Microsoft C++ and Borland C++ 7

Receiving Input : ○P○Printf( ); printf( )outputs the values to the screen whereas scanf( ) receives them from the keyboard. ○S○Scanf( ); To make the program general the program Itself should ask the user to supply the values at run time. This can be achieved using a function called scanf( ) This function is a counter-part of the printf( ) function. 8

C instructions  There are basically three types of instructions in C: Type Declaration Instruction Arithmetic Instruction Control Instruction 9

Type Declaration Instruction  This instruction is used to declare the type of variables being used in the program.  Any variable used in the program must be declared before using it in any statement.  The type declaration statement is written at the beginning of main( ) function. Ex: int add ; float ave, per ; char name, code ; 10

 There are several variations of the type declaration instruction. These are discussed below: While declaring the type of variable we can also initialize it as shown below. ○ int i = 10, j = 25 ; ○ float a = 1.5, b = * 1.44 ;  The order in which we define the variables is sometimes important sometimes not. For example; int i = 10, j = 25 ; is same as int j = 25, i = 10 ; 11

However, float a = 1.5, b = a ; is alright, but float b = a + 3.1, a = 1.5 ; is not. This is because here we are trying to use a even before defining it.  The following statements would work int a, b, c, d ; a = b = c =d= 10 ; However, the following statement would not work int a = b = c = d = 10 ; Once again we are trying to use b (to assign to a) before defining it. 12

Arithmetic Instruction  A C arithmetic instruction consists of a 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 /.  For example; int a,b,; Float c; a=2; C=3.5; b=a+5*c 13

 Here +,* are arthmetic operators. = is the assignment operator. 2, 5 are integer constants. 3.5 is real constant. a,b, are integer variables. C is real variable.  The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on left-hand side. 14

 A C arithmetic statement could be of three types. These are as follows: Integer mode arithmetic statement - This is an arithmetic statement in which all operands are either integer variables or integer constants. ○ Ex. Int a,b,c,sum; ○ a=3, b=7, c=5; ○ Sum=a+b+c;  Real mode arithmetic statement - This is an arithmetic statement in which all operands are either real constants or real variables. For example; ○ float a,b,c ; ○ a=3.4, b=2.1; ○ C=a/b; 15

 Mixed mode arithmetic statement - This is an arithmetic statement in which some of the operands are integers and some of the operands are real.  For example; float x,y,z ; int a, b, c, avg; Z=x*y/100.0 ; avg = ( a + b + c ) / 3 ;  It is very important to understand how the execution of an arithmetic statement takes place. Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side. 16

 Though Arithmetic instructions look simple to use one often commits mistakes in writing them. Let us take a closer look at these statements. Note the following points carefully. C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal. In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. note that the modulus operator cannot be applied on float. 17

 An arithmetic instruction is often used for storing character constants in character variables. char a, b, d ; a = 'F' ; b = 'G' ; d = '+' ; When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ and ‘G’ are 70 and 71 (refer the ASCII Table in Appendix E). 18

 Arithmetic operations can be performed on ints, floats and chars. Thus the statements, char x, y ; int z ; x = 'a' ; y = 'b' ; z = x + y ; are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added. 19

Integer and Float Conversions  In order to effectively develop C programs, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C. These are mentioned below. 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. 20

An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.  Following are the few practical examples; OperationResultOperationResult 5/222/50 5.0/ / / / / /

Type Conversion in Assignments  It may so happen that the type of the expression and the type of the variable on the left-hand side of the assignment operator may not be same.  In such a case the value of the expression is promoted or demoted depending on the type of the variable on left-hand side of =.  For example, consider the following assignment statements. int i ; float b ; i = 3.5 ; b = 30 ; 22

 Here in the first assignment statement though the expression’s value is a float (3.5) it cannot be stored in i since it is an int. In such a case the float is demoted to an int and then its value is stored. Hence what gets stored in i is 3.  Exactly opposite happens in the next statement. Here, 30 is promoted to and then stored in b, since b being a float variable cannot hold anything except a float value. 23

Hierarchy of Operations  While executing an arithmetic statement, which has two or more operators, we may have some problems as to how exactly does it get executed. For example, does the expression 2 * x - 3 * y correspond to (2x)- (3y) or to 2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions satisfactorily one has to understand the ‘hierarchy’ of operations. The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. It is also known as order of precedence of the operators. 24

 The hierarchy of commonly used operators is shown below; Note: We must always remember to use pair of parenthesis. If there are more than one set of parenthesis, the operations within the innermost parentheses would be performed first, followed by the operations within the second innermost pair and so on. PriorityOperatorsDescription 1 st * / %Multiplication, division, modular division 2 nd + -Addition, subtraction 3 rd =Assignment 25

Associativity of Operators  Associativity refers to the order in which operators having the same precedence are evaluated. The operators having the same precedence order are evaluated either from right-to-left or from left-to- right. OperatorsAssociativity () ++(postfix) --(postfix)Left-to-right +(unary) -(unary) ++(prefix) --(prefix)Left-to-right * / %Left-to-right + -Left-to-right = += *= /=Right-to-left 26

 For example; in the following expression, the + and – operators have the same precedence. Thus the associative rule is used to determine how the expression is evaluated The associativity is from left-to-right and the expression is evaluated from left to right as; i ii iii. 2+5 iv. 7 27

 The following table gives the equivalent expressions to indicate the precedence order and associativity of operators. ExpressionEquivalent expression 17 + c * d / e(7 + ((c*d) /3)) 2b*b - 4*a*c( (b*b)- ( (4*a) * c) ) 3a / b / c( (a/b) /c) 42 + a / b * 5(2+( ( a/b)* 5)) 52 * 2.4 / 1.2 * 5( ( ( 2*2.4)/1.2) *5) / 6 * 2 - 1( ( 6+(*12/6) * 2))-1) 7a * b % c + 1( ( ( a*b) % c) +1) 28

Control Instructions in C  As the name suggests the ‘Control Instructions’ enable us to specify the order in which the various instructions in a program are to be executed by the computer.  In other words the control instructions determine the ‘flow of control’ in a program.  There are four types of control instructions in C language which are discussed below; Sequence Control Instruction :The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program. 29

Selection or Decision Control Instruction :Decision control 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. Case Control Instruction : The case control instruction is used to select option from a set of many options. 30