Powered By Blogger

Wednesday 12 May 2021

Can we use pointers without specifying the data type of the pointer?

Void pointer
Void Pointer

Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, when we will try to get the value of the data or a variable stored in that pointer, we need to declare the data type of the pointer in an indirect way. 

So, before knowing the process, let's take a peek on what is pointer?

POINTER:- 


In C programming we use variables to store our desired value. For this, the compiler allocates some memory for the variable to store our desired value. So, when we try to get the value of that variable, we always specify the data type of the variable. But we also can indirectly manipulate or use the value stored in the variable by the use of this so-called POINTER. 

How the process works:-

Pointer stores the address of the variable that we want to use it through a pointer. And by linking the address of that variable with the pointer, we can use and manipulate the data saved in the variable.

Let's take an example to see how the whole process is going on-

Programming code:-
                                     #include<stdio.h>
                                     int main ( )
                                     {
                                            int30*ptr ;
                                            ptr = &;
                                            printf "%d", *ptr ) ;
                                     }

output:-
               30


variable value (x)
30
address of the variable
2424388
pointer value (*ptr)
2424388
address of the pointer
2424300






NOTE:- Every time we take a pointer, the data type of the pointer should be the same as that of the data type of the variable.

Now getting into our main question how can we use pointers without specifying its data type?

In C programming, we use integer, floating-point , character, etc. data types. But there is another data type VOID”. We can say this is a data type with no data type. You can say what is that means?

If we take an example :

                                      void name;

this implies the data type of the variable ‘name’ is not specified whether it is an integer, floating-point, or character data type.

  • Void means ‘null’.


  • By using this ‘void’ we can declare a pointer without specifying its data type and later we can get the value of the variable that is stored in that pointer.
By the following programming code, we can do that-

Programming Code


Output:-

Enter a number: 10
The entered number is 10



Let's see how the programming code works:



  • First, we have taken a variable of an integer data type.


  • Then, we have taken a ‘void’ pointer as “*ptr”. (‘*’ denotes that ‘ptr’ is a pointer). So, the data type of the pointer is not declared.


  • Next, we have asked the user to give input by the use of ‘scanf’ function.

Last and the main part:-

                                         printf(“The entered number is %d”,*(int *)ptr);


  • by the argument  “*(int *)ptr”, we are declaring that the data type of the variable stored in the pointer is of ‘integer’ type.


  • As I have already told that when we want to get the value of the variable through a pointer, we must declare the data type at that time otherwise, the compiler will give an error.


  • By the expression “(int *)”, the compiler understands that the variable is of integer type and if we use only this expression, then the address of the variable will be printed instead of its value.

Prints the address of the variable

Output:-
The entered number is 2424388





  • And by “*(int *)” this expression, compiler prints the value of the variable that we wanted to use it through the pointer.

Hope it has helped you.
If any doubt persists, please comment below. Thank you for reading.

Wednesday 31 March 2021

How to Print Fibonacci Series in 'C' Programming

Welcome viewers to my new blog. Today we will see how to get FIBONACCI SERIES using ‘C’ programming.







It is very easy programming to print ‘Fibonacci Series’ using ‘C’ programming.

First, let's see what is ‘Fibonacci Series’ ?


‘Fibonacci Series’ is a series of numbers in the way that each number is the sum of two preceding numbers.

For example:  a series-  0,1,1,2,3,5,8,13,21,.............

Its a Fibonacci series as we can see, 

the number ‘1’ comes after adding its two precrding numbers (i.e  0,1,1,......  ;  0+1=1),then the number ‘2’ comes after adding its two precrding numbers (i.e  0,1,1,2,......  ;  1+1=2),then we get the number ‘3’ by the same way(i.e   0,1,1,2,3,........;  1+2=3), 
by that way we get the following numbers-

‘5’:  0,1,1,2,3,5,...........;   2+3=5
‘8’:  0,1,1,2,3,5,8,..............;   3+5=8
‘12’:  0,1,1,2,3,5,8,13,............;   5+8=13

This is how the whole ‘Fibonacci Series’ proceed.

Now, let's move to our programming part:-



    1)   VARIABLE DECLARATION PART:  In this part, we have taken the following variables ,all of integer type.

int  ab=0c=1n;

Here, we have initialized the value of b=0 and c=1 in the declaration section. Its use will be discussed in the main programming part.

   2)  GETTING INPUT FROM THE USER:  Here is used a printf function and a scanf function to get the input from the user to know upto how many numbers, he/she wants to get the ‘Fibonacci Series’.

printf("Enter the value of 'n' upto which you want to calculate 'Fibonacci Series': ");
scanf("%d",&n);

