Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-16-2006, 08:48 PM   #1
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, 09:14 PM   #2
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, 09:15 PM   #3
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, 03:34 AM   #4
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Quoting from:
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...)


division, int, integer

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

All times are GMT -5. The time now is 04:21 PM.

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