ASCII Chart

ASCII chart

After seeing this chart if you want to print say square root symbol all you have to do is specify the ASCII value for %c in printf
i.e to print square root..

printf(“%c Hi! %c”,251,251);

and to print heart symbol

printf(“%c Hi! %c”,3,3);


atoi,atol and atof functions

These functions are available under stdlib.h .These are very unknown functions.Lets see what these do..

atol()
atol returns the value in terms of long datatype of the string containing numbers.the syntax is

atol(stringvariable)

ex: consider the following program
(more…)


Saving the output in a text file

consider the following c program

#include<stdio.h>
#include<conio.h>

main()
{
int i,n;
clrscr();
printf(“Enter limit\n”);
scanf(“%d”,&n);
printf(“I will print %d times\n”,n);
for(i=0;i<n;i++)
printf(“I luv C\n”);
}

the output will be
(more…)


How C Compiler decides the Garbage values in case of data overflow

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.

Here are some of the basic facts required to proceed

In case of windows(TC),

The space allocated for a variable of int datatype is 16 bits.

That means the maximum negative value that can be stored by the variable of int datatype is 2^15=-32768.

You may ask why 2^15 not 2^16.This is because one bit is reserved for sign(+ or -)

simlarly the maximum +ve value that a variable of int datatype can store is 2^15-1=32767.Hang on!!

Why -1.Because 0 is also counted in case of +ve values.

Now lets come to long datatype.
The space allocated for a variable of long datatype is 32 bits.
Maximum negative value= 2^31=-2147483648
Maximum +ve value= 2^31-1=2147483647

In case of linux(32 bit),

The space allocated for a variable of int datatype is 32 bits.
Maximum negative value= 2^31=-2147483648
Maximum +ve value= 2^31-1=2147483647

Now lets come to long datatype.
The space allocated for a variable of long datatype is 64 bits.
Maximum negative value= 2^63=-9.22337e+18
Maximum +ve value= (2^63)-1=9.22337e+18

Since long in linux can store a huge amount of value.let’s not consider it.
Let’s take int and long in case of windows and only long in case of linux

That’s basic stuffs
Now, consider this program

 

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
long int b;
clrscr();
a=45000;
b=4000000000;
printf(“%d\t%ld\n”,a,b);
getch();
}

Since both variables a and b are assigned a value greater than their limits,when you run this program some garbage value will be the output.But, the question is how c decides the garbage value??Can we predict the garbage value without running the program.

Sure,We can.C compiler calculates the garbage value by using the formula

garbage value=overflow value-reference value.

 

Overflow value is nothing but the value stored in the variable.Now what’s is the reference value??
reference value is the value such that it is greater than the overflow value(neglecting the sign)obtained by the multiplication of the basic reference value by 2 until the value is greater than the overflow value(neglecting the sign) The sign of the reference value is same as that of overflow value Basic reference value is 2^n where n represents the number of bits allocated.

That means Basic reference value in case of int is 2^15=32768 and in case of long,it is 2^31=2147483648
The garbage value thus calculated is checked whether it can fit the variable or not.If not the steps are repeated again.

Ok thats sounds confusing lets take an example

ex1:

a=45000;

overflow value=45000
Basic reference value=32768
since the reference value is less than the overflow value ,we have to multiply it by 2
i.e,32768*2=65536
65536 is greater than the 45000.That means reference value(RV)=65536.SInce RV must be of same sign,RV=+65536
now apply formula
garbage value=overflow value-reference value.
garbage value=45000-(+65536)
garbage value=-20536
-20536 comes within the limits of int datatype.

 

 

ex2:

b=4000000000;

overflow value(OV)=4000000000
SInce ‘b’ is of long type Basic reference value=2147483648
since the reference value is less than the overflow value ,we have to multiply it by 2
i.e,2147483648*2=4294967296
4294967296 is greater than the 4000000000.That means reference value(RV)=4294967296.SInce RV must be of same sign,RV=+4294967296
now apply formula
garbage value=overflow value-reference value.
garbage value=4000000000-(+4294967296)
garbage value=-294967296
-294967296 comes within the limits of long datatype.