1 3)  LOOPING AS WELL AS ARGUMENT AND MAIN CODING PART:  The coding part is very important for getting any ‘Fibonacci Series’.

                           
Here, we have used a ‘while loop’ that will iterate until the value of ‘n’(input from the user) becomes zero(0).

Now, we have given an argument as follows and this is the most important part of that programming:
                                    printf("\n\nThe 'Fibonacci Series' is as follows :-\n");
                                    while(n>0)
                                    {
                                                a=b+c;
                                                c=b;
                                                b=a;
                                                printf("%4d",c);
                                                n--;
                                      }


We have taken the arguments:-   a=b+c;  c=b  and  b=a;
In the initialization part, the value of ‘b=0’ and ‘c=1’.

Let's see the whole loop goes on:-

Let, the user given the value of the variable ‘n’=8

                                 i.            When  n=8 ,b=0 , c=1
a=b+c=0+1=1   So, a=1
Now,  Next Argument:-
c=b , current value of b=0,  So, c=0
b=a ,current value of a=1,  So,b=1

Output:-    0

                               ii.            Now,  n=7(there’s an decrement operator present  i.e  n--) , b=1 , c=0
a=b+c=1+0=1   So, a=1
Now,  Next Argument:-
c=b , current value of b=1,  So, c=1
b=a ,current value of a=1,  So,b=1

Output:-    0  1

                              iii.            Now,  n=6 , b=1 , c=1
a=b+c=1+1=1   So, a=2
Now,  Next Argument:-
c=b , current value of b=1,  So, c=1
b=a ,current value of a=2,  So,b=2

Output:-    0  1  1

                             iv.            Now,  n=5 , b=2 , c=1
a=b+c=2+1=1   So, a=3
Now,  Next Argument:-
c=b , current value of b=2,  So, c=2
b=a ,current value of a=3,  So,b=3

Output:-    0  1  1  2

                               v.            Now,  n=4 , b=3 , c=2
a=b+c=3+2=5   So, a=5
Now,  Next Argument:-
c=b , current value of b=3,  So, c=3
b=a ,current value of a=2,  So,b=2

Output:-    0  1  1  2  3

...........................................

By this way, the whole process goes on and the final output screen becomes :
0   1   1   2   3   5   8  13

  Ø    The program’s argument section can be written in another way:-
printf("\n\nThe 'Fibonacci Series' is as follows :-\n");
                                    while(n>0)
                                    {
                                                a=b+c;
                                                b=c;
                                                c=a;
                                                printf("%4d",c);
                                                n--;
                                      }

  Ø  For this type, we need to initialize the value of  b=1  and  c=0.

  Ø  Trick to remember the code:  You first write blindly the argument,  a=b+c

then, Either you take  b=c  or  c=b
·         If you take  b=c,  then the next argument should be  c=a

Mind by this arrow way:   b=c
                                          
                                         c=a    ⇒ ‘a’ always be in this position

You need the initialize the value of  c=0(on which arrow mark is placed)  and  b=1

·         If you take c=b,  then the next argument should be  b=a
Mind by this arrow way:   c=b
                                           
                                         b=a     ⇒ ‘a’ always be in this position
                                                         
You need the initialize the value of  c=1 and  b=0(on which arrow mark is placed) 


You can remember the series in that way. Hope this helps you. Please share and comment. Thank you for reading.





Thursday 26 March 2020

How to print a word or sentence inside double quotation(i.e " ") in 'C' programming

Welcome viewers to this new blog............

Today,we will print any word or sentence inside double quote using 'printf' function in 'c' programming.


Printing sentence within double quotes


As we all already know, how to print any word or sentence or value using "printf" function in 'c' programming.

for example, if we want to print a sentence: To Infinity & Beyond
then, we write: 
                          printf("To Infinity & Beyond");

Output:  To Infinity & Beyond


But have we ever tried to print it inside double quotation(" ") to make them look better.

If you simply want to write it in the 'printf' function, then it will show you an error.

for example:
                     printf(""To Infinity & Beyond"");

Output: (no output)
You should get an error.

Error Message


So, to print any word or sentence inside the double quotation, we need to write the program in the following way:

                                   printf("\"To Infinity & Beyond\"");

Writing the printf function to print double quote


Output:     "To Infinity & Beyond"

Output Window


