Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CSC141- Introduction to Computer Programming
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Structure of a C program
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Basic Input/Output and Variables Ethan Cerami New York
C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.
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.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
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.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
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.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Variables, Identifiers, Assignments, Input/Output
User-Written Functions
User Interaction and Variables
Basics (Variables, Assignments, I/O)
Formatted Input/Output
LESSON 3 IO, Variables and Operators
Input/output.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Basics (Variables, Assignments, I/O)
Formatted Input/Output
By: Syed Shahrukh Haider
CMSC 104, Section 4 Richard Chang
Introduction to C Programming
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
Visit for more Learning Resources
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Basics (Variables, Assignments, I/O)
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
INPUT & OUTPUT scanf & printf.
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Introduction to C Programming
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
Course Outcomes of Programming In C (PIC) (17212, C203):
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
Presentation transcript:

Lecture No: 16

The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard). The scanf() function read information for numbers and other datatypes from standard input (often console or command prompt). The scanf() function prototype is: scanf(“ format specifier”, &variable list);

Scanf() function cont…. In prototype of scanf() function, the format strings could be %f for input real values %d for input integer values %c for input char values etc… Following are some examples of usage of scanf() statement. scanf(“%d”, &a); scanf(“%d %f”, &i,&f);

As you can see, that the format for scanf() looks very much like that for printf(), the arguments on the left is a string that contain format specifier and variables names on the right side. The format specifiers for scanf() are similar to printf()function. %d for integer values %f or %e for real values %c for character %l for long int %lf for double type.

In pervious examples, you may notice that “&” operator before the variable name in scanf() statement is must. “&” is a pointer operator or it is also called Address operator or ampersand. The meaning or working of this operator is that it tells the memory address of variable where the input value will be store. Example: printf(“Enter value:”); scanf(“%d”, &a); Output: Enter value: a 6

Taking multiple values using scanf() function The scanf() function accept several variable at once. Means we can input several input values in single scanf staement. To demonstrate this, lets take the following programming example: void main(void) { int i; char c; float f; printf(“Enter values for i, c and f variable); scanf(“%d %c %f”, &i,&c,&f); printf(“value of i is %d, value of c is %c and value of f is %f”, i,c,f); getch(); }

The scanf() function accept several variable at once. Means we can input several input values in single scanf statement. To demonstrate this, lets take the following programming example: void main(void) { int i; char c; float f; printf(“Enter values for i, c and f variable); scanf(“%d %c %f”, &i,&c,&f); printf(“\nvalue of i is %d, value of c is %c and value of f is %f”, i,c,f); getch(); } Taking multiple values using scanf() function OUTPUT Enter values for i c and f variable: 2 B Value of i is 2, value for c is B and value for f is 36.34

In pervious example, now the question will arise here that how does scanf() knows when we’ve finished typing one value and start another? Answers is that, As we type our three input values (suppose 2,B, 36.34) we separate them by spaces. The scanf() matches each space we type with the corresponding space b/w the format specifier in scanf(). If we have tried to separate the values with another character – (dashed or comma) this would not have matched the space in format strings/ specifier. This process is shown in next slide.

Taking multiple values using scanf() function Scanf(“%d %c %f”, &i,&c,&f); i c f 2 B 36.34

Keywords Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called “Reserved words”. There are only 32 keywords available in C. The list of keywords in C are shown in next slide.

auto double if static case breakelse int struct enum long switch char extern near while typedefconstfloat register union continuefarreturn unsigned default forshortvoid do goto signed Keywords