So the output of the program will be
-20536 -294967296

 

 

Now, run the program and see whether the output is same as calculated or not.

Modify the above program

 

a=-40000;
b=-4500000000;

now , do not run the program.

ex3:

a=-40000;

OV=-40000
discard the sign take 40000.Basic reference value=32768
since the reference value is less than the overflow value ,we have to multiply it by 2
i.e,32768*2=65536
65536 is greater than the 40000.That means reference value(RV)=65536.SInce RV must be of same sign as OV,RV=-65536
Now apply the formula
garbage value=overflow value-reference value.
garbage value=-40000-(-65536)
garbage value=-40000+65536
garbage value=25536
25536 comes within the limits of int datatype.

 

ex4:

Code:

b=-45000000000;

overflow value(OV)=-4500000000
SInce ‘b’ is of long type Basic reference value=2147483648
discard the sign take 4500000000.
since the reference value is less than the overflow value ,we have to multiply it by 2
i.e,2147483648*2=4294967296 is less than 4500000000.
therefore 4294967296*2=8589934592
8589934592 is greater than the 4500000000.That means reference value(RV)=8589934592.SInce RV must be of same sign,RV=-8589934592
now apply formula
garbage value=overflow value-reference value.
garbage value=-4500000000-(-8589934592)
garbage value=-4500000000+8589934592)
garbage value=4089934592
4089934592 does not come within the limits of long datatype.

 

The process is repeated again
overflow value(OV)=4089934592
RV=+4294967296
garbage value=4089934592-(+4294967296)=-205032704
-205032704 comes within the limits of long datatype.
That means final garbage value is -205032704

So the output will be
25536 -205032704

 

Now run the program.Is your output matching with mine??

 

Some special cases:

There is a special overflow value which will return the same overflow value but opposite in sign. The values are 32768 in case of int and 2147483648 in case of long datatype.The garbage value will be -32768 and -2147483648 respectively.
Lets understand what happens.

As i already explained ,the maximum +ve value that can be stored by int datatype variable is 32767 not 32768
That means.OV=32768 Basic RV=32768. RV must be greater than OV.Therfore RV=32768*2=65536
Now, garbage value(GV)=OV-RV=32768-65536=-32768

Similarly apply this to 2147483648 you will get
GV=2147483648-4294967296
GV=-2147483648
Quite fascinating.Isn’t it??


Dynamic Output in C

Dynamic output

consider the following program

 

 

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

the output of the program will be

 

Value of pi=3.1416

wouldn’t it be nice if we allow the user to choose how the precession of the answer would be??
that is the output should be like this

 

Enter precision
6
The value of pi precised to 6 decimal number is 3.141593

Well in that case the program will be like this

 

#include<stdio.h>
int main()
{
long double a=3.14159265;
int pre;
printf(“Enter precision\n”);
scanf(“%d”,&pre);
printf(“The value of pi precised to %d decimal number is %.*Lf”,pre,pre,a);
return(1);
}

see the difference,it is upto the user to decide how the output will be.The value of pi will be rounded off to fit the precision .Such an output is called dynamic output.

This is the part of the program which is important

 

printf(“The value of pi precised to %d decimal number is %.*Lf”,pre,pre,a);

you can create a dynamic output by just specifing * before control string and specifying the variable having precesion in the arguments list.As you can see i have specified pre twice because one to print the The value of pi precesiced to__ and the other to suggest the compiler to fill the * with the value of pre.

This fact can not only be used to specify the precision of the decimal number but also for various other things.Some of them are

1.Printing first n characters of the given string

consider

printf(“The Output is %.*s”,pre,str);

suppose the string variable str has a string of “adithya” and the value of int datatype variable pre is 3 then the output will

 

The Output is adi

2.Justification
consider

 

printf(“The Output is %*.*f.Bye!”,just,pre,num);

suppose num(float)=1983.78
pre(int datatype variable)=2
just(int datatype variable)=25

then the output will be in the form of right justification i.e,

 

The Output is 1983.78.Bye!

suppose you want the output to be left justified then either you can specify ‘-*.*f’ or give a negative value to the variable ‘just’
suppose for the above example just=-25 then the output will be

 

Code:

The Output is 1983.78 .Bye!


floor and ceil

 

floor and ceil

floor and ceil functions comes under math.h header file.
floor is used to round down the value of the decimal number and ceil is used to round up the value of the decimal number.

consider the following c program

#include<stdio.h>
#include<math.h>
#include<conio.h>
{
float a;
clrscr();
printf(“Enter any decimal number\n”);
scanf(“%f”,&a);
printf(“Using floor the value is %d and using ceil it is %d\n”,floor(a),ceil(a));
getch();
}

Output:
Enter any decimal number
5656.98
Using floor the value is 5656 and using ceil it is 5657

In otherwords floor will neglect the decimal part while ceil will add 1 to the mantissa part.Hence we can say that floor is similar to casting.That means floor(a) is equivalent to (int)a or (long)a

These 2 functions do not depend upon the value after decimal point that means if the program is run with the value 5656.01 then the output will be same.

Similarly there is floorl and also ceill which returns long type integers

 

 


Const data type

 

const data type

This lesser known data type 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.

 

So, the difference between const and #define is that we can change the value of the constant during runtime execution if we use const where as we are unable to do so if we use #define.

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};

Note:
1.I have tested these in Turbo C for windows.Earlier versions of C will not support this datatype.
2.The constants created by using const is called as static constant.If you want your constant to be both dynamic as well as static,the only way is to use #define


enum data type

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}

and afterwards to declare variables of enum type ‘name’

enum name variables;

ex:enum week a;

now after doing this we can directly assign the value of a as

ex:a=friday;

the value of a will be 4.Because the compiler will automatically assign the value 0 to sunday,1 to monday and so on..
if we want to change the sequence we can specify as

enum week{sunday=1,monday,tuesday,wednesday,thursday,friday,saturday};

now the compiler will start assigning value to names from 1
that means
if we specify

a=friday;
printf(%d”,a);

then 6 will be the output.

we can also specify random values to names like

enum week{sunday=4,monday=5,tuesday=1,wednesday=3,thursday=7,friday=2,sat=6};

and corresponding values of the name will be printed.
let’s come to the defenition of enum datatype..
if you refer some books it states as

An enumerated type is an abstract data type used to model an attribute that has a specific number of options (or identifiers)


That means we cannot specify the value other than the specified names declared.
But consider the following program
#include<stdio.h>
#include<conio.h>
{
enum week{sun=4,mon=5,tues=1,wednes=3,thurs=7,friday=2,sat=6};
enum week a;
a=25;
printf(%d”,a);
getche()
}

will print 25 itself.I don’t know whether i am correct or not


Edit Set

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.
Include set:

Ex:scanf(“%[a-z]“,a);
means it will read all the characters between a to z and if other character in encountered, it will stop reading the variable.
Ex:if the input is adithyaU, then the value of string variable ‘a’ will be adithya only.

You can specify the individual chars also
Ex:scanf(“%[a,d,i]“,a);
will read only the chars a,d,i
if input adithya then the value of ‘a’ will be adi only.

Special characters and white spaces may also be included in the edit set

Ex:scanf(“%[*],a);
will read only chars *

Exclude set

ex:scanf(“%[^t]“,a);
means it wil read all the characters until t is encountered

Here all the chars means special chars and white space chars are also included.That means draw back of scanf over gets can be overcome by using exclude set

if i input adithya then the value of ‘a’ will be adi

ex:scanf(“%[^A-Z]“,a);
means no Capital’s are allowed

ex:scanf(“%[^\n]“,a);
read ALL chars until enter key is encountered(Function as gets())

The use of edit set is illustrated in the following program:

Write a c program which makes scanf to accept the special as well as white space chars

 

Example Program:

#include<stdio.h>
void main()
{
char str[100];
printf(“Enter any string\n”);
scanf(“%[^\n]s”,str); //<—– Imp step——–
printf(“\n You entered string %s \n”,str);
}