compiled program not executing, just has blinking cursor - c

trying to get this loop to run but it just has the cursor blinking. I am new to programing. Can you please help?
#include<stdio.h>
int main()
{
int i;
i = 0
while (i <= 5);
{
printf ( "i = %d" );
i += 1;
}
}

Notice how there was also a missing semicolon after i = 0, this will throw an error and prevent your program from continuing. Also, remove the semicolon after your while loop, it will not run the code inside with it present (And will just sit idle after skipping it). Your program should look like:
#include<stdio.h>
int main()
{
int i;
i = 0;
while (i <= 5)
{
printf ( "i = %d" );
i += 1;
}
}

adding to the answer of Cyral, a semicolon indicates the end of a statement. if you have
while (i <= 5);
{
printf ( "i = %d" );
i += 1;
}
the program will run statement by statement, so it will first run the while, doing nothing in every loop because it is a while with a blank instruction ;. then it will run the next statement, that is everything inside the { }.
the blinking cursor occurs because you increment i inside the statement that follows the while, so, the while, expecting an i thats grater than 5 to stop (an i that never increments inside the loop) enters in an infinite loop.

Related

top level `continue` in nested loop C instead of current level loop

How do I make continue goes to the top level of loop? I mean like this:
for(;;){ // top level loop
statement1;
for(;;){ // second level loop
statement2;
if (condition1){
continue; // I expect it continue to the start of top level so it will execute statement1.
}
}
statement3;
}
This is my real case why I got problem above, basically my program is sending data with UDP socket which the data should be sent fast. I know UDP is unrealiable but it's okay the data is tolerated with loss too.
for (;;) { // Streaming data...
camera_fb_t* fb = esp_camera_fb_get();
size_t quotient = fb ->len / UDP_BUF_SIZE;
size_t remainder = fb ->len % UDP_BUF_SIZE;
unsigned int i = 0;
for (; i < quotient; i++) { // sending packet by packet.
if (uwc_udp_send_raw((const void*)(fb ->buf + (i * UDP_BUF_SIZE)),
UDP_BUF_SIZE) < 0) {
ESP_LOGE(uwc_tag_event, "Error in itteration: %i", i);
uwc_udp_send_raw("ERR",3); // Tell receiver there was corrupt during send data.
esp_camera_fb_return(fb);
continue; // I expect it continue to the top level
}
}
if (remainder) { // last packet to be sent if remainder exist
uwc_udp_send_raw((const void*)(fb ->buf + (i * UDP_BUF_SIZE)),
remainder);
ESP_LOGE(uwc_tag_event, "Error in last itteration!");
}
esp_camera_fb_return(fb);
}
This is a rare case where you may use a goto. * Ducks for cover expecting flame-war and down votes *
People will get excited and declare your code automatically unreadable by a single use of goto and generally wring their hands.
But in these very rare cases the cleanest and most readable code has a goto. There are exceptions to almost every rule!
Remember, there is almost always a better way than using goto but the real issue with it is that liberal use quickly creates 'spaghetti' code.
But a single use can be justified.
#include <stdio.h>
int foo(){
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
if(j==i*i){
goto OUTER_LOOP;
}
}
printf("%d\n",i);
OUTER_LOOP: continue;
}
}
int main(void) {
foo();
return 0;
}
People will claim you should set a boolean and break and insist that is 'more readable'. It's no more readable at all.
There are few dogmatic laws in programming but "goto is always an necessarily bad" is one of them. It's almost true. But not quite entirely true.
Java has a labelled break statement almost specifically to provide a way of avoiding this use-case for goto and doesn't have goto itself because it has a fix.
I think it's arguable that there is a tiny gap in the language. But in practice this situation is actually quite rare.
You don't need a flag or continue or goto:
for (;;) {
camera_fb_t *uwcCamFb = esp_camera_fb_get();
unsigned char *from = uwcCamFb->buf;
size_t toSend = uwcCamFb->len;
for( int i = 0; toSend > 0; i++ ) {
size_t sendSize = toSend > UDP_BUF_SIZE ? UDP_BUF_SIZE : toSend;
if( uwc_udp_send_raw( from, sendSize ) < 0) {
ESP_LOGE( uwc_tag_event, "Error in itteration: %i", i );
uwc_udp_send_raw( "ERR", 3 );
break;
}
toSend -= sendSize;
from += sendSize;
}
// Update...
// Removed conditional as it reflected flawed OP logic...
// if( toSend == 0 )
esp_camera_fb_return( uwcCamFb );
}
If you need/want to distinguish the LAST packet, add if( toSend < UDP_BUF_SIZE ) to log that particular error message instead...
It seems your OP used both fb and uwcCamFb... I can only guess this corrects that apparently mistake...
(Thank you to #Persixty for a bug report that has been fixed.)
I think goto can solve this problem, I would not use it in normal situation but this is acceptable I think, define a label in the top level for and use goto label in the inner one :]
It's up to you if you want a boolean and check it in every inner loop or use a goto though.
Edit: Honestly 2 for loop are not much, maybe a simple if condition is enough
You need flags:
for(int i = 0; i < 15; i ++)
{
int execST3 = 1;
printf("ST1 i = %d\n", i);
for(int j = 0; j < 15; j ++)
{
printf("ST2 j = %d\n", j);
if(j == 3) {execST3 = 0; break;}
}
if(execST3)printf("ST3\n");
}
The code can be made to work cleanly by replacing the inner infinite loop with a goto style infinite loop. From the compiler point of view that eliminates the second for loop, so that the continue, continues at the first infinite loop.
for (;;)
{
statement1;
INNER_LOOP:;
{
statement2;
if (condition1)
{
continue; // at the top of the first loop.
}
goto INNER_LOOP;
}
statement3;
}

