Remove the ampersand from

printf("Those integers sum to %1d.\n", &sum);

it should be

printf("Those integers sum to %ld.\n", sum);

Edit As others pointed out, the one in %1d is a typo, and it causes your scanf to read a one-digit int value. Replace it with %ld ('ell' for 'long'). Similary in printf the 'l' modifier tells the function what size of int it got to print.

Answer from CiaPan on Stack Overflow
๐ŸŒ
C2
wiki.c2.com
While Not Done Loop
This site uses features not available in older browsers.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 40335233 โ€บ true-or-false-in-c-programming-pertaining-to-while-loop
True or False in c programming pertaining to while loop - Stack Overflow
A while loop in C evaluates whatever is in it as either true or false, before every loop. One of the guarantees you get when programming in C is that only 0 is false, and everything else is true. That means that foobar is true, 64 is true, -1 is true, and in your case, 1 is true. Because of this, the ! operator (the not symbol), it changes anything that is true into a 0, and changes a 0 into a 1.
Discussions

While loop in C exited even when the condition is not true - Stack Overflow
I hope I don't get downvoted for ... loops in C, the usage of scanf() and variable assignments to see if I've missed anything but nothing explains why this code below runs even when the check on the variable status for the while loop is not true anymore. This program was taken from a text book but it doesn't explain why it works either it's just an example... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Is using a while block to do nothing a bad thing? - Stack Overflow
My solution feels a bit messy, as it seems bit hackish to have a while loop with nothing between the curly braces. I was wondering if there are any good reasons not to do this? Thanks for any advice :-) ... You need to worry about what happens if the inner while loop encounters EOF. More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - While loop using a variable with no condition - Stack Overflow
Do you mean number = 0 or number == 0? Note that one operator is the assignment operator, and the other is the equality operator. ... Somewhere in the loop there is code that makes it meaningful to write the loop that way. Show actual code in this form. Your stripped-down example is inadequate. ... while ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
I really don't get while, do-while, and for loops in C.
You're asking your question really abstractly so I'm having a hard time coming up with an answer but I'll give it a shot. Loops are used because you want to repeat an action while a condition is true or you want to repeat it on a certain number of things or a certain number of times. The answer to "how do you know how many times to repeat" isn't something you can really come up with without knowing what problem you're trying to solve. Say you want a user to input yes or no in response to a question. The user could also enter in "applesauce" or "bumblebee" because it's free text and they can type whatever they want. In that case you might use a while loop. While the user hasn't responded yes or no, you ask them again. You don't know how many times the user is going to put in the wrong answer. They could do it infinitely. Your while loop will diligently keep sending them the prompt again until the condition of them answering "yes" or "no" is satisfied. Maybe this doesn't answer your question though. Do you have a specific problem you were given where you were confused as to how many times to loop or how to write the condition? More on reddit.com
๐ŸŒ r/learnprogramming
94
67
November 27, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-while-loop
while Loop in C - GeeksforGeeks
The while loop body will be executed if and only the test condition defined in the conditional statement is true. Body: It is the actual set of statements that will be executed till the specified condition is true. Updation: It is an expression that updates the value of the loop variable in each iteration. It is also not ...
Published ย  October 8, 2025
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ i really don't get while, do-while, and for loops in c.
r/learnprogramming on Reddit: I really don't get while, do-while, and for loops in C.
November 27, 2023 -

I can't seem to understand how can people come up what number they specifically need in the condition part. I have watched a couple of yt tutorials but they include topics that we have yet to discuss, they kept mentioning arrays but we haven't discussed that yet.

For context: I'm a freshman in college majoring in computer science. I have a major subject called "Fundamentals of Programming," where we're using the C language. We've discussed basic input and output, variables, data types, if statements, switch statements, etc. Currently, we're covering loop statements, and our next topics will be arrays and functions.

Do programmers follow a pattern in determining what equation to put in the condition part in for loops? How do they easily come up with it? How do they know what to put?

Top answer
1 of 5
74
You're asking your question really abstractly so I'm having a hard time coming up with an answer but I'll give it a shot. Loops are used because you want to repeat an action while a condition is true or you want to repeat it on a certain number of things or a certain number of times. The answer to "how do you know how many times to repeat" isn't something you can really come up with without knowing what problem you're trying to solve. Say you want a user to input yes or no in response to a question. The user could also enter in "applesauce" or "bumblebee" because it's free text and they can type whatever they want. In that case you might use a while loop. While the user hasn't responded yes or no, you ask them again. You don't know how many times the user is going to put in the wrong answer. They could do it infinitely. Your while loop will diligently keep sending them the prompt again until the condition of them answering "yes" or "no" is satisfied. Maybe this doesn't answer your question though. Do you have a specific problem you were given where you were confused as to how many times to loop or how to write the condition?
2 of 5
48
While there are some good answers here, nobody has touched on what I suspect is the reason that you're confused yet. For loops When we teach loops, we often use examples where we have a defined number of times we want to loop. These examples are often arbitrary; we essentially pick a number out of a hat and just use that. You'll often see code like: for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { printf("0"); } printf("\n") } Which prints out: 000 000 000 In real code, there may be no way for us as a programmer to know exactly how many times out program needs to loop, because that number might change depending on different factors (input from the user, settings, input from a file, etc.) The key here is that anywhere we can use a hardcoded number to define our end condition, or the number of times we want to loop, we can instead use a variable. This means as long as the code itself can calculate a number before the loop starts, the code will work exactly the same as if we'd hardcoded a number in there, and we the programmer don't ever need to know what number it came up with. The condition can also technically not even involve numeric comparison, although that is significantly less common in for loops. We can change that example code by replacing the two instances of the harcoded 3 with a variable. Suddenly whatever we set that variable to will control what size of square we build out of zeros. We could then ask the user to tell us how what size the square should be, and use that to control the loops. Taking it a step further to show what I mean about calculating things, we could instead ask the user to tell us the number of zeros in a square, and we will calculate the size of the square based on that input. We calculate the size of the square by finding the square root of the input number. In this example let's assume that any number that doesn't have an integer square root is ignored and the question asked again. This should hopefully give you a better sense of how using a variable and calculating how many times we need to loop works in more real world situations. Other kinds of loops Since while and do-while loops don't actually involve the mechanics of setting up a (possibly numeric) loop in the same way a for loop does, technically even the code doesn't need to be able to calculate how many times something needs to be repeated in order to solve the current problem. Like other people have commented already, code like this is very common: bool end_condition = false; while(!end_condition) { // do something until we're done end_condition = true; } This comes up when we don't know how many times we might need to loop, but we do have a clear way to know when we're supposed to be finished. For example, if we're searching through a list of items looking for the first item that has a specific value, or that matches some condition, this is often what that code more or less looks like. Interactive programs (programs with a GUI, games, anything that needs to be told by the user to exit) basically all have some version of this exact loop somewhere in their code. The only difference of the do-while loop is that it guarantees that the body of the loop will run once no matter what. Even if the exit condition is already true, it will still run once. The only time you should use a do-while loop in real code is when you absolutely must ensure that the code runs once. This is because in C/C++ the do-while loop places the condition at the end of the loop body. If your loop body becomes large, having the condition at the end of the loop can make reading that code much more difficult than it should be, because knowing the conditions of a loop is extremely important. Hopefully this helps you get a better understanding of how loops are used in more realistic conditions, and why you the programmer don't actually need to know an exact number to be able to write loops.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_while_loop.htm
C - While Loop
Since the expression that controls the loop is tested before the program enters the loop, the while loop is called the entry verified loop. Here, the key point to note is that a while loop might not execute at all if the condition is found to be not true at the very first instance itself.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to stop a while (true) command in c?
r/learnprogramming on Reddit: How to stop a while (true) command in C?
May 27, 2022 -

If I have a while (true) statement that has an if function in it like this:

while (true)

{

if (n > 8)

{

printf("hi");

}

}

When the if statement is false (n is smaller than 8), the code just does nothing, it seems like the code stays in the while true statement and keeps asking if the if statement is true or false. How do you make the while true command stop and go to the next line of code when it's finished?

Thank you.

๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ i have a confusion concerning a while loops behavior with "c = getchar() !=eof" as the condition.
r/C_Programming on Reddit: I have a confusion concerning a while loops behavior with "c = getchar() !=EOF" as the condition.
August 10, 2023 -

From K&R chapter 1

    #include <stdio.h>
    int main (void) 
    {
    int c;
    while ((c = getchar()) != EOF) 
        putchar(c);
        
    }    

I run the program from Windows command line and I can enter:

Input: 5

5

Input: god help me

god help me

intput: djkslf4o3

djkslf4o3

and so on.

The program does not terminate unless I press ctrl-z(followed by /n) or ctrl-c (using windows command line). I also read from another source that ctrl-z is actually "signalling EOF", causing getchar() to return -1 which is EOF's value (please fact check me).

I see this in the terminal and it contradicts my (obviously wrong) understanding of what exactly EOF is doing and what its purpose is.

In the case I type 45 and hit enter: I assume that after putchar(c) is called twice in the loop, EOF is returned by getchar(). The while loop's condition is now false. I cannot reconcile, my conceptualization of the necessity of EOF acting as the brakes in a senseโ€” I type 45 into the terminal and I get only 45 back and not 45 followed by an ongoing stream of random bytes

with

my understanding of the program as a whole where I would like to assert that after the while loop's condition evaluates to false, the program would terminate. But it doesn't terminate. Does this suggest that we haven't exited the loop and EOF is not being returned by getchar(), after reading my input 45? But then how does the putchar() know when to stop?

Is windows thinking ahead for me and running the program over again with every successive input into the terminal? Am I misunderstanding the while loop?

Also, if it is indeed the case "that '^z\n' (windows) is actually signalling EOF, causing getchar() to return -1", considering my observation that this terminates the program, this confuses me again when considering the former logic. If this does indeed terminate the loop by causing getchar() to return -1, is it not so that the loop also needs to be terminated for every successive input in order for the input x into the terminal, to only be output as x and not x followed by random garbage in memory?

Would greatly appreciate any help thanks.

Top answer
1 of 5
21
The behavior is confusing because the terminal is doing part of the job here. When you type a character, the terminal doesn't send it to your program immediately: it puts it into a buffer, and this buffer is only sent when you press enter. At that moment, there is a whole line of characters waiting to be read by getchar(), and your program reads them and prints them. But enter is not EOF, it's just enter, so after that the while loop is still running, waiting for more characters that it will only get when you press enter, and so on.
2 of 5
13
If I understand you right, I think your confusion is from the fact that you have the impression that getchar() has to return quickly. But it doesn't, and that's actually what happens. If there's no characters waiting to be sent to your program (and no EOF signaled, so some more could still be coming) then getchar() simply doesn't return until there's new information to report. In that way it's a "blocking" operation - it will stop your program's execution for an unknown amount of time until whatever condition it's waiting for finishes. The blocking aspect for getchar() is useful for simple programs like the one you wrote. Note how the program doesn't use 100% CPU (which a normal while() loop with no pausing would do). That's because when getchar() blocks, it does so in a way that doesn't require any CPU power, instead it basically tells the OS "don't run me again until there's more characters typed in" (where "me" is your process).
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 52007-when-while-loop-will-stop.html
when a while loop will stop ?
what is the condition for the while loop to be invalid ? i think the following are the condition for invalid while loop. 1. while(false ) // bool fals
๐ŸŒ
Quora
quora.com โ€บ Is-it-necessary-to-avoid-while-loops-in-C-C-What-are-some-situations-where-a-while-loop-may-be-preferred-over-a-for-loop-and-vice-versa
Is it necessary to avoid while loops in C/C++? What are some situations where a while loop may be preferred over a for loop, and vice versa? - Quora
Answer (1 of 3): โ€œIs it necessary to avoid while loops in C/C++? What are some situations where a while loop may be preferred over a for loop, and vice versa?โ€ The correct style of programming is to first get the program correct, and then think about optimising the code.
๐ŸŒ
Steve's Data Tips and Tricks
spsanderson.com โ€บ steveondata โ€บ posts โ€บ 2024-11-27
Mastering While and Do While Loops in C: A Beginnerโ€™s Guide โ€“ Steve's Data Tips and Tricks
In the example, the loop runs from 0 to 4 (inclusive), not 1 to 5. Adjust the condition accordingly based on your requirements. The do while loop is similar to the while loop, but with one key difference: the condition is checked at the end of each iteration instead of at the beginning.
๐ŸŒ
Cprogramming
cprogramming.com โ€บ tutorial โ€บ c โ€บ lesson3.html
For, While and Do While Loops in C - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ why is my while loop not working? (c language)
r/learnprogramming on Reddit: Why is my while loop not working? (C language)
July 25, 2022 -

I'm making a link list to rearrange some letters to point to each other in alphabetical order. When I run the code in Command prompt it does not show any errors however no output is given. I tested my code with some print statements to see where the error is and I saw that everything before the while condition gets executed. I also think I made mistakes in inserting strings into the nodes as well so that might also be a factor. Anyways can someone help me out, please?

#include <stdio.h>

#include <stdlib.h>

struct studentname

{

char letter;

struct studentname *next;

};

typedef struct studentname STUDENTName;

typedef STUDENTName *STUDENTNamePtr;

int main()

{

//creating 5 empty nodes

STUDENTNamePtr node1 = (STUDENTName\*)malloc(sizeof(STUDENTName));

STUDENTNamePtr node2 = (STUDENTName\*)malloc(sizeof(STUDENTName));

STUDENTNamePtr node3 = (STUDENTName\*)malloc(sizeof(STUDENTName));

STUDENTNamePtr node4 = (STUDENTName\*)malloc(sizeof(STUDENTName));

STUDENTNamePtr node5 = (STUDENTName\*)malloc(sizeof(STUDENTName));


//fill the values  ----> s,e,n,a,d


node1->letter = 's';

node1->next = NULL;



node2->letter = 'e';

node2->next = NULL;



node3->letter = 'n';

node3->next = NULL;



node4->letter = 'a';

node4->next = NULL;



node5->letter = 'd';

node5->next = NULL;


//linking ----> a,d,e,n,s


node4->next = node5;      // a -> d

node5->next = node2;      // d -> e

node2->next = node3;      // e -> n

node3->next = node1;      // n -> s

//printing

STUDENTNamePtr current;

current = node4;

while(current != NULL)

{

	printf("%s--->", current->letter);

	current = current->next;

}

printf("NULL\\n");
return 0;

}

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ difference-while1-while0-c-language
Difference between while(1) and while(0) in C language - GeeksforGeeks
November 8, 2022 - In the program, the server runs in an infinite while loop to receive the packets sent from the clients. But practically, it is not advisable to use while(1) in real-world because it increases the CPU usage and also blocks the code i.e one cannot come out from the while(1) until the program is closed manually.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-do-while-loop
do...while Loop in C - GeeksforGeeks
When the test condition is evaluated as false, the program controls move on to the next statements after the do...while loop. The initialization and updation is not a part of the do...while loop syntax.
Published ย  October 8, 2025