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
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()
{
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
 
Last edited:

thinker

Think Well Before You Do
its really good man
didnt had the slightest of idea about this
thanks a lot for this
 
OP
A

adi007

Youngling
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.
Code:
#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......
 
Last edited:

QwertyManiac

Commander in Chief
adi007, use
Code:
 always, preserves text and formatting just like it should be.

P.s. Also use [noparse] if necessary.
 
Last edited:
OP
A

adi007

Youngling
QwertyManiac said:
adi007, use
Code:
 always, preserves text and formatting just like it should be.
[/QUOTE] 
I used [quote] because you will not get :) in [code].
And if i just specify [code]:)
then it wouldn't be nice.
QwertyManiac said:
P.s. Also use [noparse] if necessary.
^^ what's it's effect??:confused::confused:
 

QwertyManiac

Commander in Chief
Doesn't Parse any vB/etc code:

[noparse][noparse] :) [/noparse][/noparse]

Will appear as:

[noparse] :) [/noparse]
 
OP
A

adi007

Youngling
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
 
Last edited:

fun2sh

Pawned!... Beyond GODLIKE
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
This lesser known datatype is used to declare constants.The syntax is
const type name=value;
ex:
Code:
const int a=10;
is equivalent to
Code:
#define a 10
now consider this program
Code:
#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
Code:
#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
Code:
#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:
Code:
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
Code:
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

Code:
#include<stdio.h>
int main()
{
long double a=3.14159265;
printf("Value of pi=%.4Lf",a);
return(1);
}

the output will be

Code:
Value of pi=3.141[B]6[/B]

not like wat adi said
adi007 said:
Code:
Value of pi=3.1415
its wrong

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

adi007

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

Code:
#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

Code:
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

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.
Code:
#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.
Code:
#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??
 
Last edited:

fun2sh

Pawned!... Beyond GODLIKE
adi007 said:
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.

Code:
#include<stdio.h>
void main()
{
[B][SIZE="4"]const int a;[/SIZE][/B]
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

Code:
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
Code:
const int a;

u hav not initialized it.

secondly after initializin it as in this code

Code:
#include<stdio.h>
void main()
{
[B][SIZE="4"]const int a=9;[/SIZE][/B]
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

Code:
9
Enter new value
7
The value is now [SIZE="4"][B]9[/B][/SIZE]

i checked it 10times n the value of 'a' is not changin.
i use TC
 
OP
A

adi007

Youngling
In a hurry i made a typing mistake,now corrected.The program is
Code:
#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
Code:
10
Enter new value
25
The value is now 25

Your same program executed in linux
fun2sh said:
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);
}
Code:
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..
 
Last edited:

fun2sh

Pawned!... Beyond GODLIKE
adi007 said:
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.

Code:
#include<stdio.h>
void main()
{
[B][SIZE="4"]const int a;[/SIZE][/B]
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

Code:
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
Code:
const int a;

u hav not initialized it.

secondly after initializin it as in this code

Code:
#include<stdio.h>
void main()
{
[B][SIZE="4"]const int a=9;[/SIZE][/B]
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

Code:
9
Enter new value
7
The value is now [SIZE="4"][B]9[/B][/SIZE]

i checked it 10times n the value of 'a' is not changin.
i use TC
 
OP
A

adi007

Youngling
^^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
 
Last edited:
OP
A

adi007

Youngling
^^fun2sh, i also use the same..
It's working fine for me.Have no idea why it's not working for you....
 
adi007 said:
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:
Code:
const int a=10;
is equivalent to
Code:
#define a 10
now consider this program
Code:
#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
Code:
#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
Code:
#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:
Code:
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
Code:
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.
 
OP
A

adi007

Youngling
^^ You are right..

Your above facts is illustrated by this program
Code:
#include<stdio.h>
#define  a 10
main()
{
printf("The address of constant 'a' is %u\n",&a);
}
will lead to an error...
while
Code:
#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.
 
Last edited:
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....

Code:
#include<stdio.h>
#include<stdlib.h>
void main()
{
  int i;
  for (i=0;i<=5;printf("Sumit"))
  {
    i++;
  }
  system("pause");
}
 
OP
A

adi007

Youngling
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
Code:
auto datatype variable name
All the variables specified without storage class is regarded as auto storage class variable.
i.e,
Code:
auto int a;
is equivalent to
Code:
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:
Code:
static datatype variable name
ex:
Code:
static int a;
To explain the concept of static variables consider the following program
Code:
#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

Code:
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,

Code:
static int a=5;

is exceuted by the compiler only once.


Note:
All static variables are automatically initialized to 0
i.e.
Code:
static int a;
printf("%d",a);

will give the output

Code:
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

Code:
#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,
Code:
#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

Code:
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
 
Last edited:
Status
Not open for further replies.
Top