473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to find the number of digit in a given number

2
Hi,

I'm trying to find the total number of digit for a user's input integer. I'm using the sizeof() function but keeps getting the size is 4 for some odd reason.

For example sizeof(32333) is 5 but it gives me 4. The following is my code, and my sizeof() function is near the middle. Thank you in advance

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main(void)
  4. {
  5.   int b;
  6.   int n;
  7.   int output[40];
  8.   int index = 0;
  9.   int condition;
  10.   int size=0;
  11.  
  12.   printf("Enter base(b) between [1,10], and a number(n), such that digits of n is between[0,b-1] in this format 'n b': ");
  13.   scanf("%d %d", &n, &b);
  14.  
  15.   printf("n is: %d\n", n);   //Test
  16.   printf("b is: %d\n", b);   //TEST
  17.  
  18.   if ((b < 1) || (b > 10))
  19.   {
  20.     printf("Your base is not between 1 and 10");
  21.     return 0;
  22.   }
  23.  
  24.   if (n < 0)
  25.   {
  26.     printf("n must be positive");
  27.     return 0;
  28.   }
  29.  
  30.   size = sizeof(n);
  31.   printf("size of n is: %d\n", size);
  32.  
  33. /*  while (condition != 0)
  34.   {
  35.     output[index] = n % 10;
  36.  
  37.     printf("number to convert: %d\n", output[index]);
  38.     index++;
  39.   }*/
  40.   return 0;
  41. }
  42.  
Aug 30 '07 #1
2 2907
Banfa
9,065 Expert Mod 8TB
Firstly xp5 please put code tags round your code when you post it.

Expand|Select|Wrap|Line Numbers
  1.   printf("Enter base(b) between [1,10], and a number(n), such that digits of n is between[0,b-1] in this format 'n b': ");
  2.   scanf("%d %d", &n, &b);
  3.  
Firstyl this doesn't do what you think it does, you request a base and a number in that base, however scanf read read and store that number as a decimal. For instance if I enter the base as 7 and enter the number 13 then that should have a decimal value of 10. But scanf will read is as decimal and will give the number the decimal value 13.

Also you read the number and the base the opposite way round to how you tell the user to input them.
Expand|Select|Wrap|Line Numbers
  1.   size = sizeof(n);
  2.  
You have mis-understood how the sizeof operator works. The sizeof operator is nothing to do with returning the size of a number held in an int (or any other numeric type). The sizeof operator returns the data size in bytes of the variable or type passed to it. In your case you are running on a 32 bit machine with 8 bit bytes (probably although from the information given it could be a 28 bit machine with 7 bit bytes but that is unlikely) so integers are 32 bits wide which is 4 bytes. So sizeof and int variable is 4. This operator is very useful when dynamically allocating memory for structures and when iterating through arrays of data (or in fact any other time you may want the array size).
Aug 30 '07 #2
xp5
2
Firstly xp5 please put code tags round your code when you post it.

Firstyl this doesn't do what you think it does, you request a base and a number in that base, however scanf read read and store that number as a decimal. For instance if I enter the base as 7 and enter the number 13 then that should have a decimal value of 10. But scanf will read is as decimal and will give the number the decimal value 13.

Also you read the number and the base the opposite way round to how you tell the user to input them.
You have mis-understood how the sizeof operator works. The sizeof operator is nothing to do with returning the size of a number held in an int (or any other numeric type). The sizeof operator returns the data size in bytes of the variable or type passed to it. In your case you are running on a 32 bit machine with 8 bit bytes (probably although from the information given it could be a 28 bit machine with 7 bit bytes but that is unlikely) so integers are 32 bits wide which is 4 bytes. So sizeof and int variable is 4. This operator is very useful when dynamically allocating memory for structures and when iterating through arrays of data (or in fact any other time you may want the array size).
ahhh...Thank you very much!
Aug 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: JJA | last post by:
I would like some advice on a data and query problem I face. I have a data table with a "raw key" value which is not guaranteed to be valid at its source. Normally, this value will be 9 numeric...
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
8
by: Steven | last post by:
Hi, I am trying to find out how many digits there are in a given number. The macro listed below works fine when applied to an INT, however when doing Doubles with numbers > then a billion ?? It...
7
by: ffrugone | last post by:
I need to create a method for a web page I'm building that encrypts a number from 1 to 1000 into a 4 digit hex code. I know nothing about cryptography. Can anyone steer me in the right direction?
14
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
by: hl2ob | last post by:
Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point. We have to make a program that test a 5 digit number as a palindrome. I have no...
7
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.