Showing posts with label data types. Show all posts
Showing posts with label data types. Show all posts

Thursday, August 5, 2010

Typecasting in c

Lesson 11: Typecasting

Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.

For example:
#include  

using namespace std;

int main()
{
cout<< (char)65 <<"\n";
// The (char) is a typecast, telling the computer to interpret the 65 as a
// character, not as a number. It is going to give the character output of
// the equivalent of the number 65 (It should be the letter A for ASCII).
cin.get();
}
One use for typecasting for is when you want to use the ASCII characters. For example, what if you want to create your own chart of all 256 ASCII characters. To do this, you will need to use to typecast to allow you to print out the integer as its character equivalent.
#include 

using namespace std;

int main()
{
for ( int x = 0; x < 256; x++ ) {
cout<< x <<". "<< (char)x <<" ";
//Note the use of the int version of x to
// output a number and the use of (char) to
// typecast the x into a character
// which outputs the ASCII character that
// corresponds to the current number
}
cin.get();
}
The typecast described above is a C-style cast, C++ supports two other types. First is the function-style cast:
int main()       
{
cout<< char ( 65 ) <<"\n";
cin.get();
}
This is more like a function call than a cast as the type to be cast to is like the name of the function and the value to be cast is like the argument to the function. Next is the named cast, of which there are four:
int main()       
{
cout<< static_cast ( 65 ) <<"\n";
cin.get();
}
static_cast is similar in function to the other casts described above, but the name makes it easier to spot and less tempting to use since it tends to be ugly. Typecasting should be avoided whenever possible. The other three types of named casts are const_cast, reinterpret_cast, and dynamic_cast. They are of no use to us at this time.

Wednesday, April 7, 2010

Comparing floats

Problem
 int main()
{

float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}

Output - I hate U
Explanation
For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes & long double takes 10 bytes. So float stores 0.9 with less precision than long double. 

Solutions 
//compares if the float f1 is equal with f2 and 
//returns 1 if true and 0 if false
int compare_float(float f1, float f2)
{
float precision = 0.00001;
if (((f1 - precision) < f2) &&
((f1 + precision) > f2))
{
return 1;
}
else
{
return 0;
}
}

You can set the precision of the comparison between the 
floating point numbers by changing the "precision" variable.
Calling the function:

//we compare our numbers
if (compare_float(x1,x2))
{
//do something if equal
}
else
{
//do something if not equal
}

Method2 - using fabs i.e.  epsilon absolute error
if (fabs(me - you) < 0.00001)
printf("I love U");
else
printf("I hate U"); 


Absolute error calculations have their place, but they aren’t what is most often used. When talking about experimental error it is more common to specify the error as a percentage. Absolute error is used less often because if you know, say, that the error is 1.0 that tells you very little. If the result is one million then an error of 1.0 is great. If the result is 0.1 then an error of 1.0 is terrible.


Saturday, December 5, 2009

Variable declaration and definition

What is BSS?
BSS, a part of Data Segment store all variables initialized to 0. static variable(initialized with value other than 0) are not stored in BSS.
Actually BSS is an "Uninitialized RAM" which is initialized to 0 before executing main().



"static" variables are hold in the heap memory and "auto" variables are stored on the stack.

Heap(Data Segment & BSS(Block start by symbol))
Static varibales are neither stored in stack nor in heap. Static varibales are stored in data segment.

Auto variables are stored in stack and dynamic allocated variables are stored in heap.



Memory allocations of structure
I think slack memory is the extra memory at the end of a structure which may go unused. It may also include unused memory within a struct. For example if a struct includes a short and then an int in that order the int needs to be on a 4-byte boundary but the short needs to be on a 2-byte boundary. Thus 2 bytes go to waste. If a stack is allocated from the heap the memory manager may fragment the heap in a manner where is unused memory contiguously after the stack object. This definition is cloudy as I couldnt find a real crisp defiition of "slack memory".



Can static variables be declared in a header file?

You can’t declare a static variable without defining it as well (this is because the storage class modifiers
static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended. 

Friday, December 4, 2009

To add two long positive numbers (each represented by linked lists in C)

node *long_add(mynode *h1, mynode *h2, mynode *h3)    //h3 = h2+h1
{
  node *c, *c1, *c2;
  int sum, carry, digit;

  carry = 0;
  c1 = h1->next;
  c2 = h2->next;

  while(c1 != h1 && c2 != h2)
  {
     sum   = c1->value + c2->value + carry;
     digit = sum % 10;
     carry = sum / 10;

     h3 = insertNode(digit, h3);

     c1 = c1->next;
     c2 = c2->next;
  }

  if(c1 != h1)
  {
     c = c1;
     h = h1;
  }
  else
  {
     c = c2;
     h = h2;
  }

  while(c != h)
  {
    sum   = c->value + carry;
    digit = sum % 10;
    carry = sum / 10;
    h3 = insertNode(digit, h3);
    c = c->next;
  }

  if(carry==1)
  {
     h3 = insertNode(carry, h3);
  }

  return(h3);
}

Thursday, December 3, 2009

Going beyond range of datatype

Egs on unsigned integer



#include
main(){
unsigned int i;
for(i=1;i>-2;i--)
printf("c aptitude");
}
Explanation:
i is an unsigned integer. It is compared with a signed value. Since the both
types doesn't match, signed is promoted to unsigned value. The unsigned
equivalent of -2 is a huge value so condition becomes false and control
comes out of the loop.







#include
int main()
{
unsigned giveit=-1;
int gotit;
printf("%u ",giveit);
printf("%u ",++giveit);
printf("%u \n",gotit=--giveit);
}


Answer
65535  0  65535






main()
{
signed char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer
-128
Explanation
Notice the semicolon at the end of the for loop. THe initial value of
the i is set to 0. The inner loop executes to increment the value
from 0 to 127 (the positive range of char) and then it rotates to the
negative value of -128. The condition in the for loop fails and so
comes out of the for loop. It prints the current value of i that is -128.




 main()
{
unsigned char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer
infinite loop
Explanation
The difference between the previous question and this one is that the char
is declared to be unsigned. So the i++ can never yield negative value and i>=0
never becomes false so that it can come out of the for loop.





main()
{
char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer:
Behavior is implementation dependent.
Explanation:
The detail if the char is signed/unsigned by default is
implementation dependent. If the implementation treats the char to be
signed by default the program will print –128 and terminate. On the other
hand if it considers char to be unsigned by default, it goes to infinite loop.
Rule:
You can write programs that have implementation dependent
behavior. But dont write programs that depend on such behavior.