Find the answer to your Linux question:
Results 1 to 2 of 2
Hi i have been trying to compile a simple C program and i seem to have 2 errors, i tried to use the debugger but it didnt help if you ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Nov 2002
    Posts
    420

    Need Help!


    Hi i have been trying to compile a simple C program
    and i seem to have 2 errors, i tried to use the debugger
    but it didnt help if you can see the problem please tell me

    heres my program:

    #include "stdafx.h"

    int main(int argc, char* argv[])
    {
    printf("Integers of 2 up 2^n!\n");
    return 0;
    }

    #include "stdafx.h"

    #define N 1600

    int main(void) {

    int n; /* The current exponent */
    int val = 1; /* The current power of 2 */

    printf("\t n \t 2^n\n");
    printf("\t====================\n");
    for (n=0; n<=N; n++) {
    printf("\t%3d \t %600d\n", n, val);
    val = 2*val;
    }
    return 0;
    }

    this program is meant to have a title "integers of 2"
    and then list the integers of it until 2^n.


    BTW i tried to compile this program in not only Emacs but also Microsoft VC++ 6.0 Pro which i got for about $0.60 american dollars from a romanian gentleman, no luck thier way!
    thanks for your help.

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    The problem seems pretty obvious to me. An "int" in C is, after all, just a signed 32-bit integral number, which means that the largest number it can hold is 2147483647, or 2 ^ 31 - 1. The largest datatype in current C on the x86 architecture is a long double, which is a floating point storage, 80 bits long, ie. still not enough by far to hold the 1600-bit numbers that you want to use. The thing is that all native C data types are the types which the hardware can handle natively, and the CPU naturally doesn't have any instructions to handle that large numbers. If you want to do such a thing, you need to implement a datatype of your own, with functions to manipulate it.
    For example, you could do something like this:

    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <sys/types.h>
    #include <string.h>
    
    #define N 1600
    
    struct bignum
    &#123;
        unsigned char *buf;
        size_t bufsize;
    &#125;;
    
    struct bignum *bignum_init&#40;void&#41;
    &#123;
        struct bignum *new;
    
        new = malloc&#40;sizeof&#40;struct bignum&#41;&#41;;
        new->buf = malloc&#40;new->bufsize = 128&#41;;
        new->buf&#91;0&#93; = '0';
        new->buf&#91;1&#93; = 0;
        return&#40;new&#41;;
    &#125;
    
    void bignum_free&#40;struct bignum *num&#41;
    &#123;
        free&#40;num->buf&#41;;
        free&#40;num&#41;;
    &#125;
    
    unsigned char *bignum_mkstr&#40;struct bignum *num&#41;
    &#123;
        int i, o;
        unsigned char *buf;
        
        buf = malloc&#40;strlen&#40;num->buf&#41; + 1&#41;;
        buf&#91;strlen&#40;num->buf&#41;&#93; = 0;
        for&#40;i = strlen&#40;num->buf&#41; - 1, o = 0; i >= 0; i--, o++&#41;
            buf&#91;o&#93; = num->buf&#91;i&#93;;
        return&#40;buf&#41;;
    &#125;
    
    struct bignum *bignum_set_int&#40;struct bignum *dest, int n&#41;
    &#123;
        int i;
        
        i = 0;
        while&#40;n&#41;
        &#123;
            dest->buf&#91;i++&#93; = &#40;n % 10&#41; + '0';
            n /= 10;
        &#125;
        dest->buf&#91;i&#93; = 0;
    &#125;
    
    struct bignum *bignum_mul_int&#40;struct bignum *dest, int f&#41;
    &#123;
        int i, m;
    
        m = 0 
        for&#40;i = 0; dest->buf&#91;i&#93;; i++&#41;
        &#123;
            m = &#40;&#40;dest->buf&#91;i&#93; - '0'&#41; * f&#41; + m;
            dest->buf&#91;i&#93; = &#40;m % 10&#41; + '0';
            m /= 10;
        &#125;
        if&#40;m&#41;
        &#123;
            if&#40;i == dest->bufsize&#41;
                dest->buf = realloc&#40;dest->buf, dest->bufsize += 128&#41;
            dest->buf&#91;i++&#93; = m;
            dest->buf&#91;i&#93; = 0;
        &#125;
        return&#40;dest&#41;;
    &#125;
    
    int main&#40;int argc, unsigned char **argv&#41;
    &#123;
        int i;
        struct bignum *num;
        unsigned char *buf;
        
        bignum_set_int&#40;num = bignum_init&#40;&#41;, 1&#41;;
        printf&#40;"       n | 2^n\n"&#41;;
        for&#40;i = 0; i < N; i++&#41;
        &#123;
            buf = bignum_mkstr&#40;num&#41;;
            printf&#40;"%8i | %s\n", i, buf&#41;
            free&#40;buf&#41;;
            bignum_mul_int&#40;num, 2&#41;;
        &#125;
        bignum_free&#40;num&#41;;
        return&#40;0&#41;;
    &#125;
    I haven't tried it myself, since I wrote it just now, but I see no reason why it shouldn't work. Try it and see.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •