LinuxQuestions.org
Visit Jeremy's Blog.
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 04-16-2006, 07:48 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question int -- floor or ceiling? (C Programming)


When an int is divided by another int and stored, (int x = y/z: int x = 5/3) does it floor or ceiling, or does it depend on the outcome? Would x be 1 or 2?
 
Old 04-16-2006, 08:14 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Think of it as always chopping off the decimal point, so floor
 
Old 04-16-2006, 08:15 PM   #3
haymaker2000
Member
 
Registered: Jan 2006
Location: New Zealand
Distribution: Ubuntu 5.10 (Breezy), Debian 3.1 (Sarge)
Posts: 37

Rep: Reputation: 15
I could be wrong, but I think it floors. I'm actually quite sure about this, because if you do modulo arithmetic, you always get the remainder.
So, 6/4 would give you 1 remainder 2. The remainder can be calculated using 6%4 (=2). So, in general a%b = r (a,b integers).
 
Old 04-17-2006, 02:34 AM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Quoting from: http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html
Quote:
The integer division and modulus operators are defined to perform truncation towards zero. (In C89, it was implementation-defined whether truncation was done towards zero or -infinity. This is (obviously) important only if one or both operands are negative. Consider:

-22 / 7 = -3
-22 % 7 = -1

truncation towards zero

-22 / 7 = -4
-22 % 7 = 6

truncation towards -infinity

Both satisfy the required equation (a/b)*b + a%b == a. The second has the advantage that the modulus is always positive -- but they decided on the other (more Fortran-like, less Pascal-like) variant...)
 
  


Reply

Tags
division, int, integer



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
C programming - Char to Int Conversion ? indian Programming 4 09-30-2005 08:00 AM
HTB shaped bandwitch runs over the ceiling! lez Linux - Networking 2 09-05-2005 04:00 AM
C programming int to char conversion paranoid times Programming 15 03-17-2005 11:06 PM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

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

All times are GMT -5. The time now is 01:36 AM.

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