Lesser known facts in C

Do you find this thread and it's facts intresting??

  • Yes

    Votes: 16 94.1%
  • No

    Votes: 1 5.9%

  • Total voters
    17
Status
Not open for further replies.
OP
A

adi007

Youngling
Register storage class

If we use register storage class then the variable is stored in the CPU registers.Register access is very fast.It is best used for looping variables or counters.

the syntax is
Code:
register datatype variable
Ex:
Code:
register int a=5;

Some compiler will alllow only int and char datatype while the others will allow all the datatypes.If the number of variables exceed the limit of CPU register then the variable is not stored in CPU register and is stored in memory(RAM)


The main advantage of using register storage class is the fast access of the register variable when compared to the normal variable.The difference is best seen when we declare a looping variable or counter using register storage class..
ex:

Code:
#include<stdio.h>
main()
{
register long i;
for(i=0;i<900000000;i++);
printf("Over\n");
}

Now run this program and note the delay and then modify the above program as

Code:
long i;

and note the delay.Time gap will increases as the limit increases.


Now the biggest disadvantage of register variables is they have no memory locations or addess.That means we cannot use '&' when we are working with register variables

ex:
consider the following program..
Code:
#include<stdio.h>
main()
{
register int a=8;
printf("a= %d\n",a);
scanf("%d",&a);
printf("a=%d\n",a);
}

if you compile the above program,compiler will issue an error.In order to solve this drawback you have to use a dummy variable.i.e,

Code:
#include<stdio.h>
main()
{
register int a=8;
int b;
printf("a= %d\n",a);
scanf("%d",&b);
a=b;
printf("a=%d\n",a);
}

the output of the above program is
Code:
a=8
10
a=10


coming next:extern
 
Last edited:

a_k_s_h_a_y

Dreaming
hey man .. this has become a tutorial in C rather then Lesser known facts
it should be renamed to lesser known facts and tutorial in C

Really Nice Work Keep it up !

a lesser know fact for VTU students is

>>>>>>>>> the datatype called as bool


bool act; // >> declaration

now a is bool type of datatype and act can store only 1 or 0 !! ;)
 
Last edited:

a_k_s_h_a_y

Dreaming
^^ that's cool
but ppl who study C for first time in VTU don't know that

as u know they worship that Padma Reddy Religiously
 
OP
A

adi007

Youngling
i am extremely sorry for neglecting this thread :(:(.Nearly 10 days has passed since the last unknow fact:shock::shock:I promise that this will not happen again.
Tommorow i will cover extren storage class along with some other unknown facts....
Once again sorry :(:(
Can anyone tell me how to prevent Automerged Doublepost:confused:.This is very irritating for me :mad: because i don't like posting 2 or more unknown facts in a single thread.I want to create new post for each unknow fact and this Automerged Doublepost is not allowing me to so so:mad:
 

fun2sh

Pawned!... Beyond GODLIKE
a_k_s_h_a_y said:
^^ that's cool
but ppl who study C for first time in VTU don't know that

as u know they worship that Padma Reddy Religiously
yeah that really true. every1 in my class is after padma PHIsadi :razz: no one use the good books like FEROZAN(dont know the spellin but me read from this book itself which is vtu prescribed n some online tutorials.)...even in lab they mug up the programs given by the teacher:sad:
but i know one cant learn c from a book. u hav to rack ur brain day n night to get the concepts n logic.

@adi

i think automerge double post cant be removed. u better ask a MOD.. n if it also dont help then make a another userid n post usin that id..
 
OP
A

adi007

Youngling
will explain extern storage class later

here are some more unknown symbols in TC editor ...

*farm3.static.flickr.com/2117/2086500160_cfb16f0ec4_o.jpg​

these will work for input as well but you have to use getche() not scanf()

and here is the ASCII chart...

*farm3.static.flickr.com/2392/2085714247_6abe4b8d2b_o.jpg​

Continue here
 
Last edited:

manubatham20

Broken In
Very very interesting and deep knowledge. I know few things discussed above but not all.
Try this--
main()
{
int a=10,b=20;
printf("%d %d");
}
What its print. Output 20 10
Keep up sharing.
 

a_k_s_h_a_y

Dreaming
^^ that's compiler dependent !

and when you decleare

like this

register int a;
Then you make a request to the compiler to store a in the register !!
the compiler may accept the request or not !!
 
OP
A

adi007

Youngling
Extern storage class
Last in the important storage classes,extern storage class is rather intresting and confusing as well:confused:

The storage class extern indicates that the variable has been declared some where else in the program(in case of linux/unix other file as well)either before or after the function in which it has been specified.It indicates the compiler not to declare the new variable but instead make the already declared variable ready to use within that function..

usage:
Code:
extern datatype variable

ex:
Code:
extern int a;

lets consider an example program
Code:
#include<stdio.h>
main()
{
void fun(void);
extern int a;
printf("Value of a in main is %d\n",a);
a++;
fun();
}

void fun(void)
{
extren int a;
printf("In fun the value of a is %d\n",a);
}
int a=6;

The output of the above program will be

Code:
Value of a in main is 6
In fun the value of a is 7

now,lets understand the program..As u can see the variable 'a' cannot be used by main() and fun() functions as it has been declared after these functions..
In order to use that variable we must use extern storage class..
it is important to note that extern will not declare a new variable but will use the existing already declared variable.
This is proved from the fact that the value of a has changed in the fun() function because we have used a++ in main() before it.That means the change in value of 'a' will reflect in the whole program wherever that value is used..


Now consider this program..
Code:
#include<stdio.h>
main()
{
void fun(void);
extern int a;
printf("Value of a in main is %d\n",a);
a++;
fun();
}

void fun(void)
{
int a=6;
printf("In fun the value of a is %d\n",a);
}
now what do u think the output is...
The program will not compile at all..
Here we have note an important thing

extren can be employed or used only to those variables which are declared outside all the functions in the given program..


In order to avoid the usage of extren one should declare the variable before main() then it becomes global and it an be used in all the functions..
i.e,
the above program can be written as
Code:
#include<stdio.h>
int a=6;
main()
{
void fun(void);
printf("Value of a in main is %d\n",a);
a++;
fun();
}

void fun(void)
{
printf("In fun the value of a is %d\n",a);
}
 
Last edited:
Status
Not open for further replies.
Top Bottom