0% found this document useful (0 votes)
4 views3 pages

Lect 1 - Part 2

The document explains the usage of the scanf function in C for reading formatted input from the standard input stream. It details the syntax, format specifiers, and provides examples for reading integers, floating-point numbers, and multiple inputs. The document emphasizes the importance of using the address operator '&' to store the input values in the specified variables.

Uploaded by

malakmahamad376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Lect 1 - Part 2

The document explains the usage of the scanf function in C for reading formatted input from the standard input stream. It details the syntax, format specifiers, and provides examples for reading integers, floating-point numbers, and multiple inputs. The document emphasizes the importance of using the address operator '&' to store the input values in the specified variables.

Uploaded by

malakmahamad376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lect 1_part 2

scanf in C

In C, scanf is a function that stands for Scan Formatted String. It is the
most used function to read data from stdin (standard input stream i.e.
usually keyboard) and stores the result into the given arguments. It can
accept character, string, and numeric data from the user using
standard input. It also uses format specifiers like printf.
Example:
#include <stdio.h>

int main() {
int n;

// Reading an integer input


scanf("%d", &n);
printf("%d", n);
return 0;
}

Input
10
Output
10
Explanation: In this example, scanf(“%d”, &n) reads an integer from
the keyboard and stores it in the integer variable n. The %d format
specifier indicates that an integer is expected, and &n provides the
memory address of n so that scanf can store the input value there.
scanf Syntax
The syntax of scanf() in C is similar to the syntax of printf().
scanf(“format”, address_of_args… );
Here,
 format: It is the format string that contains the format specifiers(s).
 address_of_args: Address of the variables where we want to store
the input.
We use & operator to find the address of the variables by appending it
before the variable name.

1
Example format specifiers recognized by scanf:
%d to accept input of integers.
%ld to accept input of long integers
%lld to accept input of long long integers
%f to accept input of real number.
%c to accept input of character types.
%s to accept input of a string.

Examples of scanf
The below examples demonstrate the use of scanf for different types of
input:
Reading Floating Point Value
#include <stdio.h>

int main() {
float f;
printf("Enter the value:\n");

// Reading real number and storing in f


scanf("%f", &f);
printf("You entered: %.2f", f);
return 0;
}

Console
Enter two numbers:
10.4 (Press Enter)
You Entered: 10.40
Explanation: The scanf(“%f”, &f) statement reads a floating-point
number from user input and stores it in the variable f. The %f format
specifier ensures the correct interpretation of the input, and &f passes
the memory address of f to store the value.
Take Two Integers as Input
#include <stdio.h>

int main() {
int a, b;
printf("Enter two numbers:\n");

// Reading two integers and storing


// them in a and b
scanf("%d %d", &a, &b);

2
printf("You entered: %d %d", a, b);
return 0;
}

Console
Enter two numbers:
10 (Press Enter)
20
You Entered: 10 20
Explanation: scanf(“%d %d”, &a, &b) read two integers from the
standard input. The space in the format string %d %d tells scanf to
expect whitespace (spaces, tabs, or newlines/enters) between the
two numbers. When you press Enter after typing the first number, it’s
interpreted as whitespace, and scanf proceeds to read the second
integer. The values are then stored in the integer variables a and b,
respectively.

You might also like