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.
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 last month.
Improve this question
I'm trying a very simple programm where whatever is written in test.txt gets copied in up.txt but in capital letters. I'm using dev c++ on windows 11 and after running the programm the up.txt file is created but it is empty and i can't figure out why.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
FILE *fpin, *fpout;
char x;
fpin=fopen("test.txt","r");
if(fpin==NULL){
fprintf(stderr,"read error\n");
exit(666);
}
fpout=fopen("up.txt","w");
if(fpout=NULL){
fprintf(stderr,"write error/n");
exit(667);
}
while((x=fgetc(fpin))!=EOF){
fputc(toupper(x),fpout);
}
fclose(fpin);
fclose(fpout);
return 0;
}
I tried the same programm on linux succesfully but i'm not sure why it doesn't work on windows
There is no way this code can work on any platform. Are you sure the code you ran on Linux is actually this exact same code you show in the question?
The main problem is here:
fpout=fopen("up.txt","w");
// up.txt is now created
if (fpout = NULL) { // you're using an assignment here (=) instead
// of the == operator
fprintf(stderr,"write error/n");
exit(667);
}
// fpout is NULL now
if (fpout = NULL) is not a comparision, but it simply assigns NULL to fout. As the result is false (which is more or less the same thing as NULL), fprintf(stderr,"write error/n");exit(667) will not be executed and the program just continues.
Then in the while loop that follows, you're writing into a NULL FILE pointer, which is undefined behaviour, most likely the program will crashes or it might just do nothing or just quit, or maybe something else). Look up "C undefined behaviour" on your favorite search engine.
There is another subtle bug: char x; should be int x; because fgetc returns an int, not a char.
Read following articles for more information:
Why does fgetc() return int instead of char?
confusion about int, char, and EOF in 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 1 year ago.
Improve this question
I have written a piece of c code which I intend to just print out the value of a 2d array
I'm very new to c so this answer might be really basic sorry.
my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.
heres my code
#include <stdio.h>
int main(void) {
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int i2 = 0;
printf("hello");
for(int i = 0; i > 4; i++){
i2 = i;
printf("hi %d", array1[i][i2]);
}
}
returned value
hello
i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.
Thanks.
There are a couple of things wrong in this piece of code. Firstly, the usage of the column index like that (i2 = i) will make you print elements at the same row and column index, like array1[0][0], array1[1][1], and you'll skip things like array1[0][1]. You'd have to fix this by using a second for loop inside the first one, incrementing a second index (check this for example).
The other wrong thing about this piece of code is the condition in the for loop: you want to keep iterating while i<3, not i>4. In that case, you never enter the for loop, since the condition is not met even at the first iteration.
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.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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'm a beginner in C programming, wrote a program for GCD in eclipse IDE as below:
#include <stdio.h>
int main()
{
int a, b, t;
scanf("%d,%d", &a, &b);
printf("GCD of %d, %d is", a, b);
if (a < b) {
t = a;
a = b;
b = t;
}
while (!(t==0) {
t = a;
a = b;
b = t % b;
}
printf(" %d",a);
}
When Build All option is used its shows Info: Nothing to build for g and when I try to run the program it shows Filename.exe has stopped working.
I have restarted IDE as well the system but same error crops up again.
Okay, since you have mentioned you are a beginner. let me explain.
This happens when your program crashes. It can be for various reasons. Normally you will have to use some debugging tool. But in this case, is simple as "Joachim Pileborg" has mentioned in the comments.
You have to think about what happens when 'b' becomes zero. You are trying to divide something by zero. That is not mathematically possible. That's why your program crashes.
This has to do with basic mathematics, not programming.
take a look