Wednesday 30 December 2015

All about scanf() function in C programming language

Okay, hi every one...
For the next half hour I'm going to write all about the scanf() function in C programming language. Here I'll share few links that will help you to know more about the scanf() function.

First, lets start with
"What does the scanf() function returns ?"
The answer to this is known by many of the people. It returns the number of items successfully read on the console.
You can check it through a simple C program.
#include <stdio.h>
int main()
{
    int i ;
    printf("%d", scanf("%d", &i));
    return 0;
}

Okay, you try to run this program on your compiler and observe few things.
If you input 5, you will get output as 1.
Try to input 123, what do you think the output should be?
Well, you may thing that the output should be 3 as we have entered 3 digit number. But that's not the output. The output will be again 1.

Okay, so the true interpretation of the returning value of scanf() function is that it returns the number of input taken on the console.
Now we can again see a different kind noticeable thing in the returning value by scanf() function.

Again in the above program if you try to input any character instead of the integer then you will found an unexpected output.
Suppose you enter 'a' on the console of the above program, then what do you think the output should be?
Better you try to run the same program on your compiler. You will find that this time it has printed 0.

Now the question is why 0?
Well, to know the reason for printing the 0 we have to again go to the main definition of the returned value by the scanf() function. And now when we look above we find that scanf() returns the number of items successfully read. And now here we have to note the word successfully.

So if we input a character instead of integer then that is termed as a unsuccessful read from the input and thus the scanf() returns 0.

Now few lines that I read on man is -
scanf,  fscanf, sscanf, vscanf, vsscanf, vfscanf
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
Here is the link that will help you to master this topic - Click here
Now I think we have gained appreciable knowledge on returning value of scanf().

Coming to the next big mistake that many people generally do while using the scanf() function.

Okay, Now if I ask you that make a program to accept two characters and display them. Most of the beginner programmer will make a program like -
#include<stdio.h>
int main()
{
    char c1, c2;
    printf("\n Enter characters one : ");
    scanf(" %c", &c1);
    printf("\n Enter character two : ");
    scanf("%c", &c2);
    printf("\n The the characters are %c and %c ", c1, c2);
    return 0;
}
Now I give you a task, stop reading further for few second and try to find the logical problem with this above code.
Have you found some? Anyway, if you haven't found yet then try to run this program in your compiler. Probably you will find some anomaly.

Here is the output of this program.
Enter two characters : a
The the characters are a and
Here you can see that we are only able to input the first character and not the second one.
Now some of you may have found the reason for the anomaly. No worries, if you haven't yet then I'm going to tell you.

When you enter the first character 'a' then if gets stored into the c1, and when you press enter key ('\n') then it gets stored into c2 and becomes the second character. Now there is no input instruction left so the console just prints the output.

Now one general question may arise that why does this anomaly does not occur when I perform the same task using the integer format specifier?
example :-
#include<stdio.h>
int main()
{
    int a, b;
    printf("\n Enter two numbers : ");
    scanf("%d%d", &a, &b);
    printf("\n The two numbers are %d and %d ", a, b);
    return 0;
}
Okay, again by now some of you have known the reason but for those who haven't known why below is the answer.

Now we again move one step back and see that what was happening in case we were dealing with the character format specifier. We can note that the second input given was a character ('\n') which has ASCII value of 13. But in this case when we use the integer format specifier then we see that the '\n' (which is a character ) does not get assigned to the variable b (which is a integer) i.e here we encounter the failure of the read operation (also known as matching failure) on the console and thus the next input buffer is still active and so when we press the next integer then it gets stored into the variable b.

Now here the discussion does not gets over, we have to note that if in the input buffer we have newline ('\n') then what difference it is going to create for :-

  1. scanf("%c", &charVar);
  2. scan("%d",  &intVar);
  • In case of scanf("%c", &charVar);, the newline is considered as the input, so the second scanf skips asking for the input from user.
  • In the case of scanf("%d", &intVar); the leftover newline is skipped and it wait for theinteger input to appear, so it stops and asks the user for the input. However in this case if you input a non-whitespace character and press ENTER, the char input will be considered as an input, causing a matching failure.