What's the purpose of the `sut` variable in this code? Why doesn't the second snippet without it work?

This function prints a rectangle of X characters:
void kutu_ciz( int line, int column ) {
int sut;
for ( ; line > 0; line--) {
for (sut = column; sut > 0; sut--)
printf("X");
printf("\n");
}
}
This version without sut doesn't work. Why not? What's the difference?
void kutu_ciz( int line, int column ) {
for ( ; line > 0; line--) {
for (; column > 0; column--)
printf("X");
printf("\n");
}
}
The second version "looks" good because it has pleasing symmetry in the two for loops. It doesn't work, though, because after the inner loop runs once the column variable ends up set to 0. The next time the inner loop runs column has lost its initial value and is permanently 0. This causes only the first row of the rectangle to print and the rest to be blank.

Stuck in a nested do { for { if loop

So with this bit of code the program hangs up and won't exit the loop, the printfs were just put in for debugging and they aren't integral to the program. I am pretty new to programming so I am not sure what I am missing the logic seems like it should work. Thank you very much for taking the time to look over this and your help.
do
{
intialcollide = 0;
for(i=0; i<11; i++)
{
if(i != currentObj)
{
if(object[currentObj].new_loctX == object[i].new_loctX && object[currentObj].new_loctY == object[i].new_loctY)
{
intialcollide = 1;
}
else
{
intialcollide = 0;
}
}
printf("%d\n", intialcollide);
}
}while(intialcollide != 1 || i != 10);
printf("Collide? %d", intialcollide);
When I run it I get infinite 1's and 0's. Thanks again for the help
At the end of your for loop, i will always equal 11. Maybe you thought it would equal 10? Anyways, there is no point comparing i to anything in the while condition because you know it is always the same value.
Add a "break;" whenever you set intialcollide to 1. Your for loop is resetting the value to 0 before it hits the while loop check.

adding { - } in a for loop : c programming

the program runs fine and clear, but i just wanted to ask why didnt we add { - } in the beggining and end of the for loop ? the program runs fine without adding them but when i tried to add { - } in the for loop the prgram didnt run fine, arent we suppose to add { - } in ever begging and end of every loop ? and why did the program run fine without them and didnt run fine with them ?
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
Without braces it takes ONE statement as its scope
like
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
Equivalent of
for ( c = 0 ; c < n ; c++ )
{
scanf("%d",&array[c]);
}
A loop executes the statement immediately after it in a loop. What is a statement? It can be:
A piece of code ended with ;.
A block of code started with { and ended with }.
Your for-loop
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
uses the first definition of statement, but you could also write it as
for ( c = 0 ; c < n ; c++ )
{
scanf("%d",&array[c]);
}
and it should work fine.
Informally: The for-loop has a one-statement body. To use multiple statements, you nest them in a block-statement.
If there is only one statement in the block the braces are optional

Does For-Loop counter stay?

Simple Question. Imagine this in ANSI-C:
int i;
for(i=0 ; i<5 ; i++){
//Something...
}
printf("i is %d\n", i);
Will this output "i is 5" ?
Is i preserved or is the value of i undefined after the loop?
Yes. If i is declared outside of the for loop it remains in scope after the loop exits. It retains whatever value it had at the point the loop exited.
If you declatred I in the loop:
for (int i = 0 ; i < 5 ; i++)
{
}
Then i is undefined after the loop exit.
Variable i is defined outside of the scope of the loop (which is great, or you wouldn't be able to print it in that case).
And it is post-icnremented for every-turn of the loop, for which the end condition is "stop when i is bigger or equal to 5".
So it really makes perfect sense for i to be equal to 5 at this point.
A block scope is not exactly the same as a function scope in C. The variable i doesn't "get back" magically to its previous value when you step out of the loop's scope.
i's value will be 5 after your loop. Unless you did something like
i = 50000;
inside of it.
It's also generally recommended against using "i" after you exit the loop in most coding standards I have ever read. In particular do NOT do:
for(i = 0; i < num_elements; i++)
{
if(element[i].id == id)
{
/* Do something to element here. */
break;
}
}
if(i == num_elements)
{
fprintf(stderr, "Failed to find element %d.", id);
succeeded == false;
}
While this will work it is poor coding. It is less readable and maintainable than the alternatives. E.g.
succeeded = false;
for(i = 0; i < num_elements; i++)
{
if(element[i].id == id)
{
/* Do something to element here. */
succeeded = true;
break;
}
}
if(false == succeeded)
{
fprintf(stderr, "Failed to find element %d.", id);
}
Yes, variables are valid only inside the block in which they are declared.
Here's an example:
#include <stdio.h>
void main(int argc, char *argv[])
{
if(argc == 2) {
int x;
x = 7;
}
x = 1;
}
That's the compiler:
gcc ex.c
ex.c: In function ‘main’:
ex.c:10: error: ‘x’ undeclared (first use in this function)
ex.c:10: error: (Each undeclared identifier is reported only once
ex.c:10: error: for each function it appears in.)

Resources