# Lesser known facts in C



## adi007 (Oct 19, 2007)

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:mindcracking C puzzles.Updated atleast once in a week_
> 
> _2.C in linux/unix:Bored of using TC,switch to linux.All about running C programs in linux/unix_
> ...





> *Index*
> _1.Editset[#1]_
> _2.scanf() return type [#6]_
> _3.enum data type[#8]_
> ...



*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

This was asked in puzzle 1 of thread Solve the given C puzzle [*www.thinkdigit.com/forum/showthread.php?t=70697]


----------



## [xubz] (Oct 19, 2007)

Bah! C Supports RegEx too eh? Interesting (didn't quite know )

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 (Oct 19, 2007)

^^ Using gets is not encouraged. You have to flush the stdin everytime.


----------



## piyush gupta (Oct 19, 2007)

yus


----------



## adi007 (Oct 22, 2007)

no contibutions


----------



## adi007 (Oct 25, 2007)

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 *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 (Oct 25, 2007)

^^ Buddy keep posting u r doing a good job  . I appreciate ur effort


----------



## adi007 (Oct 26, 2007)

^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,friday,saturday}
```

Read it my site aka blog


This was asked by me in puzzle3



> Here is a simple C program
> 
> ```
> #include<stdio.h>
> ...



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 
If you know any specify it here....


----------



## ayush_chh (Oct 27, 2007)

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 (Oct 30, 2007)

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


----------



## ayush_chh (Oct 31, 2007)

^^ gr8 buddy .........thanks for sharing.


----------



## adi007 (Nov 5, 2007)

^^thanks
will post another unknown fact tommorow 
But the only thing i feel sad is there is less amount of responses and contributions  and defenitely no ratings and poll partcipation


----------



## Vivek788 (Nov 5, 2007)

oh boy...never knew...


----------



## Quiz_Master (Nov 5, 2007)

Hmmm.. Nice stuff... 
Keep them coming....
Didn't knew most of them.


----------



## adi007 (Nov 6, 2007)

Thanks for all ur responses 

*floor and ceil*

floor and ceil functions comes under math.h header file.

continue here


----------



## Batistabomb (Nov 6, 2007)

good job buddy


----------



## adi007 (Nov 6, 2007)

*Another unknown fact*

Dynamic output

continue here


----------



## nightcrawler (Nov 6, 2007)

Nice work buddy. Keep them coming


----------



## adi007 (Nov 7, 2007)

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???

^^Did anyone tried this???


> Important announcements:
> if you google _lesser known facts in c_,then this thread will be the first result....
> But if i select pages from india then this will be not the first result.I wonder why???


----------



## ChaiTan3 (Nov 7, 2007)

^^^a smiley appears. i'm using TC in dosbox


----------



## adi007 (Nov 7, 2007)

ChaiTan3 said:
			
		

> ^^^a smiley appears. i'm using TC in dosbox


^^yes

Consider the following C program



> #include<stdio.h>
> #include<conio.h>
> void main()
> {
> ...



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 (Nov 9, 2007)

its really good man
didnt had the slightest of idea about this 
thanks a lot for this


----------



## adi007 (Nov 9, 2007)

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 

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 (Nov 9, 2007)

adi007, use 
	
	



```
always, preserves text and formatting just like it should be.

P.s. Also use [noparse] if necessary.
```


----------



## adi007 (Nov 9, 2007)

QwertyManiac said:
			
		

> adi007, use
> 
> 
> 
> ...


----------



## QwertyManiac (Nov 9, 2007)

Doesn't Parse any vB/etc code:

[noparse][noparse]*  *[/noparse][/noparse]

Will appear as:

[noparse]*  *[/noparse]


----------



## adi007 (Nov 9, 2007)

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
> *www.thinkdigit.com/forum/showpost.php?p=654023&postcount=20
> This is for C in windows
> _


Continue here


----------



## fun2sh (Nov 10, 2007)

adi007 said:
			
		

> 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*
> ...



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.141[B]6[/B]
```

not like wat adi said


			
				adi007 said:
			
		

> ```
> Value of pi=3.1415
> ```
> its wrong



here for decimal values the output gets rounded to the width value mentioned.


----------



## adi007 (Nov 12, 2007)

Regarding, const datatype ,i think your idea is wrong.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.

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



			
				adi007 said:
			
		

> 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._
> 
> ...



^^
By the way,has anyone done this homework.Give me the answers along with steps.


> ```
> #include<stdio.h>
> void main()
> {
> ...


----------



## fun2sh (Nov 12, 2007)

adi007 said:
			
		

> Regarding, const datatype ,i think your idea is wrong.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.
> 
> The program is 100 %correct as far as linux is concerned.Have just executed it in Browsing Centre of my college.
> 
> ...


----------



## adi007 (Nov 12, 2007)

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


			
				fun2sh said:
			
		

> ```
> #include<stdio.h>
> void main()
> {
> ...




```
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 (Nov 12, 2007)

adi007 said:
			
		

> Regarding, const datatype ,i think your idea is wrong.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.
> 
> The program is 100 %correct as far as linux is concerned.Have just executed it in Browsing Centre of my college.
> 
> ...


----------



## adi007 (Nov 13, 2007)

^^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


----------



## fun2sh (Nov 15, 2007)

i hav turbo c++ version 3


----------



## adi007 (Nov 15, 2007)

^^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 (Nov 16, 2007)

adi007 said:
			
		

> Poll added!
> 
> I request all members to Participate in the pole and Rate this thread
> 
> ...



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 (Nov 16, 2007)

^^ 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 (Nov 17, 2007)

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 (Nov 18, 2007)

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.
> 
> ...


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,
> 
> ...



_coming next:register and extern storage class_


----------



## brokenheart (Nov 20, 2007)

thx...4 sharing...dude...


----------



## adi007 (Nov 22, 2007)

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 (Dec 2, 2007)

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 (Dec 2, 2007)

hey man wat dow u mean by


			
				a_k_s_h_a_y said:
			
		

> 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 (Dec 2, 2007)

^^ 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 (Dec 3, 2007)

i am extremely sorry for neglecting this thread .Nearly 10 days has passed since the last unknow factI 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.This is very irritating for me  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


----------



## fun2sh (Dec 5, 2007)

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  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
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 (Dec 5, 2007)

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


----------



## manubatham20 (Dec 9, 2007)

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 (Dec 9, 2007)

^^ 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 (Dec 9, 2007)

^^ Actually i think that depends on whether the register is already full with data or not.


----------



## adi007 (Dec 17, 2007)

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



> 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
> 
> ...


----------

