Truncating digits after radix point in C

As the title says, I need to do an integer division in C but just store the whole number answer. Is there an easy way to just drop everything after the decimal?
If both the dividend and divisor are of integer types, and positive, the result will be truncated. You might need to watch out for what happens if either is negative, but in any case the result is also going to be an integer (it's just a matter of whether it truncates in the direction you expect).

Lamice
Hmm that makes sense. I'll admit my knowledge of C is very very limited. I'm doing a pretty simple program here, but it's not working out. Here's what I got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char** argv)
{
  const unsigned int pgSize = 4096; //4 KB page size
  if(argc == 2)
    {
      unsigned int addr = argv[1];
      unsigned int pageNum = addr / pgSize;
      unsigned int offset = addr - (pgSize * pageNum);
      printf("The address %u contains:\n", addr);
      printf("Page number = %u\n", pageNum);
      printf("Offset = %u\n", offset);
    }
  else
    {
      printf("Invalid args\n");
    }

  return 0;
}


The output is strange to me. I'm not getting any errors but not getting the expected output either.

The address 117 contains:
Page number = 0
Offset = 117


This is from entering 19986 as an address. So not sure why it became 117.

EDIT:
Figured it out, I wasn't converting the argument to an integer -_- Silly mistake. This is what I get for not programming in ages.
Last edited on
int main(int argc, char* argv) shouldn't this have char** or char*[] ?
Yea that was just a typo. Supposed to be a pointer to pointer.
Topic archived. No new replies allowed.