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.
Related
This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
C Unsigned int providing a negative value?
(3 answers)
Closed 2 years ago.
There is an interview question in C as below.
int main()
{
unsigned int a = 9;
a = ~a;
printf("%d\n", a);
}
I though it was supposed to be 6 but it is -10.
~a is assinged back to an unsigned integer then printed out.
It should not be a negative value.
Isn't it? Hoe come?
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);
This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 4 years ago.
Here are we use typecasting from pointer to integer, but output of arithmetic operation are different from expected answer, Why?
Source Code :
int main(){
int *p,*q;
p = 1000;
q = 2000;
printf("%d",q-p);
return 0;
}
Output: 250
The same reason due to which p++ will give you 1004. Since sizeof(int) on your machine is 4, hence each operation is shown wrt sizeof(int), even the difference i.e. 1000/4.
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).
This question already has answers here:
Initializing a float array with memset
(5 answers)
Closed 9 years ago.
I'm trying this simple example to populate a floating point array with 5.6, but upon printing the values out, every value is just 0.0.
#include <string.h>
float testArr[20];
memset(testArr, (float)5.6, 3*sizeof(float));
printf("Value 1: %lf\n",testArr[0]);
printf("Value 2: %lf\n",testArr[1]);
printf("Value 3: %lf\n",testArr[2]);
printf("Value 4: %lf\n",testArr[3]);
I've also tried not casting 5.6 as a float, setting testArr[20] = {} and testArr[20] = {0}, but they also result in the same 0.0.
memset fills the memory with 1 char, not float.