Thread: compiler differences

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    compiler differences

    halo all,
    i have made a code that converts hexadecimal to corresponding integer value.my problem is when i run this program in turbo c++ it works fine but when i run the same program in visual studio 2005 it even doesn't compiles.it gives the following error whereever pow function is used.
    Code:
    Error	4	error C2668: 'pow' : ambiguous call to overloaded function
    here is my code

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    #include<math.h>
    #include<string.h>
    long int htoi(char []);
    int main(void)
    {
    	char s[10];
    	printf("Input any hexadecimal number (between 0-9 or a-f or A-F\n)");
    	gets(s);
    	printf("%ld",htoi(s));
    	getch();
    	return 0;
    }
    long int htoi(char s[])
    {
    	int x=strlen(s);
    	long int i,sum=0;
    	for(i=0;s[i]!='\0';i++)
    	{
    		if(isdigit(s[i]))
    			sum+=(s[i]-'0')*pow(16,--x);
    		if(isalpha(s[i]))
    		{
    			if(islower(s[i]))
    			{
    				s[i]=toupper(s[i]);
    				sum+=(s[i]-'7')*pow(16,--x);
    			}
    			else
    				sum+=(s[i]-'7')*pow(16,--x);
    		}
    	}
    	return sum;
    }
    i wanna know why these differences in the compilers.
    Thank You

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, take a look why you shouldn't use gets: SourceForge.net: Gets - cpwiki
    If you want, you can also take a look at this new string library that simplifies string management: http://cboard.cprogramming.com/c-pro...-new-post.html
    Lastly, according to the manual, pow takes two doubles and returns a double, so try doing:
    pow(16.0,(double)--x);
    But you seem to be compiling this as C++... yet you are supposed to use C, though? Is that it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to compile the code as C. If you compile as C++, you would need to explicitly cast your arguments such that the compiler can choose the appropriate version of pow() to call; at the moment it has several equally viable choices.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    i have made a code that converts hexadecimal to corresponding integer value
    Code:
    #include<stdio.h>
    
    int main()
    {
        int no;      
         printf("Enter a hexadecimal number\n");
         scanf("%x",&no);
    
         printf("Integer number is - %d\n",no);
         return 0;
    }
    Last edited by creeping death; 03-29-2009 at 12:17 PM. Reason: removed fflush(stdin) as it is 'morally' wrong!! ;)
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fflush(stdin) is undefined. http://cpwiki.sourceforge.net/fflush_stdin
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by Elysia View Post
    fflush(stdin) is undefined. SourceForge.net: Fflush stdin - cpwiki
    you link is...
    Fflush stdin
    From cpwiki
    Jump to: navigation, search

    There is currently no text in this page, you can search for this page title in other pages or edit this page.

    now POSIX specs
    STDIN(3P) POSIX Programmer’s Manual STDIN(3P)

    PROLOG
    This manual page is part of the POSIX Programmer’s Manual. The Linux
    implementation of this interface may differ (consult the corresponding
    Linux manual page for details of Linux behavior), or the interface may
    not be implemented on Linux.

    NAME
    stderr, stdin, stdout - standard I/O streams

    SYNOPSIS
    #include <stdio.h>

    extern FILE *stderr, *stdin, *stdout;
    and
    FFLUSH(3P) POSIX Programmer’s Manual FFLUSH(3P)

    PROLOG
    This manual page is part of the POSIX Programmer’s Manual. The Linux
    implementation of this interface may differ (consult the corresponding
    Linux manual page for details of Linux behavior), or the interface may
    not be implemented on Linux.

    NAME
    fflush - flush a stream

    SYNOPSIS
    #include <stdio.h>

    int fflush(FILE *stream);

    i dont see why it wouldn't be
    Last edited by creeping death; 03-29-2009 at 11:40 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Crap. Didn't remember it wasn't written.
    Here are valid links:
    Cprogramming.com: FAQ
    http://apps.sourceforge.net/mediawik...p?title=Fflush
    fflush for output streams is perfectly fine, but not for input streams.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by creeping death
    now POSIX specs
    Post the entire entry for fflush(). I have my doubts if it says that fflush() is defined for input streams. If it says nothing about input streams, then it remains undefined, since the C standard only defines fflush() for output and update streams.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You don't even have to read down. This is the first line from the current "Open Group Base Specifications Issue 7" (which The Open Group are the authors of the POSIX standard) man page for fflush:
    If stream points to an output stream or an update stream in which the most recent operation was not input...
    Be nice if it did work, but the point is made. Say you heard it here first
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by MK27 View Post
    You don't even have to read down. This is the first line from the current "Open Group Base Specifications Issue 7" (which The Open Group are the authors of the POSIX standard) man page for fflush:


    Be nice if it did work, but the point is made. Say you heard it here first
    i think the catch is in the second line...goddammit!! how come it never gave any warning or errors??

    ...fflush() shall cause any unwritten data for that stream to be written to the file...
    and one cannot write to stdin.

    but so far it was mysteriously working without any errors or warnings...

    oh well! i thank you all...i stand corrected.

    Code:
    #include<stdio.h>
    
    int main()
    {
        int no,val;      
         printf("Enter a hexadecimal number\n");
         scanf("%x",&no);
         val=fflush(stdin);
         printf("val=%d",val);
         printf("Integer number is - %d\n",no);
         return 0;
    }
    Code:
    [c_d@localhost C scratchpad]$ gcc -Wall -ansi -std=c99 -pedantic temp.c
    temp.c: In function ‘main’:
    temp.c:8: warning: format ‘%x’ expects type ‘unsigned int *’, but argument 2 has type ‘int *’
    [c_d@localhost C scratchpad]$ ./a.out
    Enter a hexadecimal number
    ff
    val=0Integer number is - 255 (0 means no errors returned whatsoever, i.e. it wrote to FILE *stdin!!! wtf?! )
    [c_d@localhost C scratchpad]$
    see? nothing...
    Last edited by creeping death; 03-29-2009 at 12:15 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by creeping death
    how come it never gave any warning or errors??
    They are not required for undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by laserlight View Post
    They are not required for undefined behaviour.
    perhaps i have misconceptions about stdin also...

    so,please explain to me the nature of stdin...is it or is it not possible to write into something thats only opened for reading from?

    stdin is a stream. and a stream is a file with buffer associated to it . and stdin is meant only to be read from and not written into then, so when someone tries to write into a file which is only to be read from , generally the environment,in which the file exists returns an error...

    so whats keeping it from returning an error?
    Last edited by creeping death; 03-29-2009 at 12:40 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  13. #13
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    any explanations? or do i have to take it as an axiom, an unwritten law...
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think your "generally the environment returns an error" part is perhaps mistaken. I just tried this code:
    Code:
    #include <stdio.h>
    
    int main(void) {
    
        FILE *this;
    
        this = fopen("test.c", "r");
        fprintf(this, "Waahaahaa!\n");
        fclose(this);
        return 0;
    }
    where test.c is the file itself, compiled it, and ran it. I didn't get any runtime error, but clearly "Waahaahaa!" was not written in my file either. (I'm running Mac OSX + gcc.)

  15. #15
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by tabstop View Post
    I think your "generally the environment returns an error" part is perhaps mistaken. I just tried this code:
    Code:
    #include <stdio.h>
    
    int main(void) {
    
        FILE *this;
    
        this = fopen("test.c", "r");
        fprintf(this, "Waahaahaa!\n");
        fclose(this);
        return 0;
    }
    where test.c is the file itself, compiled it, and ran it. I didn't get any runtime error, but clearly "Waahaahaa!" was not written in my file either. (I'm running Mac OSX + gcc.)

    when i ran your code...sure enough...there was not problem in execution...i though there might be something in stderr...but there was nothing there too...
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c
    [c_d@localhost C scratchpad]$ touch test.c
    [c_d@localhost C scratchpad]$ ./a.out 2>error
    [c_d@localhost C scratchpad]$ cat test.c
    [c_d@localhost C scratchpad]$ cat error
    oh! so theres the flaw...in the C library functions...because...if i use (linux/unix)system calls...then
    Code:
    #include<unistd.h>
    #include<fcntl.h>
    #include<stdlib.h>
    
    int main()
    {
        int fd;
        if((fd=open("f1",O_RDONLY))==-1)
        {
            perror("f1 error");
            exit(1);
        }
        if((write(fd,"arrgh!",6))==-1)
        {
            perror("error writing to file");
        }    
        close(fd);
    }
    and the output would be
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c
    [c_d@localhost C scratchpad]$ touch f1
    [c_d@localhost C scratchpad]$ ./a.out
    error writing to file: Bad file descriptor
    [c_d@localhost C scratchpad]$
    see? the environment in this case returned an error...

    and if i change the mode from O_RDONLY(read only) to O_WRONLY(write only) or O_RDWR(read-write) i get no errors what so ever
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c   #compiling after modifying to O_WRONLY
    [c_d@localhost C scratchpad]$ ./a.out
    [c_d@localhost C scratchpad]$ cat f1
    arrgh![c_d@localhost C scratchpad]$
    i dont understand this because , library functions would ultimately invoke the system-calls...
    Last edited by creeping death; 03-29-2009 at 01:58 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM