Calculating offset of a Structure in C [duplicate] - c

This question already has answers here:
Structure padding and packing
(11 answers)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 7 days ago.
I was writing a code understand the calculation of offset for a structure in C.
#include <stdio.h>
#include <stddef.h>
typedef struct emp_
{
char name[10];
unsigned int salary;
char designation[100];
unsigned int emp_id;
}emp_t;
int main()
{
printf("%lu\n", offsetof(emp_t, name));
printf("%lu\n", offsetof(emp_t, salary));
printf("%lu\n", offsetof(emp_t, designation));
printf("%lu", offsetof(emp_t, emp_id));
return 0;
}
The above code returns the output of offset for structure variables as:
name - 0
salary - 12
designation - 16
emp_id - 116
However, from my understanding the following should be offset for the structure variables.
name - 0
salary - 10
designation - 14
emp_id - 114
Please let me know if why this difference.

Related

I encountered a statement like "char var : 3". What does this C statement do? [duplicate]

This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 2 years ago.
While going through some C code, I encountered statements like
char var1 : num1, char var2: num2;
From the context, it seems like the number i.e. num1 is the byte size.
I am unable to find any explanation.
This could be part of what is called a bit-field in the C programming language.
Bit-fields can only be declared inside a struct, e.g.
struct {
unsigned int flag : 1; /* A one bit flag */
unsigned int value : 5; /* A 5 bit value */
} option;
if (option.flag == 1)
option.value = 7;
About everything on bit-fields is implementation-defined. The intention is to have bit-fields arranged as compact as possible by the compiler. E.g. the above could well fit in one byte.

Why this code is printing "False", though size of int is greater than -1? [duplicate]

This question already has answers here:
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
sizeof() operator in if-statement
(5 answers)
Closed 5 years ago.
According to the code below, size of int is not greater than -1. Why is it so? Why "False" was printed instead of "True"?
#include <stdio.h>
#include <stdlib.h>
int main() {
if(sizeof(int) > -1) {
printf("True\n");
}
else {
printf("False\n");
}
// Here size of int is equals to 4
printf("Size of int: %ld\n", sizeof(int));
return 0;
}
Well sizeof returns size_t which is unsigned and when it is compared with int the int is promoted to unsigned and the bit representation all 1's now considered as unsigned, which is bigger than -1 and also that of sizeof int. That's why this result.
Correct format specifier for size_t is %zu.

why is sizeof() returning 4 bytes rather than 2 bytes of short int? [duplicate]

This question already has answers here:
Why does sizeof(char + char) return 4?
(2 answers)
Why must a short be converted to an int before arithmetic operations in C and C++?
(4 answers)
sizeof operator returns 4 for (char + short ) [duplicate]
(3 answers)
What happens here? sizeof(short_int_variable + char_variable)
(5 answers)
How does sizeof work for different data types when added and calculated? [duplicate]
(1 answer)
Closed 5 years ago.
To print the size of the variables using sizeof()
#include <stdio.h>
main()
{
int a = 10,b = 20;
short int c;
short int d = sizeof(c = a+b);
int e = sizeof(c*d); //e holds the value of 4 rather than 2
double f = sizeof(e*f);
printf("d:%d\ne:%d\nf:%lf\n",d,e,f);
}
Why is sizeof() returning the size of int rather than short int which is meant to be 2 bytes?
The statement
sizeof(c = a+b);
doesn't measure the size of variable c but the size of the value computed from expression c = a+b. It is the value of a+b that is assigned to c but it is also the value of the entire expression.
The integral values whose storage type is smaller than int that appear in an arithmetic expression are promoted to int (or unsigned int) for the computation. The storage type of the result of the arithmetic expression is int. This is not affected that the fact that you store it in a short int variable. Hence the value returned by sizeof().
The same for sizeof(c*d).

Value difference between two pointers is not making sense [duplicate]

This question already has answers here:
Pointer Arithmetic In C
(2 answers)
Closed 8 years ago.
I'm not getting the output. Why it is happening?
#include <stdio.h>
int main(void){
int a[3][3];
int *p, *q;
p=a[0];
q=a[1];
printf("%d\n",sizeof(int));
printf("%d\n",q-p);
printf("%d %d\n",q,p);
return 0;
}
Output
4
3
2686728 2686716
I thought (q-p) should be 12! Is my math degrading?!
I thought (q-p) should be 12
No. (q-p)==3 shall hold true since they have type int*. Meanwhile it's true that q == p + 3.
Also this is true: (char*)q - (char*)p == 12
You are getting output value as 3, because both pointers are pointing to integer data type and the difference between them is three objects.

C Union and simultaneous assignment to members [duplicate]

This question already has answers here:
Floating point numbers do not work as expected
(6 answers)
Closed 8 years ago.
In the following code
#include<stdio.h>
int main()
{
union myUnion
{
int intVar;
char charVar;
float floatVar;
};
union myUnion localVar;
localVar.intVar = 10;
localVar.charVar = 'A';
localVar.floatVar = 20.2;
printf("%d ", localVar.intVar);
printf("%c ", localVar.charVar);
printf("%f ", localVar.floatVar);
}
I understand that union can hold only one value at a time. So when I assign char value, int would be overwritten, n then when I assign floatValue, char would be overwritten. So I was expecting some garbage values for int and char variables and 20.200000 for float variable as it was last value to be assigned. But following is the output I'm getting on VS Express as well as gcc
1101109658 Ü 20.200001
unable to understand why float value is changed ?
This has nothing to do with union, and the float value was not changed.
It simply doesn't have enough bits to represent 20.2 exactly as a binary float. But that's okay, nobody has that many bits.
You should read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Resources