Note 1
Take Note:

Take a note while surfing.

Note With Ink

Give your Note a Colorful Tag.

Easy to Access

Stay on same information and in Sync wherever you are.

Note 2
Take Note:

Organize your information,It may take Shape.

Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3
Take Note:

Don't Let information to miss,Because it take shape

Note With Ink

Simple an Easy Way to take a note.

Easy to Access

Get the same in next visit.

C Programming :: Floating Point Problems

Home > C Programming > Floating Point Problems > General Questions

1. Member function of a class template should be defined

| | | |

Explanation:

Here is no explanation for this answer

Workspace

Tags:  No Tags on this question yet!

2. What would be the value of c?

void main()
{
int c;
float a,b;

a= 245.05;
b = 40.02;
c =a+ b;
}

| | | |

Explanation:

Here is no explanation for this answer

Workspace

Tags:  No Tags on this question yet!

3. What would be the value of i and k in below C program?

void main() {
int i,j,k;
j = 5;
i = 2 *j/2;
k = 2 *(j/2);
}

| | | |

Explanation:

at first, glance looks like i value will be 4 and k value will be 4 too.

but if you see in second statement () has been used. why?

An answer will be i = 5 and k = 4.

Workspace

Tags:  No Tags on this question yet!

4. If a = -11 and b = -3 what is the value of a%b?

| | | |

Explanation:

Note: % is a reminder operator in c programming (and programming language). For the remainder operator, the sign of the result is the same as the sign of the dividend.

C define % operator operation for a%b as below:

a = (a/b) * b + a%b

Here dividend is negative so the answer will be -2

Important Points:

=> The answer will be -2 in the scenario where a = -11 and b = 3

=> The answer will be 2 in the scenario when a = 11 and b = -3 or 3

Workspace

Tags:  No Tags on this question yet!

5. What is the output of the following 'C' program ?

#include
# pragma pack(2)
struct SIZE {
int i;
char ch ;
double db ;
} ;
main () {
printf ( "%d\n",sizeof(struct SIZE) );
}

| | | |

Explanation:

Pragma pack instructs the compiler to pack structure members with particular alignment. Most compilers, when you declare a struct, will insert padding between members to ensure that they are aligned to appropriate addresses in memory (usually a multiple of the type's size). This avoids the performance penalty (or outright error) on some architectures associated with accessing variables that are not aligned properly. For example, given 4-byte integers and the following struct:

struct SIZE { int i; char ch ; double db ; } ;

The compiler could choose to lay the struct out in memory like this:

 1 byte 2nd byte 3rd byte 4th byte int i i(1) i(2) i(3) i(4) char ch ch(1) padding padding padding double db db(1)db(5) db(2)db(6) db(3)db(7) db(4)db(8)

the size of this structure will be 16.

with pragma pack (1)

1 byte

int i(1)

int i(2)

int i(3)

int i(4)

char ch(1)

double db(1)

.

.

double db(8)

and the size of strut will be 13.

with pragma pack (2)

1st byte        2nd byte

int i(1)             int i(2)

int i(3)             int i(4)

char ch(1)        double db(1)

.                         .

double db(6)    double db(7)

the size of struct will be 14  (Answer). if pragma pack (4) is given then in 32 bit size of sruct will be 16.

pragma pack tells the compiler the boundary to align objects in a structure to in order to improve access times.

Workspace

Tags:  Global Edge

6. #define mysizeof(a) (&a+1) - &a
void main()
{
float d;
printf("%d\n", mysizeof(d) );
}
note: assume sizeof float is 8 bytes

| | | |

Explanation:

Tricky ... little bit .. basically in below line &a (address of float variable d) getting subtracted from address of variable + 1 i.e incrementing it with 1 gives the address where the next variable of the type d can be stored, the answer is 1 instead of 8 (as given float size is 8 byte).

#define mysizeof(a) (&a+1) - &a

The difference gives the result that how many variables of type of d can be stored in that amount of memory which will obviously be 1 for the type d. The result of pointer subtraction is in elements and not in bytes. so, that the below expression evaluates to 1. instead of 8.

If you above statement put like below, the answer will be 8.

#define mysizeof(a) (char*)(&a+1) - (char*)&a

typecasting it int char* and taking the difference will tell us how many variables of type char can be stored in the given memory space (the difference). Since each char requires only 1 Byte of memory therefore (amount of memory)/1 will give the number of bytes between two successive memory locations of the type of variable passed on to the macro and hence, the amount of memory that the variable of type d requires i.e 8 bytes.

Workspace

Tags:  Global Edge