Problems

This is a discussion on Problems within the C Programming forums, part of the General Programming Boards category; Can Anyone see whats worng with any of the three following pieces of code thanks 1. Code: void Display_Result(char dat) ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    Problems

    Can Anyone see whats worng with any of the three following pieces of code thanks

    1.
    Code:
    void Display_Result(char dat)
    {
          printf("the result is %X\n");
    }
    The parameter dat containing 'A' is passed to the function Display_Result and the printf rfunction produces 12FF80

    2.

    Code:
    #include <stdio.h>
    
    void main(void)
    {
        printf("e=%f\n",exp(1));
    }
    When executed this programme produces e = 0.000000 instead of e= 2.718282

    3.

    Code:
    #include <stdio.h>
    
    vois main (void
    {
    char input;
    
        printf ("Press a key: ");
        scanf("%c, input;
    
        printf("You pressed:%c\n",input);
    }
    When executed under windows XP, this programme causes a Misrosoft dialog box to open and report ....

    test1.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

    It the invites you to debug the prgramme or send an Error Report.

    Thanks a mill for your help, this is an answer im looking for for exams, please help,

    P.S i know some of it maybe in the wrong section but i wanted to keep it altogether.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Here's what I think:
    1. You need to pass char dat to the function printf.
    i.e. printf("the result is %X\n", dat);

    2. I don't know.

    3. You're missing a parentheses in your scanf line.

    Cheers


    Quote Originally Posted by tipp2007 View Post
    Can Anyone see whats worng with any of the three following pieces of code thanks

    1.
    Code:
    void Display_Result(char dat)
    {
          printf("the result is %X\n");
    }
    The parameter dat containing 'A' is passed to the function Display_Result and the printf rfunction produces 12FF80

    2.

    Code:
    #include <stdio.h>
    
    void main(void)
    {
        printf("e=%f\n",exp(1));
    }
    When executed this programme produces e = 0.000000 instead of e= 2.718282

    3.

    Code:
    #include <stdio.h>
    
    vois main (void
    {
    char input;
    
        printf ("Press a key: ");
        scanf("%c, input;
    
        printf("You pressed:%c\n",input);
    }
    When executed under windows XP, this programme causes a Misrosoft dialog box to open and report ....

    test1.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

    It the invites you to debug the prgramme or send an Error Report.

    Thanks a mill for your help, this is an answer im looking for for exams, please help,

    P.S i know some of it maybe in the wrong section but i wanted to keep it altogether.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    ohh, and you need to pass an address to your scanf statement.
    So, you need to pass &input instead of input.

    Right?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. You are missing the data you want to print, so it's printing some "rubbish" that happens to be in the location where your data SHOULD have been.

    2. Try including "math.h", your exp() function is most likely returning a floating point number in a floating point register, but the compiler doesn't know this, so it's assuming the return value is an integer, which is returned in a general purpose register (EAX on x86).

    3. You are passing the variable "input" into scanf, but scanf takes a pointer, so it's using "input" as a pointer, which is absolutely certain to not work.

    Most of these problems would be detected by a good compiler if you enable "warnings".

    --
    Mats

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Nobody mentioned all the void main's being wrong, which they are.
    main returns int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Thanks a million to everyone for such a rapid reply. Any more suggestions are welcome.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    More suggestions on what?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21