Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I would appreciate if anyone can help me figuring out why is this fuction (fn) does not print the int array elements?
#include <stdio.h>
void fn (int arr[], int s);
int main(void)
{
int arr[] = {5, 2, 0, 9, 7}, s=5;
fn (arr, s);
}
fn(int arr[], int s)
{
int i;
for (i = 0; i < 5; i++);
{
arr[i]++;
printf("%d ", arr[i] );
}
}
for (i = 0; i < 5; i++);
The semicolon at the end of the expression terminates the for-loop before even entering the body of the loop. Hence the body is seen as a stand-alone block of code by the compiler, unrelated to the for-loop.
Since the body of the loop is detached from the for-loop, variable i (the counter of the for-loop) is out of scope since i is local to the for-loop, and can only be called inside it as it was declared inside the conditional statement of the for-loop.
To fix this problem, remove the semicolon at the end of the for-loop condition.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm quite new to programming and I'm trying to make a simple program with a loop:
#include <stdio.h>
int a = 5;
int b = 0;
int main(void)
{
for (int i = 0; i < a, i++)
{
b++;
}
printf("%i", b);
}
However, when I try to compile I get the errors: relational comparison result unused [-Werror,-Wunused-comparison] and expected ';' in 'for' statement specifier for line 8. I've tried to look at several different sources on how to construct for loops and I just can't see what I'm doing wrong. Any help would be much appreciated.
Here's an example where you can practice interpreting the error statement. As you'll see, it says expected ';' in 'for' statement specificer. That's saying there's a place where you should have a semi-colon but you don't.
In your casse, there should be a semi-colon after the i < a. Right now, you have a comma.
I think you are using , instead of ; in the for loop after the statement.
for (int i = 0; i < a; i++)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
#include<math.h>
#include<stdio.h>
int main(void)
{
int i = 0;
int f = 10000;
int div1 = (powl(10,i));
int temp1 = f/div1;
for(i = 2; temp1 > 1; i++)
{
printf("%i\n",temp1);
}
}
As far as I know, the value of div1 should be 100,1000,10000... With corresponding increments in I. Then temp1 should be 100,10, then loop stops (?). But I get an endless loop of 10000 10000 10000 10000......
Can someone explain what am I doing wrong?
The for loop checks for temp1, but temp1 is not modified in the loop's body. Try putting the desired modification inside the loop's body or as the last expression in the for loop; the variable i is perhaps not necessary at all.
Your for statement should have like this. You missed to call those to statement inside your for loop
for(i = 2; temp1 > 1; i++)
{
div1 = (powl(10,i));
temp1 = f/div1;
printf("%i\n",temp1);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
today i tried to do something new,but i didn't do that correct.Would anyone be able to do that and explain why is it so like that? Thank you in advance
#include<stdio.h>
void function(int a[],int n)/*The definition of function with void type,with parameters
int a[],int n */
{
int i;// declared count,type integer//
for(i=0;i<n;i++)//count goes from 0,to <n,and increment all time while goes//
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
printf("\n");// printing the newline //
}
main()
{
int a[]={1,2,3,4,5,6,7}; // declaration of array with 7 elements //
int n=5;// declaration of variable n type integer value of 5 //
function(a,n) // calling the function with parametres a,n//
} // end of sequence //
In my case i got the result of the 1,2,3,4,because i tought that the count goes from 1,to the one number less than n=5,but the IDE show the result of 135 ,i think the problem in my way is with counter...but all advices are welcome,thanks
Please make sure you are posting properly formatted valid C code.
Note that what you get is not one hundred and thirty five, but one, three, and five. You get that because you are incrementing the loop counter twice.
Here's a working, more readable version:
#include <stdio.h>
void function(int a[],int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
int main(void)
{
int a[]={1,2,3,4,5,6,7};
int n=5;
function(a,n);
return 0;
}
replace
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
with
printf("%d",a[i]);// printing on the screen integers (a[i],i=i+1)//
in your code you were incrementing i twice. Once in the while and once in the a[i++]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
this is a simple question as i'm a new-comer to C. I am trying to write a script for outputting an array of the tangents of radians, of multiples of 5 from 0-60. but for some reason the for loop i have written only does this for the first element, and all other elements in the resulting array are 0.00, and it wont print them for each loop. i'm sure i've done something simple wrong with my loop, but i just can't see it.
#include <stdio.h>
#include <math.h>
float rad(float degree){
return degree*M_PI/180;
}
int main(void){
int i, j, dim=13;
float Tan[dim];
for(i=0; i<13; i++);{
j+=5;
Tan[i]=tan(rad(j));
printf("%f\n", Tan[i]);
}
return 0;
}
First of all, in your code
j+=5;
is undefined behavior, as the intial value of j is indeterminate. To elaborate, j is an automatic local variable and not initialized explicitly, so the content is indeterminate.
Then, the for loop is also buggy.
for(i=0; i<13; i++);
should be
for(i=0; i<13; i++) // no ; here
to have a meaningful loop body to be executed.
1. You have inserted a semi-colon which you shouldn't have. Change your loop to :
for(i = 0; i < 13; i++){ //erase the ; after the parenthesis
j+=5;
Tan[i] = tan(rad(j));
printf("%f\n", Tan[i]);
}
2. Initialize variable j before trying to increase it with the statement j+=5, as this will lead to undefined behaviour.
There are two problems in your code :-
1) You haven't initialized j here int i, j, dim=13;
2) The way you used for loop is as per your requirement.Remove semicolon from the for loop statement.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I know that a string should be n+1 in length, but for some reason my program is printing the sizeof(string) as n-2.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name [] = "Tom";
int x = sizeof(name);
int i;
printf("sizeof(name) = %d\n", i);
for(i = 0; i < x; i++)
{
printf("Character at %d is %c\n", i, name[i]);
}
return 0;
}
Can anyone explain why?
You're printing i, not x.
i was never initialized, so you get undefined behavior.