Explain about Formatted Input in detail with the prototype of ‘scanf’’ function in C
programming
scanf()
function
v The scanf() function
id formatted input function it depends on Format specifiers.
v The scanf() function
is used to read multiple data values of different data types from the keyboard.
v The scanf() function
is built-in function defined in a header file called "stdio.h".
When we want to use scanf() function in our program, we need to include the respective
header file (stdio.h) using #include statement.
Syntax:
scanf("format
strings",&variableNames);
Format
string: %d, %c, %f, %s has been use as format
string.
Variable list: Name of variable followed by address
operator where input though keyboard been transferred.
Example
scanf(%d”,&a);---------->Integer input
scanf(“%c”,&a);---------> character input
scanf(“%f”,&a);---------> float input
scanf(“%s”,&a);---------> string input
Space will not be accept through keyboard
Scanf("%d%d%s", &a,
&b, &c);
Example Program
#include<stdio.h>
int main()
{
int i;
printf("\nEnter any integer
value: ");
scanf("%d",&i);
printf("\nYou have entered %d
number",i);
return 0;
}
Output:
1.
Constants and types of constants with examples
Definition:
A constant is a named memory location which holds only one value
throughout the program execution.
Types
of Constants:
1.
Integer constants
2.
Floating Point constants
3.
Character Constants
4.
String Constants
Integer constants
v An integer constant can be a decimal integer or octal integer or
hexadecimal integer.
v A decimal integer value is specified as direct integer value whereas
octal integer value is prefixed with 'o' and hexadecimal value is prefixed with
'OX'.
Floating Point constants
v
A floating-point
constant must contain both integer and decimal parts.
v
Some times it may
also contain the exponent part. When a floating-point constant is represented
in exponent form, the value must be suffixed with 'e' or 'E'.
Example
Float f=3.14
The floating-point value 3.14 is represented
as 3E-14 in
exponent form.
Character Constants
A character constant is a symbol enclosed in
single quotation. A character constant has a maximum length of one character.
Example
char ch='A';
char b='2';
char c='m';
String Constants
A string constant is a collection of characters, digits, special
symbols and escape sequences that are enclosed in double quotations.
Example:
Char name [30] =”Hi How r u?”
Explain the
precedence and associativity rules of operators help in executing a ‘C’
expression with an example.
C
Operator Precedence and Associativity
Operator Precedence:
Operator precedence is
used to determine the order of operators evaluated in an expression. In c
programming language every operator has precedence (priority). When there is
more than one operator in an expression the operator with higher precedence is
evaluated first and the operator with the least precedence is evaluated last.
Example:
10 + 4 * 3 / 2
In the above expression,
there are three operators +, * and /. Among
these three operators, both multiplication and division have the same higher
precedence and addition has lower precedence. So, according to the operator
precedence both multiplication and division are evaluated first and then the addition
is evaluated. As multiplication and division have the same precedence they are
evaluated based on the associativity. Here, the associativity of multiplication
and division is left to right. So,
multiplication is performed first, then division and finally addition. So, the
above expression is evaluated in the order of *
/ and +. It is evaluated as follows...
4 * 3 ====> 12
12 / 2 ===> 6
10 + 6 ===> 16
the expression is evaluated to 16.
Operator Associativity:
Operator associativity is
used to determine the order of operators with equal precedence evaluated in an
expression. In the c programming language, when an expression contains multiple
operators with equal precedence, we use associativity to determine the order of
evaluation of those operators.
Let us try to evaluate
an arithmetic expression as shown below:
x = a-b/3+c*2-1
Let a = 9, b =12, and
c=3. Then our expression becomes:
x = 9-12/3+3*2-1
From the above table,
we can see that the * and / operators are having higher precedence than + and –
operators. Also, the * and / operators are at the same level of precedence, so
we have to apply the associativity rules. Since the associativity rule is
left-to-right, we apply the / operator first and the expression evaluates to:
x = 9-4+3*2-1
Next, we apply the *
operator and the expression becomes:
x = 9-4+6-1
Next, we apply the
first – operator as the – and + operators are at the same level and the
associativity rule is from left to right. The expression becomes:
x = 5+6-1
Now, we apply the +
operator and the expression become:
x = 11-1
Finally, we apply the
– operator and the result is:
x = 10
No comments