Why variable doesn't have value a outside loop? [closed] - c

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 8 years ago.
Improve this question
I have this code and i wanna know why tha variable is not returning any value outside the for loop.
void juizes_nota_alta(str_nomepont a)
{
int i,j;
int notamax=0;
for (i=0;i<MAX_JUIZES;i++)
{
if (a.pontuacao[i]>notamax)
{
notamax=a.pontuacao[i];
j=i;
}
}
printf("O juiz que deu a nota mais alta foi:\n",j);
Variable j is not returning value.
Thank you

Your printf() is wrong.
It should include a %d conversion specifier. The string controls what gets printed, just passing more arguments won't make them show up unless the string says so.
Addressing that will bring you one more step closer. Also add j = 0 before the loop to make sure j has a valid value when you print it.

Initializing a variable based on some condition is not a good idea.
If the condition fails and the variable never gets initialized and when you try to use the variable you have an indeterminate variable value
So just before assigning j to anything while declaring make
int j=0;
In your printf() as suggested by everyone use %d format specifier.

Related

its giving me random numbers [closed]

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 months ago.
Improve this question
I'm practicing some simple code. When my int number is 12 the output is -743998012 for some reason even with different numbers too.
here is my input code
#include<stdio.h>
int main()
{
int favnum;
printf("Enter number: ");
scanf("%d", &favnum);
printf("My favorite number is %d.", &favnum);
return 0;
}
I try to change variables like float or double didn't work
As said by user3121023, you need to remove the & in printf("My favorite number is %d.", &favnum);.
This is because when you add it, it means that you're not passing its value, but its address in the memory (the pointer where the variable favnum is stored). That's why in the line before you use & : you want to store the input of the console in the right memory location, where the variable favnum is.

This code should print i=1 but it is printing i=0 Why is it so? [closed]

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 8 months ago.
Improve this question
#include<studio.h>
int main()
{
int a=5,i;
i!=a>10;
printf("i=%d",i);
return 0;
}
This code should print i=1 but it is printing i=0 Why is it so?
That's because you don't do anything to i.
Your "i!=a>10" evaluates to false, but the result is not stored into a variable.
As it is mentioned in the comments, you need something like this:
int a = 5;
int i = !(a > 10);
The != is mostly used in if-clauses, like
if (a != 0) {...}
I hope this helps. ;)
It's not printing because:
the variable i is not initialized
the second statement should be changed and be a variable int i = !(a > 10)
Returning a value and printing it are different things, but that's not the point of the question.

Two same type of C codes giving different output . Need help understanding this [closed]

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 10 months ago.
Improve this question
int n;
scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 5.
n=scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 1.
Need help understanding the second one.
scanf returns the number of fields it assigns. So, first, it assigns n as 5. Then it returns 1 as it has assigned to only 1 variable. Then, n takes that value.
That's not the correct way to use scanf(). In C standards scanf() always returns the number of conversion it did from stdin, in your case only 1 conversion is done, hence 1 is assigned to n.
In C, if you want to modify a parameter's value inside a function, you need to pass the address of the variable (&var) to that function, and the parameter of the function should be a pointer (type *). Then, you need to de-reference the pointer like (*ptr).
Now, we need to change the value of n inside scanf() function, that's why we need to give the address of n.

Loop iteration in c [closed]

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 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i,x=10;
for(i=0;i<7;i++);
{
x++;
}
printf("%d",x);
}
Output : 11
No matter how many times the for loop iterates, the value of x stays 11. Why is that ?
Remove the semicolon from here:
for(i=0;i<7;i++);
The semicolon makes the for loop have an empty body. It makes it equivalent to
for(i=0;i<7;i++){}
Including warning flags in your compiler(-Wextra in GCC) emits a warning about these kind of issues.
Semicolon (; ) punctuation mark in C means that block of code is finished. That means if you use
for(i=0;i<7;i++);
{
x++;
}
For loop ends before it reaches brackets. Then code between your brackets runs like normal lines out of loop. If you want your loop to include brackets, get rid of the semicolon, like:
for(i=0;i<7;i++)
{
x++;
}

C - Nested For loop never stops [closed]

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 9 years ago.
Improve this question
I wrote a simple nested For loop, but for some reason it never stops.
#include <stdio.h>
int main (void) {
int x,y;
for (x=10;x<100;x+=10) {
for (y=10;y<100;x+=10)
printf("x is %d \n",x);
printf("y is %d \n",y);
}
return 0;
}
I'm new at c, but from examples I read, it should stop when x and y gets to 100. But for some reason it keeps on going for ever.
You need to increment y, not x
for (y=10;y<100;y+=10)
Also, it really looks like you meant to put braces around the inner loop
for (y=10;y<100;y+=10)
{ // <-- Did you mean to leave this out?
printf("x is %d \n",x);
printf("y is %d \n",y);
} // <-- and this?
Because y is not updated in the loop, which will make the second for loop always true (i.e. run forever).
You need to change
for (y=10;y<100;x+=10)
^
should be y here
to
for (y=10;y<100;y+=10)

Resources