For more information on scanf input conversions for reading string and character values: ‘%s’, ‘%S’, ‘%[’, ‘%c’, and ‘%C’ you cal follow :-
Link 1

To have a look on online version of C11 Standard you can visit here.
Now we will see how to correctly input characters consecutively from the user. 

#include<stdio.h>
int main()
{
    char c1, c2;
    printf("\n Enter characters one : ");
    scanf(" %c", &c1);
    printf("\n Enter character two : ");
    scanf("%c", &c2);
    printf("\n The the characters are %c and %c ", c1, c2);
    return 0;
}


To make this program logically correct (so that we can take two inputs and their values can be inserted into c1 and c2 respectively) we can perform many tasks. Below I have listed some ways that you can follow.
First approach :-
  • We can put the space between the first %c and the second %c. Now you change the above code little bit and you will find your program working successfully.
Now many of you will ask that "what change the space is going to do in scanf() function?"
Okay, now lets us study about "Meaning of space in scanf() function"
  • Space in scanf format means "to skip all whitespace (such as blanks, tabs, or newlines)" from the current position on. However, when you are using a format specifier that does not ignore whitespace, including space into the format when necessary (to force the skip) will work perfectly.
  • The specifiers that do not ignore whitespace are `c` `[` and `n`. So, specifying a space in front of one of those specifiers makes a difference. Otherwise, it doesn't make any difference.

Monday 7 December 2015

Pointer to Pinter in C++ (IN HINDI)

नीचे एक pointer to pointer पर उदाहरण है :-

नीचे स्पष्ट रूप से नीचे के कोड की व्याख्या करने के लिए एक तस्वीर है। आशा है कि यह आपकी निशित रूप से मदद करेगा।
इस प्रोग्राम के पास एक पॉइंटर वेरिएबल p  है जो कि (a) की एड्रेस पर पॉइंट करता है। और एक और पॉइंटर वेरिएबल है जो की p की एड्रेस को पॉइंट करता है जिसका नाम क्यू (q) है। इसे ही हम pointer to pointer का नाम देते है। इसके साथ एक और pointer to pointer to pointer वेरिएबल है जो की r है यह वेरिएबल क्यू(q) की एड्रेस पर  पॉइंट करता है।

// उदाहरण :- pointer to pointer पर
#include<iostream>
using namespace std;
int main()
{
    int a = 6;
    int *p;
    p = &a;
    int **q;
    q = &p;
    int ***r;
    r = &q;
    cout<<" Value of &p : "<<&p<<endl;// address of p
    cout<<" Value of p : "<<p<<endl;// address of a
    cout<<" Value of *p : "<<*p<<endl;// 6
    cout<<" Value of q : "<<q<<endl;// address of p
    cout<<" Value of *q : "<<*q<<endl;// address of a (value stored in p)
    cout<<" Value of **q : "<<**q<<endl;// 6
    cout<<" Value of r : "<<r<<endl;//  address of q
    cout<<" Value of *r "<<*r<<endl;// address of p
    cout<<" Value of **r : "<<**r<<endl;// address of a
    cout<<" Value of ***r : "<<***r<<endl;// 6
    return 0;
}


Saturday 5 December 2015

Difference between pointer variable and Reference variable in C++ (IN HINDI)

एक रिफरेन्स वेरिएबल एक प्रकार का उपनाम है। एक वेरिएबल जो पहले  से प्रोग्राम में मौजूद है उसी के उपनाम को हम रिफरेन्स वेरिएबल कहते हैं। एक बार जब हम रिफरेन्स का आरंभीकरण कर देते हैं, तब हम वेरिएबल अथवा रिफरेन्स का उपयोग उस वेरिएबल को अपने प्रोग्राम में उपयोग करने के लिए इस्सतमल कर सकते हैं।

C++ References एवं  Pointers के ३ बड़े अंतर :

रिफरेन्स को काफी समय पॉइंटर्स समझ लिया जाता है। परन्तु यह गलत है, निचे कुछ अंतर दिए गए हैं।
  • हम रिफरेन्स को नुल्ल् वैल्यू के साथ सहयोगी नहीं बना सकते , परन्तु हम पॉइंटर्स को नुल्ल् वैल्यू के साथ सहियोग कर सकते है।
  • अगर एक बार रिफरेन्स किसी वेरिएबल / ऑब्जेक्ट(object) के साथ सहियोग में हो गया तो हम उस रिफरेन्स की वैल्यू चेंज नहीं कर सकते (मतलब अब हम उस रिफरेन्स को किसी और वेरिएबल के साथ साहियोह नहीं कर सकते )। पॉइंटर्स के साथ ऐसा नहीं है। हम पॉइंटर्स की वैल्यू de-referencing से बदल सकते हैं। 
  • // example
    int x = 5;
    int y = 6;
    int *p;
    p =  &x;
    p = &y;
    *p = 10;
रिफरेन्स उदाहरण
  • int x = 5;
    int y = 6;
    int &r = x;
  • एक रिफरेन्स वेरिएबल को उसके निर्माण के समय ही किसी दूसरे वेरिएबल के साथ सहियोग करना जरुरी है। परन्तु हम पॉइंटर्स को कभी भी किसी वेरिएबल के साथ सहियोग कर सकते है। 

Creating References in C++ :-

हम अपने प्रोग्राम में एक वेरिएबल घोषित कर देते है। 
`int var = 5 ;`
अब हम रिफरेन्स वेरिएबल कुछ इस प्रकार घोषित कर सकते है। 
`int& ref = var ;`
हम लोग इस घोषणा को इस प्रकार पढ़ते है (`ref` is an integer reference initialized to `var`)

उदाहरण
#include<iostream>
using namespace std;
int main()
{
    cout<<"program to illustrate use of reference variable : \n";
    int var ;
    int & ref = var ;

    var = 5;
    cout<<"The value of var is : " << var << endl ;
    cout<<"The value of reference variable ref is : " << ref << endl ;
    return 0;
}

Output :-
program to illustrate use of reference variable : 
The value of var is : 5
The value of reference variable ref is : 5