Thread: compiler differences

  1. #31
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Elysia View Post
    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?
    i typecasted pow and then it works fine but i still didn't get it why it didn't work without typecasting.if typecasting was needed it should have been needed with turbo c++ also.are definitions of pow different in c and c++?please explain it.and yes i was compiling this as C++ in visual studio.

  2. #32
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by BEN10 View Post
    are definitions of pow different in c and c++?please explain it.and yes i was compiling this as C++ in visual studio.
    C does not have function overloading, while C++ has
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #33
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by BEN10 View Post
    i typecasted pow and then it works fine but i still didn't get it why it didn't work without typecasting.if typecasting was needed it should have been needed with turbo c++ also.are definitions of pow different in c and c++?please explain it.and yes i was compiling this as C++ in visual studio.
    save the file as .c not .cpp

    and seriously do you need to do so much to find the decimal equivalent of a hexadecimal number?
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #34
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by creeping death View Post
    save the file as .c not .cpp

    and seriously do you need to do so much to find the decimal equivalent of a hexadecimal number?
    after saving the file as .c it worked.
    it doesn't matter whether to find decimal equivalent or sum of two integers but to understand the concepts behind the thing.it could have been the same thing if i would have used the pow function in a very difficult program but now as i understand the main concept behind it i'll be able to use it at other places also.
    thanks for all your replies.

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by BEN10 View Post
    i typecasted pow and then it works fine but i still didn't get it why it didn't work without typecasting.if typecasting was needed it should have been needed with turbo c++ also.are definitions of pow different in c and c++?please explain it.and yes i was compiling this as C++ in visual studio.
    It might work in Turbo C++ because it's old and not very standards compliant today.
    It works differently in C++ because C++ have the ability to have a number of functions with the same name. The compiler chooses which one to call depending on what types you are passing to the function. But there's no overload that takes two integers, so the compiler simply couldn't decide which function to call, since an integer can be promoted to a number of types.
    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. #36
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by MK27 View Post
    Keep in mind each process has it's own stdin and stdout. If you had a forked process connected to a parent with a pipe, you write into the stdin of the pipe and it comes out of the stdout of the pipe, right? But if the child wrote into it's own stdin (usually you would close that) the same thing will happen.
    Code:
    fwrite("this",4,1,stdin);
    Does the same thing you just observed.
    i tried this...
    Code:
    #include<stdio.h>
    
    int main()
    {
        fwrite("this",4,1,stdin);
        perror("fwrite status");
    }
    output:
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c
    [c_d@localhost C scratchpad]$ ./a.out
    fwrite status: Bad file descriptor
    [c_d@localhost C scratchpad]$
    no it did not mimic the system-call.
    Code:
    #include<unistd.h>
    #include<fcntl.h>
    
    int main()
    {
        close(STDOUT_FILENO);
        if((write(STDIN_FILENO,"arrgh!",6))==-1)
        {
            perror("stdin");
        }
        if((write(STDOUT_FILENO,"arrgh!",6))==-1)
        {
            perror("stdout");
        }
    }
    output:
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c
    [c_d@localhost C scratchpad]$ ./a.out 
    arrgh!stdout: Bad file descriptor
    [c_d@localhost C scratchpad]$ ./a.out <temp.c
    stdin: Bad file descriptor
    stdout: Bad file descriptor
    [c_d@localhost C scratchpad]$
    so process stdin stdout fd points /dev/tty under "normal circumstances"

    this is what i found about stdin...
    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdio.h>
    
    
    int main(void) 
    {
        struct stat st;
        char *whichtty;
    
        if ( !isatty(STDIN_FILENO) ) 
        {
    	    (void) fprintf(stderr, "%s\n", "stdin is not connected to a tty device.");
        } 
        else 
        {
    	    if ( (whichtty = ttyname(STDIN_FILENO)) == NULL ) 
    	    {
    	        perror("ttyname");
                return 1;
    	    } 
    	    else 
    	    {
    	        (void) fprintf(stderr, "stdin is connected to tty device %s.\n", whichtty);
    	    }
        }
    
        if ( fstat(STDIN_FILENO, &st) == -1 ) 
        {
    	    perror("file status");
    	    return 1;
        } 
        else 
        {
    	    if ( st.st_mode & S_IWUSR ) 
    	    {
    	        (void) fprintf(stderr, "%s\n", "stdin appears to be writeable by the user.");
    	    } 
    	    else 
    	    {
    	        (void) fprintf(stderr, "%s\n", "stdin does not appear to be writeable by the user.");
    	    }
        }
        return 0;
    }
    output:
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c
    [c_d@localhost C scratchpad]$ ./a.out
    stdin is connected to tty device /dev/pts/3.
    stdin appears to be writeable by the user.
    [c_d@localhost C scratchpad]$
    now, when i do this
    Code:
    [c_d@localhost C scratchpad]$ ./a.out <temp.c
    stdin is not connected to a tty device.
    stdin appears to be writeable by the user.
    [c_d@localhost C scratchpad]$ chmod -w temp.c  #remove write perm
    [c_d@localhost C scratchpad]$ ./a.out <temp.c
    stdin is not connected to a tty device.
    stdin does not appear to be writeable by the user.
    [c_d@localhost C scratchpad]$ chmod +w temp.c
    fwrite() may invoke write() eventually, but there's lots of stuff going on inbetween...

    why so much difference FILE *stdin(stream) and STDIN_FILENO(descriptor of the same stdin stream)?
    Last edited by creeping death; 03-31-2009 at 05:05 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #37
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    anyone?...guys?...

    can you answer my questions? or do you think its best that i not dig into these things...
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What does it matter?
    The C Library does everything necessary to make the code work.
    We don't need to understand its implementation details. Plus these details are implementation-defined. They can change (and probably will) between different compilers.
    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.

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the answer is that FILE * and the stdio.h functions are standard. They work in DOS, Windows, OS/2, Linux, Unix, VAX/VMS, RT-11, RSTS/E, CP/M, many differnet RTOS's, and most other environments there is a C compiler for.

    The correspondence between the lower level implementation (e.g. open, write, read, etc) may be close or quite far apart, depending on the actual low-level implementation. Modern OS's tend to work similar to what stdio.h implements, which makes it easier to use the OS. Older OS's such as CP/M or RT-11 are not quite so straight forward to translate from FILE * operations into native form.

    --
    Mats
    Last edited by matsp; 04-01-2009 at 04:31 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    yes, i guess its because its standard and because they have been implemented in that way...

    i just will stop trying to write into stdin then. i dont want confuse and to set my poor keyboard on fire

    i ll leave you all in peace. thanks again for your patience.
    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