This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
int arr[3],i=0;
while(i<3)
{
arr[i]= ++i;
}
for(int i=0;i<3;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
why is this outputting garbage value,1,2 ? i expected it to be 1,2,3. Please help
TIA
The expression:
arr[i]= ++i;
is undefined behaviour because i appears on both sides of the assignment operator (=), but the behaviour in this instance is empirically:
i++ ;
arr[i] ;
i.e. i is incremented before arr is indexed.
You need:
arr[i] = i + 1 ;
i++;
for the "expected" output:
1
2
3
Related
This question already has answers here:
What does the integer suffix J mean?
(2 answers)
What does c++ constant value 1j mean? [duplicate]
(1 answer)
Why can't we do three-way comparison in C++? [duplicate]
(6 answers)
Comparing a variable to a range of values
(7 answers)
Closed 4 months ago.
Translation:
"After running the code below, what is the value of k?'
This question was from my friend's homework(we were senior highs). He didn't know how to deal with this, so he asked me and then I tried to test this on my computer(Debian 10, gcc-10.2.1). The result is (A) , which means the value of k is 6. I reflected on the if statement but still had no idea. It utterly made no sense - especially the syntax(who will even write code in that way?) . That's why I would like to ask this question. Perhaps that's a bug I haven't met before.
The C code:
#include <stdio.h>
int main() {
int i, j, k = 0;
for ( i = 0 ; i <= 2 ; ++i ) {
for ( j = 0 ; j <= 2 ; ++j ) {
if ( i != 1i != 2 && j != 1 ) {
k = k + j;
}
}
}
printf("%d\n", k);
return 0;
}
Why can the following if statement work?
if ( i != 1i != 2 && j != 1 )
And what's the value of 1i?
This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I am trying to understand how array boundaries work in C, so tried the following code:
#include <stdio.h>
int main(){
{
int i, s[4], t[4], u=0;
// fisrt loop
for (i=0; i<=4; i++)
{
s[i] =i;
t[i] =i;
printf("s = %d\n", s[i]);
printf("t = %d\n", t[i]);
}
// second loop
printf("s:t\n");
for (i=0; i<=4; i++)
printf("%d:%d\n", s[i], t[i]);
printf("u = %d\n", u);
}
return 0;
}
The output is as follows :
fisrt loop
s = 0
t = 0
s = 1
t = 1
s = 2
t = 2
s = 3
t = 3
s = 4
t = 4
second loop
s:t
4:0
1:1
2:2
3:3
4:4
u = 0
I am expecting both loops to print 5 elements from 0 to 4. As you can see, the first for loop's output looks ok, but in the second loop the value of s[0]
is wrong and for some reason turned to 4.
I am assuming that this is happening because of some problem in the array boundary, but I am not sure, please correct me if I am wrong. Is there any way to fix this, and make s[0] correct value? Cheers.
for (i=0; i <4; i++)
in your code you access 5 elements of the array and your array has only 4 elements. It ans Undefined Behaviour.
Second loop the same error.
There is a problem with the size of the array, you need to modify your code of that line to given code:
int i, s[4], t[4], u=0;
to
int i, s[5], t[5], u=0;
Delete the equals in the for loops:
for (i = 0; i < 4; i++)
The length of the arrays is 4. From 0 to 4 (included) there are 5 positions.
When you declare int s[4]; you create an arrays of lenght 4: s[0], s[1], s[2], s[3]. You can't access s[4].
This is because the position starts to 0.
If you want to have 5 elements for both arrays:
Change the declarations of s and t to s[5] and t[5]. Only at the declaration the 5 means 5 elements. Only at indexing and addressing a certain element, counting starts at 0, not 1.
If you want to have the arrays to be consisted of 4 elements:
for (i = 0; i <= 4; i++)
i <= 4 - With this condition and the initialization i = 0 you access memory beyond the bounds of the array at the last iteration, which invokes undefined behavior.
When i == 4, s[i] and t[i] is an out of bounds access as s and t are both arrays of 4 int, not 5.
It needs to be i < 4 or i <= 3 for the arrays of 4 int or (if you want to) or change the initialization to i = 1.
This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
Closed 2 years ago.
I've been trying to find the length of a given number using this program, however I get the following error every time I run it:
check_length.c:16:26: runtime error: division by zero
#include <cs50.h>
#include <stdio.h>
void check_length(long);
int main(void)
{
long c = get_long("Enter Number: ");
check_length(c);
}
void check_length(long w)
{
for(int i=1;i<=16;i++)
{
int scale = w/((10)^i);
if (scale<10 && scale>0)
{
int length = i+1;
printf("%i\n",length);
}
}
}
The simple answer is that the operator ^ isn't doing what you expect. It seems you expect it to raise 10 to the power of i but ^ is actually the bitwise XOR operator.
So when i is 10, you do 10 XOR 10 which is zero. Therefore the division by zero.
You could take a look at the pow function instead but why make things that complicated?
Simply keep diving by 10 until you get zero and then return the number of divisions.
Something like this for non-negative values:
#include<stdio.h>
int check_length(unsigned long w)
{
int result = 0;
while (w > 0)
{
++result;
w = w/10;
}
return result;
}
int main()
{
printf("%d\n", check_length(0));
printf("%d\n", check_length(9));
printf("%d\n", check_length(10));
printf("%d\n", check_length(1234567890));
return 0;
}
Output:
0
1
2
10
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
#include< stdio.h >
int main()
{
int i = 1;
int x = ++i * ++i * ++i;
printf("%d\n", x);
printf("%d\n\n",i);
return 0;
}
Im getting output of 1!! and 4 in gcc. I use ubuntu linux
The behaviour of your code is undefined since i is modified more than once between sequence points:
int x = ++i * ++i * ++i;
See the FAQ (I urge you to read the entire section 3).
Undefined Behaviour this is:
int x = ++i * ++i * ++i;
Don't do it!!!!
This question already has answers here:
for loop missing initialization
(6 answers)
Closed 2 years ago.
Not long ago, I stumbled across a very interesting innovation in a for loop (or maybe it is nothing new but I've been living in a cave?). Please take a look and explain to me why the initialize gap is empty? Of course, that's the part of the program which works. I would grateful for any revelation:)
void rzad_zn(char ch, int i, int j)
{
int pom;
pom = i;
for(; i<=j; i++)
{
printf("%d ", i);
}
printf("\n");
for(; pom<=j; pom++)
{
printf("%c ", ch);
}
printf("\n");
}
The for loop has three components, all of which are optional, as in this is a valid loop as well:
for (;;) { }
Though it is an infinite loop unless something calls break.
Any combination of arguments may be used, even compound ones like:
for (int i = 0, j = 0; i < j; ++i) { ... }
Where you can declare multiple variables, or use the comma operator in the other parts to join together several operations if necessary.