Behaviour of Printf


1. if '' or "" are not used the behaviour of printf becomes to be print and echo


Important Interview Question based on - escape Operator


1.How will you print \ using printf

printf("\\")

2.What does escape operator internally do ?

a -> plain Operator

% -> convertible string or operator

\-> convertible string

"=> print as it is 

\ escape operator converts convertible string to plain string

3. How to print \n inside printf()

using \\n as \ as escape operator

4. How to print %d inside printf()

Answer : behaviour of escape operator(\,%) can only be changed by its operator

itself

5. How to print % inside printf() 

use %% to escape suffix %


6. Can you a special operator will cannot be escape by \ 
% modulo operator

2. can you tell a special digit which cannot escape by \

\0 => NULL


Some of the Important escape character sequence

\a => alarm,beep

\f => form feed

\b => backspace

\v => verticle tab

\t => horizontal tab

\n => new line

\r => return carriage


Format Specifiers

1. %d - Interger

printf("The no is %d")

                                   |--------->convertible string

 

Argument List [<...>] :  is the no of supplied argument seperated by comma,s in any function as single argument entity.


for example


printf([arg1],[arg2,arg3,arg4,arg5...]); valid

                |1           1       2        3       4    |

                                                                  |2


printf([arg1],[arg2],[arg3],[arg4],[arg5]); invalid

|1     |2          |3         |4          |5

Total No of Argument = 5

Total No of Entity =5

Example:

$a=10;

$b=20;

add($a,$b);

total no of arguemnt =2

total no of Entity =2

$a=10;

$b=[10,20,30,40,50,60...]

add($a,$b);





Q1 : 

print_r([arg1],[arg2]);

printf([arg1],[arg2,arg3,arg4,arg5]);

printf([arg1],[arg2]);


In java

public static void main(String arg[])

public static void main(String arg...)

Most Common Error while using format specifier

few Argument supplied Count Error

if you using min 1 format specifier and not supplying argument list

too few argument supplied Error will raise.


character datatype = 


1 .gettype() => string 

2. strlen() => length in digit


if length == 1 it is character

else

 it is string 

This can  be using ord() function


Sprintf() : it same as printf but it donot print anything rather it returns

$x=sprintf("This is no value = %d",10);

echo $x;

This is no value = 10


Difference b/w printf and sprintf

$a=sprintf("vakue=%d",10);  //Output generated by print assignd  as output string

$b=printf("vakue=%d",10); //no of character display on the screen

php > printf("This is no value = %d",10);

This is no value = 10

php > sprintf("This is no value = %d",10);

php > $x=sprintf("This is no value = %d",10);

php > echo $x;

This is no value = 10

php > $x=printf(10);

10

php > echo $x;

2

php > $x=printf(1000000);

1000000

php > echo $x;

7

7

php > $x=printf("awnish");

awnish

php > echo $x;

6

php > $y=printf(10);

10

php > echo $y;

2

php > $y=printf(printf("awnish"));

awnish6

php > echo $y;

1

php > $x=print(1000);

1000

php > echo $x;

1

php > 


Write a program to take name as input from the user

and find the length of name


Trick: printf() returns no of character printed on the screen


echo "The length of string "; 

$length=printf("%s",$name);

echo "={$length}";