increment in while loop doesn't work? [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 5 years ago.
Improve this question
I am doing the following while loop, and the line number doesn't get incremented (it's always 0). Why is that?
int main(int argc, const char* argv[])
{
int line_number = 0;
int f = 0;
while (f == 0) {
printf("LINE NUMBER IS %f\n", line_number);
line_number++;
}
return EXIT_SUCCESS;
}
(I realize this is an infinite loop, but I am interested here in why line_number isn't getting incremented.)

With printf, it matters what letter you put after a % sign for variable replacement. If you look at the list of format specifiers, you’ll see that %f is for floating-point numbers. This means it thinks line_number is a float; it doesn’t do the proper conversion because it doesn’t realize it needs to, and instead just reads the same bits as a float.
The way the floating-point format works is complicated, but you’ll stay very close to zero for a very long time. Depending on how fast your computer is and how patient you are, you might or might not see it change if you leave it running for longer.
(Technically it’s even worse than this, as this is undefined behavior; however, this is the most likely outcome.)
Most compilers will have some option to warn you if you use the wrong format specifier; you should probably turn on the default set of compiler warnings so it can tell you about this and other issues.

Related

Variable in C not printing the given value [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 1 year ago.
Improve this question
Alright, so basically recently I started studying C as a hobby, and I wanted to create a small program.Everything works fine, but when I make the variable "Age" like this:
int myAge = "14";
printf("My age is%d\n", myAge);
It prints out 1642.
I have tried switching to
printf('My age is%i\n', age);
But it did the same.
I also tested changing the number to a string but it obviously failed, because this isn't Python.
Anybody can help?
Save time, enable all compiler warnings.
Perhaps receive a warning like:
warning: initialization of 'int' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
int myAge = "14"; sets myAge to the address of the string literal "14".
Instead, initialize with an int.
int myAge = 14;
This is a fixed version of your program. Just declaring the age variable as an integer is enough to fix it.
#include <stdio.h>
int main() {
int age = 14;
printf("My age is %d", age);
}
Friendly note:
C is not like Python. Arrays don't work the same, strings don't exist, and programming in C is fundamentally different in every way. I would advise that you definitely take a course rather than trying to teach yourself from scratch.
u should write int myAge = 14 , writting 14 in double quotes means its a string (thx HAL)
We use double quotes when we have string and single quote when we have character but 14 is an integer we should write int myAge=14; or if u want "14" u should write char myAge[2]="14"; then printf("%s",myAge);

C program execution success apparently depends on file name [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 3 years ago.
Improve this question
I tried to run a C source file and the result was it got stuck in an infinite loop. I literally copied and pasted the same code into a new source file, Untitled1, and it ran fine. Both the original and the new source file are saved on the desktop. Why is this happening?
#include <stdio.h>
int main()
{
int i, j, d, a;
scanf("%d %d", &i, &d);
printf("%d\n", i);
a = 1;
while(i>1)
{
i = i%d;
for(j = 1; j<=a; j++)
{
printf(" ");
}
printf("%d\n", i);
d = d/100;
a++;
}
//////////
return 0;
}
Just a simple exercise from CodesDope. The goal is to print
1010101
10101
101
1
Which you get by entering i=1010101 and d=1000000.
I cannot doubt your experience but I'm not sure your conclusion is correct. First we don't run source code, it has to be compiled first. This leaves open the possibility that you have an old executable, i.e. an executable that doesn't reflect the code. The same code compiled the same way should produce the same runtime behavior (given that that code logic is correct).
Since all the variables are integer the d variable can become 0 and if this happens before i becomes less than or equal to 1 the i%d would result in a divide by zero error. Trying your code on repl.it with i = 1000 and d = 77 generates a floating point exception, but different compilers/environments may surface that undefined state differently (though all should produce an error state).
My advice is to delete both your compiled executables and any object files (clean your project), then recompile and compare results. If you still see different behavior based on the same output, then carefully compare your source files (or 'diff' them if you are on a unix'y system). If you still find a discrepancy, update your question with both source files (even if you find them identical), the compiler (name/version) and environment (OS/version) you are using.

Where did i make a mistake in my program i think its a logical error but i cant quite find it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 have this program that I wrote, it complies but I don't think it outputs correctly. Did I make a mistake here?
Here's my program:
#include <stdio.h>
void main(void)
{
int loop_counter = -8;
int user_input = 9;
char c1 = '9';
char c2 = 43;
while(loop_counter != 21);
{
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
loop_counter = loop_counter + 1;
loop_counter++;
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
getchar();
}
printf("loop exit\n\n");
getchar();
}
The biggest problem is probably that you have not articulated what you are hoping the code will do or what you intended the code to do or what you think it does. I am assuming you just need some help so my attempt to do so is below.
Running this through the compiler and interpreting the error messages helps a little.
Right off the bat, the compiler doesn't like your call to main. The compiler offers a suggestion that will successfully fix this error, so just follow the advice (and don't forget to add a
return 0;
statement before you exit main.
Second warning the compiler generates is that you have a semicolon at the end of your while statement. It also tells you how to fix it. Follow the instructions and you should be able to generate an executable.
You will still, however, have problems at runtime. This gets back to what is your intent.
With errors above corrected, your loop_counter variable enters the while loop initialized to -8, increases by 1 on line 19 and again increases by 1 on line 20. A call is made to getchar() but no input is given. Also lines 7, 8, and 9 are not used by your program.
Hope this gives you some direction. :)
The biggest error is
while(loop_counter != 21);
where the trailing ; will make the loop infinite.

Why variable doesn't have value a outside loop? [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 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.

Receiving strange number from fscanf - int conversion? [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 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon

Resources