This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
What does ': number' after a struct field mean? [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to understand the following code in C:
struct values{
int i:3;
int j:3;
int k:2;
};
int main(){
struct values v = {-6,2,5};
printf("%d %d %d", v.i,v.j,v.k);
}
This code produces the following output:
2 2 1
I'm trying to understand what does it mean the assignment for int values used inside the struct i.e.: int i:3 ?
I know that : is not an operator. So what does it do?
Also, can someone explain how this output is achieved?
Thanks very much!
The numbers specifies the length in bits for each field.
Hence i and j are represented in 3 bits while k in 2 bits.
By the way this is question is clearly a duplicate of this question and there's a very good answer I suggest you to read.
Related
This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9
This question already has answers here:
C Unions output unclear
(2 answers)
Closed 6 years ago.
I was at an interview and the following code was given to me:-
union mix
{
int i;
char c[2];
};
int main()
{
union mix key;
key.i=512;
printf("\n %d,%d,%d",key.i,key.c[0],key.c[1]);
return 0;
}
and when I came home and ran it on the system I got the output:- 512,0,2.
Can anyone explain it to me how it works or why is c[1]=2?
Edit :- all I know is that it is related to bit storage in the memory.
It's a union, meaning i and c occupy the same memory.
512 is 0x200, meaning first byte is zero, second is 2 (each byte takes two hex digits).
This of course depends on the endianness of your CPU.
This question already has answers here:
What does "%.*s" mean in printf?
(4 answers)
Closed 6 years ago.
Why the following program work ? & what is difference b/w %d vs %*d ?
#include<stdio.h>
int main()
{
int n=5;
printf("n=%*d\n", n, n);
return 0;
}
What does width meant here by ?
The first n belongs to the * in %*d, so for your example it is %5d. %5d means to print an integer of width 5. Example: 234 is printed as __234 (2 spaces)
This question already has answers here:
Is uninitialized data behavior well specified?
(7 answers)
What will be the value of uninitialized variable? [duplicate]
(6 answers)
Closed 8 years ago.
Please explain why I am getting output 2 here. My expected o/p is 5 or 7. Please throw some light. Thank you!
#include<stdio.h>
typedef enum {a=3, b, c, d, j}e;
void f(e *e1) {
printf("%ld", (int)*e1);
}
main(){
e es;
f(&es);
}
You haven't initialized es, so your program is just printing the random value that happens to be on the stack when the program runs.
You need to say something like:
e es = c;
That will give you the 5 output you seek.
This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 10 years ago.
in running the code below , i got this output
num= 2359120, addr of num=2359120, *num=10,addr of num[0]=2359120
I can't comprehend how num and &num have the same value. any help, please? i know that the name of an array is a pointer itself
#include <math.h>
#include<stdio.h>
main()
{
int num[]={10,20,30,40,50};
printf("num= %d, addr of num=%d, *num=%d,addr of num[0]=%d\n",num,&num,*num,&num[0]);
}
name of array num is same as the address of the array &num which is same as the address of the first element &num[0] and hence, your output.