ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
From the research I've done all I've found is that %i and %d are synonymous when used in printf(), there is a difference in scanf() where %i will convert from octal or hex notation automatically while %d only converts from decimal, but I didn't see anything relating to the bitsize, just that both are "signed ints".
The %p sounds useful, but afaict %i seens entirely cosmetic.
You stick to much to what is written instead of what does it mean. Eg. first for loop tells how many times some task should be done. However the second for loop is how to count these numbers. There are task to be done by machine - and ways they can be done. Some of these task are repetitive, for some order is important, for other is unimportant - some of them are exclusive. So essentially there are needed at least two languages: one to describe how to achieve these tasks, second to describe the way they can be executed. But there is only one language: C. The word about user interaction. These are strange times people thinks machine can talk - or we can talk with machine. I mean machine is mimicking a person: how are you? what can I do for you? etc. But no. Machine is doing what it is required to do. User is issuing command, user expects that machine does what is expected to do : calculate triangular number, add 3+4, so if there is no choices, options - instead of questions there should command interpreter. Eg. here what I am thinking
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Thanks guys.
Thanks for the explanation of the for loop igadoter.
Thanks again for your help RT, I've modified the while loop I posted before, and included the "upper limit" in the while statement itself, but I've also modified the if statement, so once the loop reaches 50, it stops. I do want to learn about debugging, but I just wanted to try and follow the C book in the order the chapters appear in, and I haven't gotten up to debugging yet. But once I get to debugging, I do plan on debugging what I've done so far, to (as you say) see exactly what's going on in the little programs I've written so far. Also, your blog will make more sense to me then as well, so it will be easier for me to follow.
astrogeek, for that little program you wrote me here, that I've called "bases", did you use the for loop because you only wanted the loop to run once?
Here's my latest code for the while loop;
Code:
#include <stdio.h>
int main(void)
{
int enteredNumber;
printf("Enter a number: \n");
scanf("%i", &enteredNumber);
while ( enteredNumber >= 10 && enteredNumber <= 50 ) {
if ( enteredNumber >= 50 )
break;
else
printf("%i\n", enteredNumber);
++enteredNumber;
}
return 0;
}
Last edited by jsbjsb001; 02-04-2019 at 11:39 PM.
Reason: corrected code
astrogeek, for that little program you wrote me here, that I've called "bases", did you use the for loop because you only wanted the loop to run once?
If you mean this part...
Code:
for(pow2=1;;pow2 *= 2)
if(pow2 >= x)
break;
That runs more than once unless x<2. What it does for us is finds the lowest power of two which contains x, which we then use to print out the binary digits of the value of x in highest to lowest, left to right order.
Are you maybe thinking the outer loop, this, runs only once?
Code:
while(1){
...
}
With regard to your loop writing, that is progress! But what is missing (unless I have missed it) is an explanation of what you actually want your loop to do - the specification of your loop. Something like "I want to enter a number in the range of... and I want the number entered to be printed this many times... and when a number outside that range is entered I want it to...".
It is good to practice by writing loops, but mostly meaningful only if you are are writing your loops and decision statements to meet some desired behavior. Only then can you, or others say it is or is not working as "correctly".
So set yourself a clear but simple loop specification, then try to make a simple program that works as specified - the bare essence of programming!
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Quote:
Originally Posted by astrogeek
...
Are you maybe thinking the outer loop, this, runs only once?
Yeah sorry, I got confused. Thanks for explaining it to me.
Quote:
With regard to your loop writing, that is progress! But what is missing (unless I have missed it) is an explanation of what you actually want your loop to do - the specification of your loop. Something like "I want to enter a number in the range of... and I want the number entered to be printed this many times... and when a number outside that range is entered I want it to...".
Yeah sorry once again, I was going to write what it's supposed to do, but I completely forgot to.
Anyway, the idea behind it was so that you can only enter 10 or more, but no more than 49, and if you go outside of that range, it just quits, and won't run the loop, and therefore returns to the shell. And if you do enter a number within that range it will count from that number to 50. Although, I wouldn't mind seeing if I can get it to count from say "0", regardless of which number you enter - as long as it's within range of course (being 10 - 50).
Thanks again for your help astrogeek!
Last edited by jsbjsb001; 02-05-2019 at 12:30 AM.
Reason: fixed first quote - forgot [/quote]
...
Are you maybe thinking the outer loop, this, runs only once?
Quote:
Originally Posted by jsbjsb001
Yeah sorry, I got confused. Thanks for explaining it to me.
In that case, that is not correct, the outer loop runs forever, until some other condition interrupts it!
Code:
while(1){
...
}
This tells the while loop to run as long as the condition in parenthesis evaluates as boolean true, which the digit '1' does. This is a common method of writing a loop which runs forever, usually until some condition inside the loop breaks out of it. Can you tell from the code posted there what condition breaks the while loop?
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Quote:
Originally Posted by astrogeek
...Can you tell from the code posted there what condition breaks the while loop?
It looks like for the first while loop it's if variable "x" is equal to "0". And for the for loop, it's if variable "pow2" is equal to, or more than variable "x" ?
igadoter, I'll get back to ya about the results of your test in post #220.
Thanks again RT, I've got rid of the if-else statement, and posted the modified version below;
Code:
#include <stdio.h>
int main(void)
{
int enteredNumber;
printf("Enter a number: \n");
scanf("%i", &enteredNumber);
while ( enteredNumber >= 10 && enteredNumber <= 50 ) {
printf("%i\n", enteredNumber);
++enteredNumber;
}
return 0;
}
Just a quick note that it would be advisable for you to use consistent indenting. I've adjusted your post above accordingly - compare that with your original to see the differences.
igadoter, I'll get back to ya about the results of your test in post #220.
No disrespect to igadoter, however I would not pursue those questions at this time. You are having trouble with simple syntax. They are expanding the topic, to make you think, and that's fine. I'm not sure it will be helpful right now for you.
Quote:
Originally Posted by jsbjsb001
I've got rid of the if-else statement, and posted the modified version below
So ... you've done exactly what you were told and then stopped?
Did you compile it?
Did you run it?!?!!
Unfortunately without noticing sooner, I overstepped GazL's reply where they hinted at what was wrong, and I just told you exactly what was wrong.
You are still making fundamental mistakes, and yes you are still improperly indenting your code.
Partly these are small things, however if you wish to write much larger, more complicated code, then you really do need to develop coding standards and be uniform with what you do when you code.
One of the primary reasons for these criticisms and repeated points are because the C language syntax does allow you to do a very wide variety of things in different manners. Such as igadoter's post. I feel that is too advanced for you right now. One danger is that people tend to do down the path of, "Hey look! I wrote one line of code that does all these things, a loop, an if-test, prints stuff, calculates payroll, and computes GPS coordinates. Is that awesome, or what?" The answer, my answer, is that it usually is 100% correct code, but also horribly written code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.