LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2019, 10:33 AM   #211
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019

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.
 
1 members found this post helpful.
Old 02-04-2019, 11:21 AM   #212
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
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
Code:
Calculate triangular numbers
numbers to calculate: 6
n= 2
n= 3
......
n= 7
how to read this? User is telling machine: calculate for me 6 triangular numbers second, third at the end seventh. Quite self-explanatory.
 
1 members found this post helpful.
Old 02-04-2019, 11:37 PM   #213
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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
 
Old 02-05-2019, 12:18 AM   #214
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by jsbjsb001 View Post
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!

Glad to see you sticking with it!
 
1 members found this post helpful.
Old 02-05-2019, 12:28 AM   #215
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by astrogeek View Post
...
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]
 
Old 02-05-2019, 12:44 AM   #216
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
...
Are you maybe thinking the outer loop, this, runs only once?
Quote:
Originally Posted by jsbjsb001 View Post
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?

Last edited by astrogeek; 02-05-2019 at 12:45 AM.
 
1 members found this post helpful.
Old 02-05-2019, 12:54 AM   #217
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by astrogeek View Post
...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" ?
 
1 members found this post helpful.
Old 02-05-2019, 03:56 AM   #218
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by jsbjsb001 View Post
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;
}
I fixed the indenting/formatting for you as it was driving me nuts! I've not changed any of the logic. Was that what you intended to write?

Now, what's wrong with that bit? (hint: when will it run?)
 
1 members found this post helpful.
Old 02-05-2019, 04:44 AM   #219
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Yes, that's what I intended to write. But no, the if statement won't run. I didn't even notice that until you pointed it out GazL.

I'll figure out the indenting sooner or later.
 
Old 02-05-2019, 05:38 AM   #220
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Mhmm, test question: whats difference between
Code:
for (i = 0 ; i < MAX ; ++i)
and
Code:
for (i = 0 ; i < MAX ; i++)
and other: loops count up and count down, say
Code:
for ( done = 0 ; done < MAX ; ++done )
and
Code:
for ( to_do = MAX ;  to_do > 0 ; --to_do)
do they loop the same time?
 
1 members found this post helpful.
Old 02-05-2019, 06:32 AM   #221
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Regarding the code change you put in post #213, you do not need the if test.

Why?

The conditions in the while loop have been fixed, per my earlier comment. Great job.

Therefore no need to test it. That condition is now being tested by the loop.

You need only have the printf() and the increment statement, no if-else term is required.
 
1 members found this post helpful.
Old 02-05-2019, 07:02 AM   #222
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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;
}
 
Old 02-05-2019, 07:19 AM   #223
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by jsbjsb001 View Post
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.
 
1 members found this post helpful.
Old 02-05-2019, 07:23 AM   #224
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by jsbjsb001 View Post
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 View Post
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.
 
1 members found this post helpful.
Old 02-05-2019, 07:26 AM   #225
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Yeah, I tested it, it works as expected as far as I can tell, even tested it again, just to make sure - it works.

I'm still working on indenting the code - it's a work in progress...
 
  


Reply

Tags
c programming, learning c



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally decided to try to get libinput to work Timothy Miller Linux - Hardware 3 01-04-2018 08:04 PM
Decided to try Lubuntu 14.04 on my netbook... pcninja Ubuntu 4 04-20-2014 08:18 PM
Finally decided to get serious & learn a302svt LinuxQuestions.org Member Intro 1 07-19-2007 12:27 PM
Decided to try Debian some guidance required ninadb Debian 2 08-20-2004 11:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration