Saving the output of a C program in a text file(Linux/Unix)

Consider the following c program

#include
main()
{
int i,n;
printf("Enter limit\n");
scanf("%d",&n);
printf("I will print %d times\n",n);
for(i=0;i

the output will be


Enter limit
4
I will print 4 times
I luv C
I luv C
I luv C
I luv C

This is a simple program which will print "I luv C" n times.Now, wouldn't be nice if we saved the output in a text file
then in such case you have to use directed symbol ">>"
(more...)


Important instructions for programs using math.h library function

if your program uses math.h header file then sometimes the compilation will lead to errors.In such cases you have to suffix -lm to the compilation file command

Usage

cc filename.c -lm

lm means link mathematical header file.Note that this step is not always required use it only if your program is correct and compilation is leading to errors.
and all other procedures are same.
(more…)


Advantages of running c in Linux/Unix-1

C is strongly linked with linux/unix .The kernel of Unix/Linux is written in C.Running C in linux/unit has several advantages .The most important among it is full word length utilization.
Consider the following C program

#include
int main()
{
int a;
float b;
printf("Size of int datatype =%d bits\n",(sizeof(a)*8));
printf("Size of float datatype =%d bits\n",(sizeof(b)*8));
return 1;
}

(more…)


C in linux/unix-First step

First let’s learn how to run a c program in linux/unix.Nearly every distro support’s running c programs.

First thing is to type the c program.you can use any editor you want but here i use vi editor which comes with allmost all distro

vi editor

To create a new c file or to open an existing c file,type vi followed by filename.c
ex:
[student@localhost student]$vi adi.c

will create a new file adi.c or will open an exisiting file adi.c if it’s present

now press insert key to edit or modify or add new content
after you complete writing the program,press Esc key and type :w (colen w) and press enter to save the file then press :q (colen q) and press enter to exit the vi editor.
(more…)


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