7.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 2.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Computer Programming w/ Eng. Applications
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.
Structure of a C program
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to C Programming
CS113 Introduction to C Instructor: Ioannis A. Vetsikas
Programming in C Spring Semester 2013Programming and Data Structure1.
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.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Programming and Data Structure CS13002 Pabitra Mitra Dept. of Computer Science & Engineering Room No: 310.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Input, Output, and Processing
Introduction to Computer Systems and the Java Programming Language.
CPS120: Introduction to Computer Science
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 4.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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 Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CS111 Computer Programming
Chapter 2 - Introduction to C Programming
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Presentation transcript:

Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 2

Sudeshna Sarkar, IIT Kharagpur 2 Examples of Software Read an integer and determine if it is a prime number. A Palindrome recognizer Read in airline route information as a matrix and determine the shortest time journey between two airports Telephone pole placement problem Patriot Missile Control A Word-processor A C language Compiler Windows 2000 operating system Finger-print recognition Chess Player Speech Recognition Language Recognition Discovering New Laws in Mathematics Automatic drug discovery

Sudeshna Sarkar, IIT Kharagpur 3 Programming Languages Machine language Only the machine understands. Varies from one class of computers to another. Not portable. High-level language Easier for the user to understand. Fortran, C, C++, Java, Cobol, Lisp, etc. Standardization makes these languages portable. For example, C is available for DOS, Windows, UNIX, Linux, MAC platforms.

Sudeshna Sarkar, IIT Kharagpur 4 Operating Systems Makes the computer easy to use. Basically the computer is very difficult to use. Understands only machine language. Categories of operating systems: Single user Multi user Time sharing Multitasking Real time

Sudeshna Sarkar, IIT Kharagpur 5 DOS is a single-user operating system. Windows 95 is a single-user multitasking operating system. Unix is a multi-user operating system. Linux is a version of Unix Question How multiple users can work on the same computer?

Sudeshna Sarkar, IIT Kharagpur 6 Computers are often connected in a network. Many users may work on a computer. Over the network. At the same time. CPU and other resources are shared among the different programs.

Sudeshna Sarkar, IIT Kharagpur 7 Multi-user environment Computer User 1User 2User 3User 4 User 5 Printer

Sudeshna Sarkar, IIT Kharagpur 8 Number System :: The Basics We are accustomed to using the so-called decimal number system. Ten digits :: 0,1,2,3,4,5,6,7,8,9 Every digit position has a weight which is a power of 10. Example: 234 = 2 x x x = 2 x x x x x 10 -2

Sudeshna Sarkar, IIT Kharagpur 9 A digital computer is built out of tiny electronic switches. From the viewpoint of ease of manufacturing and reliability, such switches can be in one of two states, ON and OFF. A switch can represent a digit in the so-called binary number system, 0 and 1. A computer works based on the binary number system.

Sudeshna Sarkar, IIT Kharagpur 10 Digital Information Computers store all information digitally: Numbers Text Graphics and images Audio Video Program instructions In some way, all information is digitized – broken down into pieces and represented as numbers

Sudeshna Sarkar, IIT Kharagpur 11 Binary Numbers Once information is digitized, it is represented and stored in memory using the binary number system A single binary digit (0 or 1) is called a bit. A collection of 8 bits is called a byte Word: Depends on the computer 4 bytes 8 bytes

Sudeshna Sarkar, IIT Kharagpur 12 An k-bit decimal number Can express unsigned integers in the range 0 to 10 k – 1 For k=3, from 0 to 999. An k-bit binary number Can express unsigned integers in the range 0 to 2 k – 1

Sudeshna Sarkar, IIT Kharagpur 13 Variables and constants All temporary variables are stored in variables and constants. The value of a variable can be changed. The value of a constant does not change. Variables and constants are stored in main memory.

Sudeshna Sarkar, IIT Kharagpur 14 Memory How does memory look like ? A list of storage locations, each having a unique address Variables and constants are stored in these storage locations. A variable is like a house. The name of the variable is the address of the house.

Sudeshna Sarkar, IIT Kharagpur 15 Address and Values

Sudeshna Sarkar, IIT Kharagpur 16 Memory Map Every variable is mapped to a particular memory address C 32

Sudeshna Sarkar, IIT Kharagpur 17 Variables in Memory Instruction executed Memory location allocated to a variable X TimeTime X = X = 20 X = X +1 X = X*5

