Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Please consider the following C Program

Please consider the following C Program

Scheduled Pinned Locked Moved C / C++ / MFC
help
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    Please consider the following C program.

    main()
    {
    int i, j, *p;
    i = 25;
    j = 100;
    p = &i; // Address of i is assigned to pointer p
    printf("%f", i/(*p) ); // i is divided by pointer p
    }

    I took an online quiz on C and the correct answer was syntax error. I cannot find anything wrong with the above program. I tried both GCC and the Microsoft Development Stdio and both conmpilers accept the program. Please comment. Thanks Bob

    T C D CPalliniC T 5 Replies Last reply
    0
    • B BobInNJ

      Please consider the following C program.

      main()
      {
      int i, j, *p;
      i = 25;
      j = 100;
      p = &i; // Address of i is assigned to pointer p
      printf("%f", i/(*p) ); // i is divided by pointer p
      }

      I took an online quiz on C and the correct answer was syntax error. I cannot find anything wrong with the above program. I tried both GCC and the Microsoft Development Stdio and both conmpilers accept the program. Please comment. Thanks Bob

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      it's ok for me as well. p is of type int*, so *p is an int... and nothing forbids you to divide an int with another int. so the result should be 1. (notice the . which means it is a float - actually, the division returns an int, but I suspect (not sure though) that the %f asks for an implicit cast)

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      1 Reply Last reply
      0
      • B BobInNJ

        Please consider the following C program.

        main()
        {
        int i, j, *p;
        i = 25;
        j = 100;
        p = &i; // Address of i is assigned to pointer p
        printf("%f", i/(*p) ); // i is divided by pointer p
        }

        I took an online quiz on C and the correct answer was syntax error. I cannot find anything wrong with the above program. I tried both GCC and the Microsoft Development Stdio and both conmpilers accept the program. Please comment. Thanks Bob

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        Well, i see no syntax error either, what i do see and imho could cause a problem are the following: -if main is actually the main entry routine then it should look like this: int main(int argc, char **argv);, but that wouldn't give you a syntax error, besides, that alone is just a function definition, nothing says it is "THE MAIN" entry point method. Besides, i think nowadays most compilers will accep that as the main entry point anyways. -the "%f" in printf will expect a floating point value rather than an integer, but then again, not a syntax error -you didn't specify any return type for the method, am not sure how compilers handle that but VC++ selfrigthously will take it as int, and might call you names for not returning anything from that method. But again, that is not a syntax error. Hmm...

        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

        T 1 Reply Last reply
        0
        • B BobInNJ

          Please consider the following C program.

          main()
          {
          int i, j, *p;
          i = 25;
          j = 100;
          p = &i; // Address of i is assigned to pointer p
          printf("%f", i/(*p) ); // i is divided by pointer p
          }

          I took an online quiz on C and the correct answer was syntax error. I cannot find anything wrong with the above program. I tried both GCC and the Microsoft Development Stdio and both conmpilers accept the program. Please comment. Thanks Bob

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          BobInNJ wrote:

          Please comment.

          printf("%f", (double) i / *p);

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          1 Reply Last reply
          0
          • C Code o mat

            Well, i see no syntax error either, what i do see and imho could cause a problem are the following: -if main is actually the main entry routine then it should look like this: int main(int argc, char **argv);, but that wouldn't give you a syntax error, besides, that alone is just a function definition, nothing says it is "THE MAIN" entry point method. Besides, i think nowadays most compilers will accep that as the main entry point anyways. -the "%f" in printf will expect a floating point value rather than an integer, but then again, not a syntax error -you didn't specify any return type for the method, am not sure how compilers handle that but VC++ selfrigthously will take it as int, and might call you names for not returning anything from that method. But again, that is not a syntax error. Hmm...

            > The problem with computers is that they do what you tell them to do and not what you want them to do. <

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            I don't agree. the synopsis of the main() function (as an application entry point), there are several syntaxes allowed, and int main(int argc, char **argv) is definitely not *THE* ultimate one. in C, the returned type is not mandatory. if not provided, int is assumed. and under windows, main can accept a third parameter which is an array containing the environment variables...

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            C CPalliniC 2 Replies Last reply
            0
            • T toxcct

              I don't agree. the synopsis of the main() function (as an application entry point), there are several syntaxes allowed, and int main(int argc, char **argv) is definitely not *THE* ultimate one. in C, the returned type is not mandatory. if not provided, int is assumed. and under windows, main can accept a third parameter which is an array containing the environment variables...

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              See the Besides, i think nowadays most compilers will accep that as the main entry point anyways. part in my post, but you are right, sorry for the confusement. :)

              > The problem with computers is that they do what you tell them to do and not what you want them to do. <

              T 1 Reply Last reply
              0
              • C Code o mat

                See the Besides, i think nowadays most compilers will accep that as the main entry point anyways. part in my post, but you are right, sorry for the confusement. :)

                > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                normally, a C++ compiler would not accept a not typed function ; so the minimal main should be int main() at least...

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                1 Reply Last reply
                0
                • B BobInNJ

                  Please consider the following C program.

                  main()
                  {
                  int i, j, *p;
                  i = 25;
                  j = 100;
                  p = &i; // Address of i is assigned to pointer p
                  printf("%f", i/(*p) ); // i is divided by pointer p
                  }

                  I took an online quiz on C and the correct answer was syntax error. I cannot find anything wrong with the above program. I tried both GCC and the Microsoft Development Stdio and both conmpilers accept the program. Please comment. Thanks Bob

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  As David Crow already pointed out, there is an error, but it is not a syntactic one. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • T toxcct

                    I don't agree. the synopsis of the main() function (as an application entry point), there are several syntaxes allowed, and int main(int argc, char **argv) is definitely not *THE* ultimate one. in C, the returned type is not mandatory. if not provided, int is assumed. and under windows, main can accept a third parameter which is an array containing the environment variables...

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    toxcct wrote:

                    and under windows, main can accept a third parameter which is an array containing the environment variables...

                    Windows it's not alone, UNIX main has the above feature too. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    T 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      toxcct wrote:

                      and under windows, main can accept a third parameter which is an array containing the environment variables...

                      Windows it's not alone, UNIX main has the above feature too. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      CPallini wrote:

                      Windows it's not alone

                      and you say this to me ?! :doh: when you're not sure, don't talk about something you don't know... that's what I did ;P

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      CPalliniC 1 Reply Last reply
                      0
                      • T toxcct

                        CPallini wrote:

                        Windows it's not alone

                        and you say this to me ?! :doh: when you're not sure, don't talk about something you don't know... that's what I did ;P

                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #11

                        toxcct wrote:

                        and you say this to me ?!

                        Shouldn't I? Why? ;P

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        In testa che avete, signor di Ceprano?

                        1 Reply Last reply
                        0
                        • B BobInNJ

                          Please consider the following C program.

                          main()
                          {
                          int i, j, *p;
                          i = 25;
                          j = 100;
                          p = &i; // Address of i is assigned to pointer p
                          printf("%f", i/(*p) ); // i is divided by pointer p
                          }

                          I took an online quiz on C and the correct answer was syntax error. I cannot find anything wrong with the above program. I tried both GCC and the Microsoft Development Stdio and both conmpilers accept the program. Please comment. Thanks Bob

                          T Offline
                          T Offline
                          Timothy Baldwin
                          wrote on last edited by
                          #12

                          BobInNJ wrote:

                          p = &i; // Address of i is assigned to pointer p

                          That was a syntax error in the previous version of the C standard, comments begining with "//" were introduced in C99.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups