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

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
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
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
How to stop a while (true) command in C?
This is a potential infinite loop since boolean true never changes value. When using a while loop always use a proper conditional check for example while(n<8) to have a defined condition when the loop terminates. Ex while (n<10) {if (n > 8) {printf("hi");} n++ } More on reddit.com
๐ŸŒ r/learnprogramming
20
3
May 27, 2022
๐ŸŒ
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.

๐ŸŒ
NAO IT Systems LLC.
digibeatrix.com โ€บ home โ€บ control structures โ€บ c while loops & conditions: basics, tips, and common errors
C While Loops & Conditions: Basics, Tips, and Common Errors - C Programming for System Development
September 18, 2025 - || (OR): The overall expression is true if either condition is true. ! (NOT): Inverts the truth value of a condition (true โ†’ false, false โ†’ true). When looping while a is at least 1 and b is at most 10
๐ŸŒ
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
๐ŸŒ
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
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ while loop in c
While Loop in C: Differences, Infinite Loop, Examples
May 20, 2025 - The following table summarizes the differences between while loops and do while loops in C: Each loop type has its own advantages and disadvantages, depending on the specific programming situation: ... Suitable for situations where the condition must be true before executing the loop body. Can help avoid infinite loops if proper care is taken to ensure that the condition becomes false at some point. Easy to understand and implement, with a straightforward syntax. ... Not suitable for situations where the loop body should be executed at least once, regardless of the initial condition.
๐ŸŒ
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)
August 10, 2023 -

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;

}

๐ŸŒ
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
๐ŸŒ
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.