0

So i am writing a code for Bit Stuffing in C. What I am trying to do is take an input from user,keep on dividing the number to get each digit using "%" operator.This works as long as the no is in the range of int. If I use long int type,the code no longer works mostly because the "n/10" is no longer an integer.So how do I do an integer division of the long int type variable.In python "//" used to work for this purpose.Anything similar in C? And I don't want to use an array because in that case the user has to input one digit at a time.Here's my code for reference.Thanks

#include <stdio.h>
int main(){
    int c,n,m,bit[200]={0,1,1,1,1,1,1,0},x=7,count=0,ch=0;

    do{
        printf("Enter No:\n");
        scanf("%d",&n);
        m=n;
        while(m>0){
            c=m%10;
            m=m/10;
            if(c==1){
                count++;
            }
            else if(c==0){
                count=0;
            }
            else if(c>1 && c<10){
                printf("Invalid  Pattern.Enter Either 1 or 0.\n");
                ch=1;
                break;
            }
            ch=0;
            x++;
            bit[x]=c;
            if(count==5){x++;bit[x]=0;count=0;}
        }

    }while(ch==1);

    x++;
    bit[x]=0;
    for(int i=0;i<=5;i++){
        x++;
        bit[x]=1;
    }
    x++;
    bit[x]=0;
    for(int i=0;i<=x;i++){
        printf("%d",bit[i]);
    }
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
Dexter
  • 25
  • 5
  • 3
    Use `long c;` (or, C99, `long long c;`) for a larger limit. To scan a `long` use `"%ld"` (`"%lld"` for long long). To print a `long` use `"%ld"` (`"%lld"` for long long) – pmg Mar 06 '19 at 21:08
  • 3
    As long as you use `long` for all your intermediate results you should be fine. – dbush Mar 06 '19 at 21:09
  • 2
    "*... because the "n/10" is no longer an integer*" - why would `n/10` no longer be an integer? – melpomene Mar 06 '19 at 21:15
  • Integer division works just fine for all integer types, including `long int` in particular. Ditto the modulus operator. Your problem is likely that you are not *inputting* your value into a `long int` correctly (but we don't know, because you've not presented a version of your code that attempts to do so). – John Bollinger Mar 06 '19 at 21:16
  • 3
    Since you have `scanf("%d",&n);` and `n` is an `int`, you can't get a value outside the range of `int` into `n`, so the question is moot on that point. It isn't even interesting to ask what happens if you type a value for `n` that is too big — that is simply undefined behaviour (in the [`scanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html) function). – Jonathan Leffler Mar 06 '19 at 21:18
  • 1
    You should really be reading the user input as a string instead of an integer. Then you won't be limited to the range of any integer type and you can enter bit patterns that start with one ore more zeros. – dbush Mar 06 '19 at 21:23
  • Post sample input and example outputs to add clarity. – chux - Reinstate Monica Mar 07 '19 at 00:06

0 Answers0