debugg not going inside the loop - c

I am practicing on chars and i wrote a while loop that ends whenver i see a ";"
however when i am trying to debug it doesnt go inside the loop only at the last loop and I dont understand why
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char str[] = "abccc;";
int i = 0;
while (str[i] != ';')
{
i++;
printf("%d \n",i);
}
}
thats the code, it works and realy simple but if I debugg to see it only goes inside the loop in the last time
I am not sure why and how to fix

I would suggest keep tracing even if it looks like its not going inside the loop. Sometimes the way the code is generated, the execution in the debugger is not linear. In fact as you keep tracing, keep a watch for output of the printf from the program.

Related

for loop in c is not incrementing on the final iteration of the loop

Hello and thank all who bothered to read. I am currently trying to execute a program in c that contains a function with this for loop.
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
int i, goal;
char resp[1];
printf("How many people would you like to ask?");
scanf("%d" ,&goal);
for(i = 0; i < goal; i++){
printf("-Person %d: Would you like to register to vote?\n",(i+1));
scanf("%s", resp);
if((strcmp(resp, "n"))==0){
printf("\nOk.\n");
}
else if((strcmp(resp, "y"))==0){
printf("\nRegistering...\n");
}
}
printf("\n%d people asked! Taking a break.\n", goal);
}
Yet for some reason, when I run the program, after pressing n for all of the runs, the for loop does not increment, leading to an infinite loop on the final run.
I have tried changing the for loop to a while loop with the same results and have also tried the y option for the last time which led to a whole host of other issues. In order to get the goal value I am using scanf though when I tried using fgets I still came into the same flawed result or worse. I ran this code into a debugger and when executing the program line by line it works fine, but running it normally seems to be the issue. Any help is appreciated and I am willing to clarify or expand on my code. This is my first post here so apologies if this has been asked 50 trillion times.

How does the infinite loop further down stop earlier code from execution?

I've come across this weird situation i can't wrap my head around. In the following code, the Program is apparantly enetering the infinite loop, but doesn't execute the code that comes before it.
#include <stdio.h>
#include <stdlib.h>
int main() {
char buf[100];
if (scanf("%s", buf)==EOF) return 0;
printf("This does not get printed");
while(1) {} //infinite loop
return 1;
}
Somehow, the printf command does not get executed, even after pressing enter or using Ctrl-d.
However, it seems as if the code would end up in the infinite loop.
Does anyone explain whats going on here? I'm using gcc.
Standard output is line buffered by default. If you write less than a line to the output, some or all of it can be delayed until more output occurs.

Printing several \t in one line

For some weird reason my little program over here doesn't show up the way I want to.The last \t before Quit for a weird reason doesn't do anything.
Can anyone explain me why?
#include <stdio.h>
int n,litera;
void main (void)
{ n=1;
printf("File\tEdit\tView\tCompile\tQuit\n");
printf("Selectati optiunea aleasa:");
}
There's nothing really weird about it. Since Compile is more than 4 letters long, it eats up three spaces into the next tab block. Hence the result. Use two \t\t otherwise.
Here's a Q&D solution:
#include <stdio.h>
#define TAB " "
int n,litera;
int main (void)
{ n=1;
printf("File"TAB"Edit"TAB"View"TAB"Compile"TAB"Quit\n");
printf("Selectati optiunea aleasa:");
return 0;
}

printf doesn't work

This C code is supposed to create some random numbers and print them and then sort them and print them again, but it just prints the sorted numbers. Could any body help me?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int i, j, k;
float temper;
time_t t;
float grades[1000];
fflush(stdout);
printf("Here are the number\n");
srand(time(&t));
for(i=0;i<1000;i++){
grades[i]=rand();
printf("%f\n", grades[i]);
}
for(i=0;i<1000;i++){
int swap=0;
for(j=i;j<1000;j++){
if(grades[i]>grades[j]){
temper=grades[i];
grades[i]=grades[j];
grades[j]=temper;
swap=1;
}
}
}
printf("sorting is done");
for(i=0;i<1000;i++){
printf("%f\n", grades[i]);
} }
Your program is working correctly. Try changing everything from 1000 to 10 just to test and see for yourself.
What is happening is that it is printing everything out so quickly that the first 1000 is off the page.
The code is correct.
Try a small size to array,
write all the logs to a file.
Code worked fine for me too. Maybe your terminal is not storing enough lines for you to see the beginning of the output. You can change that in the settings for your terminal. Or you can cat them to a file instead. There is an easy option to do so if you google it. Also, add in another printf in between as a marker that is obvious like:
printf("+++++++++++++++++++++++++++ here is the break point ++++++++++++++++");
It will make it that much harder to miss it. Good luck!
PS: to cat your output to a file simply type '> filename' when running the program. I called mine math.c so when I ran I typed:
'$./math > file'
And the whole output in in a file named 'file'

executing a program from another program in C

I am trying to launch a program from another program.
Here is the code below
Figure :1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int main()
{
printf("Before Execution \n");
system("c:\\Rasmi Personal\\PERSONAL\\C\\Code Block\\C_Test\\bin\\Debug\\C_Test.exe");
printf("\nAfter Execution \n");
return 0;
}
In c:\Rasmi Personal\PERSONAL\C\Code Block\C_Test\bin\Debug\C_Test project contains the code is
Figure 2:
#include <stdio.h>
int main()
{
int x = 10;
while( x --> 0 ) // x goes to 0
{
printf("%d\n", x);
} return 0;
}
But while executing the 1st program (Figure 1) the output comes as below.
Before Execution
'c:\Rasmi' is not recognized as an internal or external command,
operable program or batch file.
After Execution
Please help me in solving this.
PS:- I am using CODE::BLOCKS in Windows XP.
You're using path names with spaces in them. Everything gets more confusing when you do that, and you have to add quotes around the right things in the right places to get anything to work.
I recommend using path names without spaces in them.
If you still want to try to make this work with spaces in your path names, the following might do it:
system("\"c:\\Rasmi Personal\\PERSONAL\\C\\Code Block\\C_Test\\bin\\Debug\\C_Test.exe\"");

Resources