View Full Version : Lesser known facts in C
adi007
19-10-2007, 09:16 AM
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
This thread is dedicated to lesser known facts in C
Don't forget to visit these other threads started by me
1.Solve the given C puzzle (http://www.thinkdigit.com/forum/showthread.php?t=70697):mindcracking C puzzles.Updated atleast once in a week
2.C in linux/unix (http://www.thinkdigit.com/forum/showthread.php?t=71889):Bored of using TC,switch to linux.All about running C programs in linux/unix
3.Presenmaker 1.0 (http://www.thinkdigit.com/forum/showthread.php?t=74832)Presenmaker stands for presentation maker. It's a free software developed by me which can be used to create interactive agent animations in seconds. By using Presenmaker you can convert a lengthy text file into agent animation. It is very useful to create interactive presentations, tutorials, to read lengthy lessons etc
Index
1.Editset[#1]
2.scanf() return type [#6]
3.enum data type[#8]
4.const data type[#10]
5.floor and ceil[#15]
6.dynamic output[#17]
7.smiley in C[#21]
8.Garbage value in case of dataoverflow[#23]
9.Saving the output to a text file[#27]
10.atol(),atof() and atol()[#33]
11.storage class-1[#39]
12.storage class-2[#41]
13.ASCII chart and some shortcuts in TC editor[#47]
14.storage class-3[#51]
EditSet:
This will be great useful in strings.You can specify the characters you want to include or exclude in C.This will work with scanf.
Read it my site aka blog (http://blog.aditech.info/2008/02/16/edit-set/)
This was asked in puzzle 1 of thread Solve the given C puzzle [http://www.thinkdigit.com/forum/showthread.php?t=70697] (http://www.thinkdigit.com/forum/showthread.php?t=70697%5D)
[xubz]
19-10-2007, 10:10 AM
Bah! C Supports RegEx too eh? Interesting (didn't quite know :o)
Instead of scanf("%[^\n]s", a);, One _can_ use gets(a);
Interesting Post Anyway, Thanks :)
(Edit: Does anyone know of a RegEx based text replace function code in C? like in preg_replace in PHP?)
aditya.shevade
19-10-2007, 10:44 AM
^^ Using gets is not encouraged. You have to flush the stdin everytime.
piyush gupta
19-10-2007, 04:04 PM
yus
adi007
22-10-2007, 10:32 AM
no contibutions:(
adi007
25-10-2007, 11:02 AM
Here is the second lesser know fact
scanf() return type:
scanf() will return number of variables it succesfully stored new values.i.e,
a=scanf("%d%d",&b,&c);
where a,b,c are integer variables
if we input 12 b
then the value of a will be 1
if we input 12 1
then the value of a will be 2
that means it can be used to verify whether the user has given the values according to the specified data type or not.
here is the problem that was asked in 2nd C puzzle
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
scanf("%c%b%c",&a,&b,&c);
getch();
}
This is an program which accepts a character followed by 2 integer values.
NOW ALL I WANT TO DO IN THIS PROGRAM IS TO CHECK WHETHER THE USER HAS ENTERED CORRECT INPUT(FIRST CHARACTER AND FOLLOWED BY THE 2 INTEGER VALUES.
It should display "That's Good" if the user has inputted in correct sequence else it should display "That's Bad".
Here are some sample ouput's
Output:
Enter a character and 2 integer values
a 23 45.96
That's good
^^ decimal values are rounded off.Hence it is valid input
Output:
Enter a character and 2 integer values
2 23 45.96
That's good
^^'2' is also a character
Output:
Enter a character and 2 integer values
2 ad 45.96
That's bad
^^ ad is not a decimal value.
The following are the Rules:
1.The keyword if or operator '?' should come only once in a program
2.No logical operator's are allowed (that means && || ! should not be used)
3.No new variabels must be used
4.No header file other than the existing one should be used
Here is the solution
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("\n thats good");
else
printf("\n thats bad");
getch();
}
Find more of the C puzzles at http://www.thinkdigit.com/forum/showthread.php?t=70697
no contributions:(:(
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("\n thats good\n");
else
printf("\n thats bad\n");
printf("The values are %c %d %d\n",a,b,c)
getch();
}
the above program is going to work correctly.I earlier stated that the output will be some garbage values but it is not so.
Sorry for the mistake:(
I just wonder how come no one recognised this mistake.That means no one is trying the facts specified here :(:(:(
saurabh kakkar
25-10-2007, 11:38 AM
^^ Buddy keep posting u r doing a good job :) . I appreciate ur effort :)
adi007
26-10-2007, 12:37 PM
^thanks:):)
Looks like only i have to post ..
Ok,here is a very unknown fact about C
enum data type
it means enumerated type.
It's a user defined datatype.It's syntax is
enum name{value1,value2,.....valuen);
ex:
enum week{sunday,monday,tuesday,wednesday,thursday,frid ay,saturday}
Read it my site aka blog (http://blog.aditech.info/2008/02/16/enum-data-type/)
This was asked by me in puzzle3
Here is a simple C program
#include<stdio.h>
#include<conio.h>
void main()
{
/*Complete the declartions*/
clrscr();
a=sunday;
b=monday;
c=tuesday;
d=wednesday;
e=thursday;
f=friday;
g=saturday;
printf("%d/n%d/n%d/n%d/n%d/n%d/n%d/n",a,b,c,d,e,f,g);
getch();
}
NOW ALL I WANT YOU TO DO IN THIS PROGRAM IS TO COMPLETE DECLARATIONS SO THAT IT MACTHES THE OUTPUT.
Output:
1
2
3
4
5
6
7
The following are the Rules:
1.sunday,monday,tuesday,wednesday,thursday,friday, saturday etc are not varibles nor constants.Means
int sunday=1;
or
#define sunday 1
are not allowed
2.a,b,c,d,e,f,g are not of int datatype
int a;
^^ not allowed
And the solution to this is
#include<stdio.h>
#include<conio.h>
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
enum days a,b,c,d,e,f,g;
a=sunday,
b=monday,
c=tuesday,
d=wednesday,
e=thursday,
f=friday,
g=saturday;
clrscr();
printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f,g);
getch();
}
Note:!:
if i just specified
enum days{
sunday,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
then it would have generated the output
0
1
2
3
4
5
6
So it is necessary to assign 1 to sunday.
Now what's it's use:
I have never seen any exclusive use of enum data type.Otherwise it would have been know fact to everyone :D:D
If you know any specify it here....
ayush_chh
27-10-2007, 07:50 PM
may be i can give one...........don't know if all of you know this already.
There is only one special character which you can use in a identifier name.
that special character is _ (underscore)
so you give give a identifier name as var_1
or _var
or (the most interesting part) just _ (underscore)
so you can create identifiers as
int _;
int __;
int ___;
adi007
30-10-2007, 11:18 AM
Poll added!
I request all members to Participate in the pole and Rate this thread
Here is another unknown fact
We all know that we can declare a constant by using #define but do you know that there is still other method to do so.
const data type
This lesser known datatype is used to declare constants.The syntax is
const type name=value;
ex:
const int a=10;
Continue here (http://blog.aditech.info/2008/02/20/const-data-type/)
ayush_chh
31-10-2007, 11:47 PM
^^ gr8 buddy .........thanks for sharing.
adi007
05-11-2007, 10:39 AM
^^thanks:D:D
will post another unknown fact tommorow :D:D
But the only thing i feel sad is there is less amount of responses and contributions :(:( and defenitely no ratings and poll partcipation :(:(
Vivek788
05-11-2007, 05:31 PM
oh boy...never knew...
Quiz_Master
05-11-2007, 05:33 PM
Hmmm.. Nice stuff...
Keep them coming....
Didn't knew most of them.
adi007
06-11-2007, 12:14 PM
Thanks for all ur responses :D:D:D
floor and ceil
floor and ceil functions comes under math.h header file.
continue here (http://blog.aditech.info/2008/02/20/floor-and-ceil/)
Batistabomb
06-11-2007, 12:31 PM
good job buddy
adi007
06-11-2007, 12:39 PM
Another unknown fact
Dynamic output
continue here (http://blog.aditech.info/2008/02/20/dynamic-output-in-c/)
nightcrawler
06-11-2007, 01:43 PM
Nice work buddy. Keep them coming :)
adi007
07-11-2007, 12:29 PM
I am not sure whether this is an unknown fact or not.
This will work in Turbo or windows
In the TC editor press <CTRL><B>
Tell me what happens???:wink::wink:
^^Did anyone tried this???
:!::!:Important announcements:
if you google lesser known facts in c,then this thread will be the first result....:D:D:D
But if i select pages from india then this will be not the first result.I wonder why???:confused:
ChaiTan3
07-11-2007, 12:32 PM
^^^a smiley appears. i'm using TC in dosbox
adi007
07-11-2007, 12:40 PM
^^^a smiley appears. i'm using TC in dosbox
^^yes
Consider the following C program
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr();
printf("Enter smiley\n");
a=getche();
if(a==':)' ) /*use ctrl b to insert the smiley*/
printf("\n You did what i said\n");
else
printf("\n You should have entered :) not %c\n",a);
getch();
}
the output will be
Enter smiley
a
You should have entered :) not a
Enter smiley
:)
You did what i said
^^
During runtime also you have to press CTRL B to enter smiley
thinker
09-11-2007, 01:03 AM
its really good man
didnt had the slightest of idea about this
thanks a lot for this
adi007
09-11-2007, 08:43 AM
Garbage value in case of data overflow
I did some intense R&D for garbage values and found these facts.After understanding these concepts u can tell what will be the garbage value to be stored in a variable in case of dataoverflow.
Continue here (http://blog.aditech.info/2008/02/20/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow/)
Home work section
Now a assignment for you.If you have understood these concepts correctly then tell me without running the program what will be the output of this program.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
long int b;
clrscr();
a=72683;
b=-5267367896;
printf("%d\t%ld\n",a,b);
getch();
}
Eagerly waiting for your responses......
QwertyManiac
09-11-2007, 09:11 AM
adi007, use [code] always, preserves text and formatting just like it should be.
P.s. Also use [noparse] if necessary.
adi007
09-11-2007, 10:10 AM
adi007, use always, preserves text and formatting just like it should be.
I used because you will not get :) in [code].
And if i just specify [code]:) then it wouldn't be nice.
[QUOTE=QwertyManiac]
P.s. Also use [noparse] if necessary.
^^ what's it's effect??:confused::confused:
QwertyManiac
09-11-2007, 11:04 AM
Doesn't Parse any vB/etc code:
:)
Will appear as:
:)
adi007
09-11-2007, 03:41 PM
Saving the output to a text file
If you use linux,then how to save the output to a text file is already posted in my C in linux/unix thread
http://www.thinkdigit.com/forum/showpost.php?p=654023&postcount=20
This is for C in windows
Continue here (http://blog.aditech.info/2008/02/20/saving-the-output-in-a-text-file/)
fun2sh
10-11-2007, 10:07 PM
Here is another unknow fact
We all know that we can declare a constant by using #define but do you know that there is still other method to do so.
const data type
This lesser known datatype is used to declare constants.The syntax is
const type name=value;
ex:
const int a=10;
is equivalent to
#define a 10
now consider this program
#include<stdio.h>
void main()
{
const int a=10;
printf("%d",a);
a=14;
printf("%d",a);
}
what do you think the ouput will be???
First off all the program will not compile itself because it is not allowed to change the value of 'a' statically(within the program)
now change the program as
#include<stdio.h>
void main()
{
const int a=10;
printf("%d",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d",a);
}
will change the value of a
That means we can change the value of the constant declared by using const in run time mode(Dynamically) but not Statically(within the program).
now consider this program
#include<stdio.h>
#define a 10
void main()
{
printf("%d",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d",a);
}
what do you think the output will be???
the program will not be compiled,that means that we can access the value of 'a' but not able to use scanf.
The use of const is not restricted to int only.
ex:
const char a='y';/*char*/
const char a[10]={"adithya"};/*String*/
const float a=4.67;/*float*/
const long double a=9.78843434;/*long double*/
now you can say that all these can be done using #define also.
But there are several things for which we cannot use #define
The excellent example is array.
Can anyone tell me how to declare an static constant array using #define???
But i can tell you how to do so using const
const float a[10]={1,3,5};
sorry buddy, but "const" data type mean variable has to be constan n its value wont change either durin execution or tru program code.
i checked it again in turbo c compiler n usin "scanf" for contant data type is not changin the value
also wen we use
#include<stdio.h>
int main()
{
long double a=3.14159265;
printf("Value of pi=%.4Lf",a);
return(1);
}
the output will be
Value of pi=3.1416
not like wat adi said
Value of pi=3.1415
its wrong
here for decimal values the output gets rounded to the width value mentioned.
adi007
12-11-2007, 01:01 PM
Regarding, const datatype ,i think your idea is wrong:confused:.We can change the const datatype variable value during program execution.I have tested it on linux but i don't know in TC.I will cross check but i think it will work in TC also because i remember that i have executed it on TC also.:confused::confused:
The program is 100 %correct as far as linux is concerned.Have just executed it in Browsing Centre of my college.
#include<stdio.h>
void main()
{
const int a=10;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
In linux(Red hat),the output of the above C program will be
bash-2.05b$ ./a.out
10
Enter new value
25
The value is now 25
bash-2.05b$
next regarding the dynamic output,i commited a typing mistake.The value is 3.1416 not 3.1415 as i said earlier.
Sorry for the mistake:(:(:(.
Post has been modified.And also their is another typing mistake for precesion 6.The value is 3.141593 not 3.141592.
Thanks fun2sh for detecting mistakes in my post:D:D
Home work section
Now a assignment for you.If you have understood these concepts correctly then tell me without running the program what will be the output of this program.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
long int b;
clrscr();
a=72683;
b=-5267367896;
printf("%d\t%ld\n",a,b);
getch();
}
^^
By the way,has anyone done this homework.Give me the answers along with steps.
#include<stdio.h>
void main()
{
const int a=10;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
^^:!:fun2sh,i have cross checked this program regarding const datatype in windows and it works and it has changed the value of const datatype varaiable.I have no idea why it's not working for you.
Can you give me the error message??
fun2sh
12-11-2007, 04:27 PM
[QUOTE=adi007]Regarding, const datatype ,i think your idea is wrong:confused:.We can change the const datatype variable value during program execution.I have tested it on linux but i don't know in TC.I will cross check but i think it will work in TC also because i remember that i have executed it on TC also.:confused::confused:
The program is 100 %correct as far as linux is concerned.Have just executed it in Browsing Centre of my college.
#include<stdio.h>
void main()
{
const int a;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
In linux(Red hat),the output of the above C program will be
bash-2.05b$ ./a.out
10
Enter new value
25
The value is now 25
bash-2.05b$
hey i again checked ur program by copy pastin ur code n first mistake u hav done is
const int a;
u hav not initialized it.
secondly after initializin it as in this code
#include<stdio.h>
void main()
{
const int a=9;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
the outout is as follows
9
Enter new value
7
The value is now 9
i checked it 10times n the value of 'a' is not changin.
i use TC
adi007
12-11-2007, 04:41 PM
In a hurry i made a typing mistake,now corrected.The program is
#include<stdio.h>
void main()
{
const int a=10;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
and the output (tested in both linux and windows ) is
10
Enter new value
25
The value is now 25
Your same program executed in linux
#include<stdio.h>
void main()
{
const int a=9;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
bash-2.05b$ vi fun2sh.c
bash-2.05b$ cc fun2sh.c
bash-2.05b$ ./a.out
9
Enter new value
7
The value is now 7
bash-2.05b$
Could you specify your TC version..
fun2sh
12-11-2007, 04:52 PM
[QUOTE=adi007]Regarding, const datatype ,i think your idea is wrong:confused:.We can change the const datatype variable value during program execution.I have tested it on linux but i don't know in TC.I will cross check but i think it will work in TC also because i remember that i have executed it on TC also.:confused::confused:
The program is 100 %correct as far as linux is concerned.Have just executed it in Browsing Centre of my college.
#include<stdio.h>
void main()
{
const int a;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
In linux(Red hat),the output of the above C program will be
bash-2.05b$ ./a.out
10
Enter new value
25
The value is now 25
bash-2.05b$
hey i again checked ur program by copy pastin ur code n first mistake u hav done is
const int a;
u hav not initialized it.
secondly after initializin it as in this code
#include<stdio.h>
void main()
{
const int a=9;
printf("%d\n",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d\n",a);
}
the outout is as follows
9
Enter new value
7
The value is now 9
i checked it 10times n the value of 'a' is not changin.
i use TC
adi007
13-11-2007, 11:29 AM
^^specify your TC version.
Another unknown fact
atoi atol atof
These functions are available under stdlib.h .These are very unknown functions.This is evident from the fact that no one was able to solve puzzle 5.Lets see what these do..
Continue here (http://blog.aditech.info/2008/02/20/atoiatol-and-atof-functions/)
fun2sh
15-11-2007, 04:57 PM
i hav turbo c++ version 3
adi007
15-11-2007, 05:03 PM
^^fun2sh, i also use the same..
It's working fine for me.Have no idea why it's not working for you....
Krazy_About_Technology
16-11-2007, 01:49 AM
Poll added!
I request all members to Participate in the pole and Rate this thread
Here is another unknow fact
We all know that we can declare a constant by using #define but do you know that there is still other method to do so.
const data type
This lesser known datatype is used to declare constants.The syntax is
const type name=value;
ex:
const int a=10;
is equivalent to
#define a 10
now consider this program
#include<stdio.h>
void main()
{
const int a=10;
printf("%d",a);
a=14;
printf("%d",a);
}
what do you think the ouput will be???
First off all the program will not compile itself because it is not allowed to change the value of 'a' statically(within the program)
now change the program as
#include<stdio.h>
void main()
{
const int a=10;
printf("%d",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d",a);
}
will change the value of a
That means we can change the value of the constant declared by using const in run time mode(Dynamically) but not Statically(within the program).
now consider this program
#include<stdio.h>
#define a 10
void main()
{
printf("%d",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d",a);
}
what do you think the output will be???
the program will not be compiled,that means that we can access the value of 'a' but not able to use scanf.
The use of const is not restricted to int only.
ex:
const char a='y';/*char*/
const char a[10]={"adithya"};/*String*/
const float a=4.67;/*float*/
const long double a=9.78843434;/*long double*/
now you can say that all these can be done using #define also.
But there are several things for which we cannot use #define
The excellent example is array.
Can anyone tell me how to declare an static constant array using #define???
But i can tell you how to do so using const
const float a[10]={1,3,5};
I would like to add that the reason for the above is that when we define a constant using const, an actual storage location in the RAM gets allocated to our program where the value of that constant is stored. Thats why you can scanf it. But when we define a #define constant, it just creates a symbolic constant and compiler replaces the name of that constant in the program everywhere. So actually no memory location is allocated and hence cant be passed to scanf.
adi007
16-11-2007, 12:46 PM
^^ You are right..
Your above facts is illustrated by this program
#include<stdio.h>
#define a 10
main()
{
printf("The address of constant 'a' is %u\n",&a);
}
will lead to an error...
while
#include<stdio.h>
main()
{
const int a=10;
printf("The address of constant 'a' is %u\n",&a);
}
will display the memory address of a.
This explains that memory location is given to const variable while if you use #define compiler just replaces the name of that constant in the program everywhere.
Krazy_About_Technology
17-11-2007, 10:12 PM
A lesser known fact about the for loop is that the part of for loop where we almost always do increment or decement (++ or --) is simply a statement and it can take any keyword or function call or whatever for that matter. for example the following code works perfectly fine....
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i;
for (i=0;i<=5;printf("Sumit"))
{
i++;
}
system("pause");
}
adi007
18-11-2007, 10:54 AM
Storage classes
There are basically 4 storage classes supported in C.They are
1.Static
2.Auto
3.Register
4.Extern
Now, let's consider auto storage class
The variables declared with storage class as auto are created at the starting of the function and destroyed at the end of the function.The are often reffered as local variables because it's scope lies within the function itself
The syntax is
auto datatype variable name
All the variables specified without storage class is regarded as auto storage class variable.
i.e,
auto int a;
is equivalent to
int a;
Since all normal variables declared without storage class are of auto storage class type,there is no need to give example and further explain.
Static storage class
The variables declared with storage class as static are created at the starting of the function and are not destroyed at the end of the function.That means compiler declares the variable only once and it will remain un destroyed until the completion of the whole program itself
The static variables are declared according to the syntax:
static datatype variable name
ex:
static int a;
To explain the concept of static variables consider the following program
#include <stdio.h>
main()
{
void test(void);
printf("1 st call\n");
test();
printf("2 nd call\n");
test();
printf("3 rd call\n");
test();
printf("Bye\n");
}
void test(void)
{
static int a=5;
a++;
printf("%d\n",a);
}
Now, what do you think the output is ....
The output is not 6 6 6
But the output is
1 st call
6
2 nd call
7
3 rd call
8
Bye
See, the compiler will declare the static variables only once.i.e,
static int a=5;
is exceuted by the compiler only once.
Note:
All static variables are automatically initialized to 0
i.e.
static int a;
printf("%d",a);
will give the output
0
In case of arrays also all elements are initialized to 0.
In case of strings all elements are initialized to null
That means there is no need to use '\0' to indicate the end of the string if we use static storage class on strings
The use of static storage class can be avoided by using global variables
The variables declared before main() are called global variables.It's scope lies on all the functions in the program.
i.e, the above example program can be written as
#include <stdio.h>
int a=5;
main()
{
void test(void);
printf("1 st call\n");
test();
printf("2 nd call\n");
test();
printf("3 rd call\n");
test();
printf("Bye\n");
}
void test(void)
{
a++;
printf("%d\n",a);
}
Conflict between global and local variables
consider this program,
#include <stdio.h>
int a=5;
main()
{
int a=7;
printf("The value of a is %d\n",a);
printf("Bye\n");
}
What do you think the output is??.Whether it is 5 or 7.The output of the program wil be
The value of a is 7
Bye
If the local variable has the same name and type as global,then local variable will be considered.
coming next:register and extern storage class
brokenheart
20-11-2007, 02:27 PM
thx...4 sharing...dude...
adi007
22-11-2007, 11:26 AM
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
register datatype variable
Ex:
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:
#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
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..
#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,
#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
a=8
10
a=10
coming next:extern
a_k_s_h_a_y
02-12-2007, 05:31 PM
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 !! ;)
fun2sh
02-12-2007, 07:00 PM
hey man wat dow u mean by
a lesser know fact for VTU students is
i m in vtu n i know it since class 8.
a_k_s_h_a_y
02-12-2007, 07:05 PM
^^ 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
adi007
03-12-2007, 10:46 AM
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
05-12-2007, 11:29 AM
^^ 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..
adi007
05-12-2007, 12:12 PM
will explain extern storage class later
here are some more unknown symbols in TC editor ...
http://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...
http://farm3.static.flickr.com/2392/2085714247_6abe4b8d2b_o.jpg
Continue here (http://blog.aditech.info/2008/02/20/ascii-chart/)
manubatham20
09-12-2007, 12:10 AM
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
09-12-2007, 05:27 PM
^^ 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 !!
Pathik
09-12-2007, 06:10 PM
^^ Actually i think that depends on whether the register is already full with data or not.
adi007
17-12-2007, 08:51 AM
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:
extern datatype variable
ex:
extern int a;
lets consider an example program
#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
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..
#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
#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);
}
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.