So, we need to use (\") before the starting of the word or sentence and again one more(\") after the completion of the sentence.

So, that is the simple method by which you can print any word or sentence inside double quotation using 'printf' function in 'c' programming.

Hope, this helps you. Thank you very much for reading my blog. Do comment and share if this helps you. Thank you.

PRINT HOLLOW DIAMOND OF NUMBERS IN 'C' PROGRAMMING (PRINTING PATTERN IN 'C')

PRINT HOLLOW DIAMOND OF NUMBERS IN 'C' PROGRAMMING (BY ANY NUMBER THAT CAN BE DIVIDED BY 4 IN 'C' PROGRAMMING)


WELCOME VIEWERS TO MY NEW BLOG. LET'S SEE WHAT IS TODAY'S PATTERN

HOLLOW DIAMOND PATTERN IN 'C'


Today, we will print this quite diamond-like pattern in 'c' with numbers distributed as in the above picture.

First, let's see the programming code for this hollow diamond printing..........

PROGRAMMING CODE TO PRINT UPPER HALF OF THE DIAMOND

PROGRAMMING CODE TO PRINT LOWER HALF OF THE DIAMOND



FULL PROGRAMMING CODE TO PRINT THR DIAMOND



Here is the PDF file link to download the programming code......

CLICK ON DOWNLOAD BUTTON


Wednesday 25 March 2020

Difference between Left justified and right justified format in 'c' that is "%nd" and "%-nd" in the format specifier of 'C' programming (n denotes any numerical value)

First, we need to know, what is format specifier in 'c programming?




The format specifier specifies the format of the printable or scannable variable whether it is an integer(%d), floating point (%f), character(%c), string(%s) etc. which we want to print or scan from the user.

That means it tells the compiler about the format of the taken variable which we want to print or scan.

For example: 
                     printf("%c",a);
Compiler understands that the format to print the value of the variable 'a' is a character type.
                      scanf("%f",&x);
This time compiler understands that the format to read the variable 'x' is a floating-point type.

This is simple to understand.
Now, when we add any value before any format specifier, let's see what happens.
For example:. printf("%3d",a);
Let, a=12345
Naturally ,itwill print the value of 'a' i.e 12345 though we have specified the integer limit i.e 3.
But if a=12
Then, it will print:.  (space)12
That is when the no. of number of 'a' exceeds the integer limit(%3d), then it prints the whole value of the corresponding variable i.e. 12345(n=total numbers=5).
But for the second case, as the total numbers in 'a' are less than the integer limit, then it prints the required space before the numbers to adjust the limit. For this, it prints only one space as there is total of two numbers in 'a'.

If a=356
printf("%5d",a);

Then the output will be:

(space) (space) 356

Now, coming to our main part.
At most, we have seen the use of only positive integer limits, but if we take negative, then what will happen?

Let's see, if we take the negative integer limit, then the variable value will be printed in "Left justified" way.

For example: 
x=1356
printf("%-5d",x);

The output will be:
1356(space)
That means this time, the value of the variable is printed first and thereafter the required number of spaces printed, this type of form is called "Left justified" form

So, we conclude that, if the total numbers in a variable are less than the given integer limit then, for positive limit the value will be printed in "Right justified form" and for negative limit, it will be printed in "Left justified form".

This feature is very useful to print spaces before or after any value for good visualization of output frame in 'C' programming.

Hope, this helps you. Thank you very much for reading.

Sunday 22 March 2020

EVALUATION OF AN EQUATION AND SUM USING ‘GOTO’ AND ‘POW’ FUNCTION IN ‘C’ PROGRAMMING...


EVALUATION OF THE FOLLOWING EQUATION AND SUM USING ‘GOTO’ AND ‘POW’ FUNCTION IN ‘C’ PROGRAMMING...

EQUATION FOR EVALUATION OF SUM IN 'C'
EQUATION FOR EVALUATION OF SUM IN 'C'





SUM= (1+X+X2+X3+X4+..........+Xn)

Today in this we are going to discuss the above equation and to evaluate the Sum by using ‘goto’ function and ‘pow’ function in ‘C’ programming.
So, let's start...............
Before starting let's see the programming code used here.......

PROGRAMMING CODE IN 'C' PROGRAMMING
PROGRAMMING CODE IN 'C' PROGRAMMING





INITIALIZATION PART: here, we have taken ‘sum’ and ‘x’ as float value.
Then we have taken two variables ‘n’ and ‘i’ as an integer value. Lastly, a character variable has been taken as ‘choice’.
This is the initialization part, we will see the use of the above variables in the following parts.


float sum,x;

int n,i;
char choice;

Let's move to the argument making part.

ARGUMENT MAKING  AND MAIN CODING PART:  In this part, we will see the use of ‘goto’ and ‘pow’ function. Lets what is this thing and how they work?

“GOTO” FUNCTION:  The function is already included in stdio.h header file. This function mainly works to move to the control to a position of the programme where it is set to move. That means it can move the control of the execution in any part of the program code.

For example: if we write a program
                      int n,x;
                     input: printf(“Enter the value of ‘n’”);
                     scanf(“%d”,&n);
                     if(n%2==0)
                         {           
                                printf( “The number can be divided by 2”);
                          }
                      else
                         {
                               printf(“The number can’t be divided by 2”);
                          }
                      printf(“Do you want to continue? press 0 or 1”);
                     if(x==1)
                      {
                           goto input;
                       }
                       else

                       printf(“Thank you........”);



By the above program, we can decide that the number given by the user can be divided by 2 or not.  Then we are taking a decision from the user whether he wants to continue or repeat the program again or not. So, we need here the ‘GOTO’ function. For this, we need to specify a location in the program which tells the compiler to repeat the execution of the program once more or again and again according to the input from the user. So, we have marked the location as “input”. Remember after specifying the location and taking the variable, there should be a colon after the declaration of the location i.e (input:) and the code format will be “goto(space) (name as given in the location)”.

In the given example if the user gives input ‘1’ then the control goes to the “input” named location.
Thus, repeating the execution once more.

Now, What is “pow”?
‘POW’ denotes power. It is a math function included in “math.h” header file. So, before using ‘pow’ function we must include the ‘math.h’ header file in the header section. The function mainly helps to make the power of a number.
It is as the following format:



                                                          pow((number),(power))



for example:  pow(2,4)==24==16   or  pow(3.5,2)==3.52==12.25
That how the ‘POW” function works.


let's move to our coding section.
we have used here a ‘for loop’ to evaluate the value of the sum.

        sum=1;

for(i=1;i<=n;i++)
{
sum=sum+pow(x,i);
}


As seen, inside ‘for loop’ the argument  is  “sum=sum+pow(x, i)”
taken sum=1  and ‘for loop’ starts with i=1 up to i<=n.

If user gives the input x=4 and n=3 then,
So, when i=1
sum=1+pow(4,1)==5

Now, the present value of ‘sum’=65
when i=2
sum=5+pow(4,2)==21

Now,the present value of ‘sum’=21
when i=3
sum=21+pow(4,3)=85

So, the evaluated ‘sum’ is equal to 85.

This way the argument works with ‘pow’ function by the argument  “sum=sum+pow(x,i)”

Now, the ‘for loop’ ends.

Next, we are moving towards the user’s decision making section where he/she chooses whether he wants to repeat the program once more or not.


printf("\n\nDo you want to continue ?\nPress y or n.....\n");
fflush(stdin);                  /*it is used to make the scanf function to get input from user without skipping*/
scanf("%c",&choice);            /* or a space before %c can also do the same*/
if(choice=='y'||choice=='Y')
{
    printf("\n\n");
goto input;
}
else
    printf("\n\nThank You......");



Here, we are taking input from the user in the form of “Y/y or N/n”.
NOTE: We have used a function “fflush(stdin)” to stay the input from the user at the time of asking from the user. It can also be achieved by giving a (space) before the formatting string “%c” i.e. “(space)%c”. Without it, the input from the user can’t be taken, it will skip the respective ‘scanf’ part, thus not getting any input from the user. You can try it and see what is happening without it.
Then, we have taken an ‘if’ function with the condition      
                 if(choice==’Y’||choice==’y’)
As the input from the user is saved in the variable ‘choice’ , so we have given this condition. Here the sign ‘||’ stands for “OR” meaning and remember the sign ‘&&’ stands for “AND” meaning. If the user enters capital ‘Y’ or(||)  small ‘y’ then, as per ‘GOTO’ function control moves to ‘input’ location in the program. Otherwise, the program ends with printing “Thank you......”


Hope, the given example is understandable to you. If any doubt persists please comment below and if this helps you please share the blog. Thank you so much............. 

Thursday 19 March 2020

List of ASCII values in 'C' Programming with Pdf

For your comfortability I am giving here the List of important ASCII values and also pdf is given below.

For better understanding, please go through the given example in the following link:  Click Here



ASCII  VALUE  OF  CHARACTERS

THE ASCII VALUES ARE VERY IMPORTANT FOR PROGRAMMING. BY THIS VALUES COMPILER UNDERSTANDS ANY CHARACTER. ASCII VALUES ARE INTEGER VALUES OF THE CORRESPONDING CHARACTER.
THE CONCEPT WILL BE MORE CLEAR TO YOU BY SOME EXAMPLES GIVEN ON MY BLOG:
2nfinityandbyond.blogspot.com
for example : 
CLICK HERE


ASCII VALUES
ASCII VALUES

ASCII VALUES
ASCII VALUES




Click ON download

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...