Sudeshna Sarkar, IIT Kharagpur 18 Variables in Memory Instruction executed Memory location allocated to a variable X TimeTime X = X = 20 X = X +1 X = X*5

Sudeshna Sarkar, IIT Kharagpur 19 Variables in Memory Instruction executed Memory location allocated to a variable X TimeTime X = X = 20 X = X +1 X = X*5

Sudeshna Sarkar, IIT Kharagpur 20 Variables in Memory Instruction executed Memory location allocated to a variable X TimeTime X = X = 20 X = X +1 X = X*5

Sudeshna Sarkar, IIT Kharagpur 21 Variables (contd.) 20 ? X Y X = 20 Y=15 X = Y+3 Y=x/6

Sudeshna Sarkar, IIT Kharagpur 22 Variables (contd.) X Y X = 20 Y=15 X = Y+3 Y=x/6

Sudeshna Sarkar, IIT Kharagpur 23 Variables (contd.) X Y X = 20 Y=15 X = Y+3 Y=x/6

Sudeshna Sarkar, IIT Kharagpur 24 Variables (contd.) 18 3 X Y X = 20 Y=15 X = Y+3 Y=X/6

Sudeshna Sarkar, IIT Kharagpur 25 Multiplying two integers  0: Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 26 Multiplying two integers : Start  1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 27 Multiplying two integers : Start 1: Read 12  2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 28 Multiplying two integers : Start 1: Read 12 2: Read 13  3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 29 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14  4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 30 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 31 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9  6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 32 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14  7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 33 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13  8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 34 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 35 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9  6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 36 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14  7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 37 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13  8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 38 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 39 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9  6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 40 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14  7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 41 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13  8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 42 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 43 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9  6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 44 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14  7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 45 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13  8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 46 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 47 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9  6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 48 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14  7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 49 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13  8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 50 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 51 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9  6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 52 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14  7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 53 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13  8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 54 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15  5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 55 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5  9: Write 14 10: Halt

Sudeshna Sarkar, IIT Kharagpur 56 Multiplying two integers : Start 1: Read 12 2: Read 13 3: Load 0, 14 4: Load 1, 15 5: J_Zero 13, 9 6: Add 12, 14, 14 7: Sub 13, 15, 13 8: Jump 5 9: Write 14  10: Halt

Sudeshna Sarkar, IIT Kharagpur 57 High-Level Programs 0: Start 1: Read 20 2: Read 21 3: Compare 20, 21, 22 4: J_Zero 22, 7 5: Write 20 6: Jump 8 7: Write 21 8: Halt Variables x, y; Begin Read (x); Read (y); If (x >y) then Write (x) else Write (y); End.

Sudeshna Sarkar, IIT Kharagpur 58 The C Programming Language

Sudeshna Sarkar, IIT Kharagpur 59 Why learn C ? "Least common denominator" - good building block for learning other languages Subset of C++ Similar to JAVA Closeness to machine allows one to learn about system-level details Portable - compilers available for most platforms Very fast

Sudeshna Sarkar, IIT Kharagpur 60 /* Program Name : countdown Description : This program prompts the user to type in a positive number and counts down from that number to 0, displaying each number */ #include #define STOP 0 main () { int counter ; /* Holds intermediate count value */ int startPoint ; /* Starting point for countdown */ /* Prompt the user for input */ printf ("Enter a positive number : ") ; scanf ("%d", &startPoint) ; for (counter=startPoint; counter >=STOP;counter--) printf ("%d\n", counter) ; }

Sudeshna Sarkar, IIT Kharagpur 61 intro]$ cc t1.c intro]$./a.out Enter a positive number : intro]$

Sudeshna Sarkar, IIT Kharagpur 62 The first C program #include void main () { printf ("Hello, World! \n") ; } All programs run from the main function printf is a function in the library stdio.h To include any library use #include

Sudeshna Sarkar, IIT Kharagpur 63 Second C program #include void main() { int x = 1, y; int sum; y = 3; sum = x + y; /* adds x to y, places value in variable sum */ printf( “%d plus %d is %d\n”, x, y, sum ); }

Sudeshna Sarkar, IIT Kharagpur 64 Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’not be nested! Be careful. /* /* /* Hi */ is an example of a comment. /* Hi */ */ is going to generate a parse error

