Tuesday, February 22, 2011

small braces with functions......

While playing with the programs i certainly come up with the strange thing that samll braces does not matter with functions ....I mean to say that they does not matter if closed properly...
let us make this more clear by the help of an example....

#include<stdio.h>
#include<conio.h>
void func(int);
void main()
{
int f=20;
func(f);
getch();
}
void func(int num)
{
printf("num is:%d",num);
}

Everybody knows the output yes you are thinking right the output is:
num is:20

Now coming to the point,everybody knows about function declaration,function definition,function calling and i am not going to explain that...

change function calling statment to
(func(f));,
 ((func(f)));,
 (((func(f)))); and any number of braces but the conditions is that the braces must be balanced....that is you can't say ((func(f))
you will get the same output,the same applies with function definition and function calling....

function declaration:
void func(int);, 
void (func(int));, 
void((func(int)));, 
void (((func( int)))); and with any number of balanced braces are equivalent and does not lead to any compiler error....

function definition:
void func(int num), 
void (func(int num)), 
void ((func(int num))) and with any number of balanced braces are equivalent and does not lead to compiler error..

Are you amazed?????? And dun agree with me...run the follwing one and respond to this post.........

#include<stdio.h>
#include<conio.h>
void ((((func(int)))));
int (main())
{
    int f=20;
clrscr();
((func(f)));
    getch();
    return 0;
}
void (((((((func(int n))))))))
{
     printf("this is %d",n);
}

Note::missing braces in the above example may be a due to the editor ,please be sure that braces are balanced,because i know you are going to check it for sure.......

4 comments:

  1. really amazing..........

    ReplyDelete
  2. i read this logic first time....
    so thnx a lot.....

    ReplyDelete
  3. But
    void func((int num))
    is giving compiler error...
    why it is so instead the braces are balanced here????????

    ReplyDelete
  4. ya that will be an error for sure....what happens when we call a function....
    we can take it as
    int num=20;
    where 20 is the value passed while calling the function....
    now consider the case when u put the braces around that it will look like
    (int num)=20;
    now this is not the way to initilize a variable..becaue on the left hand side there must be a lvalue and (int num) is not an lvalue because "int num" is not the lvalue...
    num is an lvalue so you can say like this
    int (num)=20;
    or int ((num))=20;
    because num is a lvalue so (num) or ((num)) is also lvalue..
    you can try that as function argument
    void func(int (((num))))
    will work fine....

    ReplyDelete

Feel free to comment......