LinuxQuestions.org
Visit Jeremy's Blog.
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 01-08-2023, 10:21 PM   #16
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 952

Original Poster
Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325

Indeed. I made that change. I spent the day doing a few other calculators as well. Also learned to how to include another .c file so I've got a mylibs.c started which 3 of my little programs currently include. Just one function at the moment. I've managed to build these other ones with zero output from
Code:
gcc -Wall -O1 -o $file $file.c
That one eludes me though, the original calc.c. It just tosses a single issue that I just cannot sort out. It works, more of a notification from that gcc command than anything I guess. Worked on that one quite a bit today trying to isolate it.
 
Old 01-09-2023, 08:29 AM   #17
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,683

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
Quote:
Originally Posted by jmgibson1981 View Post
Indeed. I made that change. I spent the day doing a few other calculators as well. Also learned to how to include another .c file so I've got a mylibs.c
FYI, it's usually a bad idea to include .c files. Typically only .h files are included. For multiple .c files, you would usually compile each of them to .o files which are then linked together. This means that if you change only one .c file, you don't have to recompile the others. You can use a Makefile to manage that.

Quote:
That one eludes me though, the original calc.c. It just tosses a single issue that I just cannot sort out. It works, more of a notification from that gcc command than anything I guess. Worked on that one quite a bit today trying to isolate it.
Ah, I guess gcc doesn't do the inference you expect there. Anyway, I think the best way is to add a default: case into the switch statement, since again with a separate if you end up with two lists of legal operators (and also, gcc won't be confused about it).
 
Old 01-10-2023, 04:41 PM   #18
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 952

Original Poster
Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325
I found it. i'm not sure it's what you suggested, may be. I found somewhere that setting a variable inside braces keeps it in those braces, at least as far as the compiler output. It worked but it wasn't perfect. I've since simplified my calcs down to a single one that does float. I made a solution_check function and just call it after setting the solution in each operation instead of once for the entire function. Not a peep from
Code:
gcc -Wall -O1 -o $file $file.c
 
Old 01-10-2023, 05:16 PM   #19
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,683

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
Hmm, calling exit(0) like that looks kind of odd to me. What I had in mind was more like

Code:
switch (op) {
case '+': ...
case '-': ...
case '*': ...
case '/': ...

default:
   printf("invalid operator %c\n", op);
   exit(1);
}

print_the_answer(...);
 
Old 01-10-2023, 06:00 PM   #20
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 952

Original Poster
Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325
Indeed. that would be better. thank you much
 
Old 01-11-2023, 03:46 AM   #21
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,594

Rep: Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629
Quote:
Originally Posted by ntubski View Post
Hmm, calling exit(0) like that looks kind of odd to me. What I had in mind was more like

Code:
switch (op) {
case '+': ...
case '-': ...
case '*': ...
case '/': ...

default:
   printf("invalid operator %c\n", op);
   exit(1);
}

print_the_answer(...);
you are right, theoretically we need only return something at the end of the main, calling exit in the middle is not really nice.

To OP:
if you have more than one .c files you can write a small makefile to build your executable, in that case you only need to run make (to make it).
https://www.gnu.org/software/make/ma...imple-Makefile
 
Old 01-11-2023, 03:19 PM   #22
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,102
Blog Entries: 4

Rep: Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662
I can accept this use of "exit(1)" because this is an exception. The program cannot complete and produce an answer. Therefore it exits, returning a "nonzero return code" as a well-understood indication that "something went wrong."

Last edited by sundialsvcs; 01-11-2023 at 03:20 PM.
 
Old 01-11-2023, 03:22 PM   #23
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,594

Rep: Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629
Quote:
Originally Posted by sundialsvcs View Post
I can accept this use of "exit(1)" because this is an exception. The program cannot complete and produce an answer. Therefore it exits, returning a "nonzero return code" as a well-understood indication that "something went wrong."
in this case yes, in a real program you need to do some cleanup (for example). But that is another topic.
 
Old 01-11-2023, 04:56 PM   #24
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,683

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
Quote:
Originally Posted by sundialsvcs View Post
I can accept this use of "exit(1)" because this is an exception.
Yes, I was talking about exit(0) as seen in https://gitlab.com/jmgibson1981/mycp...r/calculator.c

Code:
		case '+': // addition working
			solution = num1 + num2;
			solution_check(solution);
		case '-': // subtraction working
			solution = num1 - num2;
			solution_check(solution);
[...]

// verify solution exists and exit 0. 1 if fail

void solution_check(float solution) {
	if (solution) {
		printf("answer is %.3f\n", solution);
		exit(0);
	} else {
		printf("unable to complete calculation\n");
		exit(1);
	}
}
 
Old 01-11-2023, 05:35 PM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
So `5 - 5` would be problematic?
 
Old 01-11-2023, 06:24 PM   #26
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,102
Blog Entries: 4

Rep: Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662Reputation: 3662
Quote:
Originally Posted by pan64
... But that is another topic.
Yes. We need to be sure that we don't scare him off.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Building a new computer, looking for suggestions and build critique largedon Linux - Hardware 17 03-20-2013 02:03 PM
Teaching myself C++ Amdx2_x64 Programming 28 07-21-2008 02:08 PM
Teaching myself Linux. Hit a road block dcj1978 Programming 5 08-06-2007 06:33 AM
teaching active directory w/o teaching network concept (possible???) Tafta General 4 01-21-2004 07:12 PM
teaching myself perl -- need feedback farhanali Programming 2 06-28-2003 12:36 PM

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

All times are GMT -5. The time now is 11:47 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