Sudeshna Sarkar, IIT Kharagpur 65 Keywords Reserved words that cannot be used as variable names OK within comments... Examples: break, if, else, do, for, while, int, void Exhaustive list in any C book

Sudeshna Sarkar, IIT Kharagpur 66 Identifiers • A token (word) composed of a sequence of letters, digits, and underscore (_) character. (NO spaces.) First character cannot be a digit C is case sensitive, so beware (e.g. printf  Printf) • Identifiers such as printf normally would not be redefined; be careful • Used to give names to variables, functions, etc. • Only the first 31 characters matter

Sudeshna Sarkar, IIT Kharagpur 67 Constants 0, 77, 3.14 examples. Strings: double quotes. “Hello” Characters: single quotes. ‘a’, ‘z’ Have types implicitly associated with them too large for most machines

Sudeshna Sarkar, IIT Kharagpur 68 Simple Data Types Void Integer types (signed or unsigned): char, short int, int, long int char is an 8 bit (=1 byte) number Floating-point types: float, double, long double No boolean types Use 0=False and anything else(usually 1)=True

Sudeshna Sarkar, IIT Kharagpur 69 Input and Output printf : performs output to the standard output device (typically defined to be the monitor) It requires a format string to which we can provide The text to print out Specifications on how to print the values printf ("The number is %d.\n", num) ; The format specification %d causes the value listed after the format string to be embedded in the output as a decimal number in place of %d.

Sudeshna Sarkar, IIT Kharagpur 70 Input scanf : performs input from the standard input device, which is the keyboard by default. It requires a format string and a list of variables into which the value received from the input device will be stored. scanf ("%d", &size) ; scanf ("%c", &nextchar) ; scanf ("%f", &length) ;

Sudeshna Sarkar, IIT Kharagpur 71 Variables Variables hold the values upon which a program acts. They are the symbolic reference to values. The following declares a variable that will contain an integer value. int num_of_students ; The compiler reserves an integer's worth of memory for num_of_students In C, all variables must be declared before they can be used.

Sudeshna Sarkar, IIT Kharagpur 72 A variable declaration conveys three pieces of information the variable's identifier its type its scope - the region of the program in which the variable is accessible. (implicitly specified by the place in the code where the declaration occurs.)

Sudeshna Sarkar, IIT Kharagpur 73 #include main () { int num_of_students ; scanf ("%d", &num_of_students) ; printf ("%d\n", num_of_students) ; } C Program # 3

Sudeshna Sarkar, IIT Kharagpur 74 Sample C program #4 #include #define PI /* Compute the area of a circle */ main() { float radius, area; float myfunc (float radius); scanf (“%f”, &radius); area = myfunc (radius); printf (“\n Area is %f \n”, area); } float myfunc (float r) { float a; a = PI * r * r; /* return result */ return (a); }

Sudeshna Sarkar, IIT Kharagpur 75 Operators Operators are used to manipulate variables. They perform arithmetic logic functions comparisons between values int x = 6 ; int y = 9; int z, w; z = x + y ; w = x * y ;

Sudeshna Sarkar, IIT Kharagpur 76 Expressions and statements Expressions : combine constants and variables with operators x * y Expressions can be grouped to form statements z = x * y ; Semicolons terminate statements One or more simple sentences can be grouped to form a compound sentence or a block by enclosing within { }

Sudeshna Sarkar, IIT Kharagpur 77 Assignment operator int x = 4 ; x = x + 9 ; 1. The right hand side is evaluated. 2. The left hand side is set to the value of the right hand side. All expressions evaluate to a value of a particular type. x + 9 evaluates to the integer value of 13.

Sudeshna Sarkar, IIT Kharagpur 78 Arithmetic operators + : addition - : subtraction * : multiplication / : division % : modulus operator distance = rate * time ; netIncome = income - tax ; speed = distance / time ; area = PI * radius * radius y = a * x * x + b*x + c; quotient = dividens/divisor; remainder=dividend %divisor;

Sudeshna Sarkar, IIT Kharagpur 79 C Program # 5 /* FIND THE LARGEST OF THREE NUMBERS */ main() { int a, b, c; scanf (“%d %d %d”, &a, &b, &c); if ((a>b) && (a>c)) /* Composite condition check*/ printf (“\n Largest is %d”, a); else if (b>c) /* return result */ printf (“\n Largest is %d”, b); else printf (“\n Largest is %d”, c); }