Printing "Hello World" in C programming language.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jningombam
    New Member
    • Dec 2006
    • 3

    Printing "Hello World" in C programming language.

    Hi all,
    Could anyone please help me to print "Hello World" in C programming language without including any code inside main function

    Regards.
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    #2
    main()
    {
    printf("hello, world\n");
    }

    like this?

    Regards,
    Nicky Eng.

    Comment

    • jningombam
      New Member
      • Dec 2006
      • 3

      #3
      Originally posted by nickyeng
      main()
      {
      printf("hello, world\n");
      }

      like this?

      Regards,
      Nicky Eng.
      Sorry fren, there should be no code inside the function main. So you can't include that printf statement inside the main function.

      Regards.

      Comment

      • nickyeng
        Contributor
        • Nov 2006
        • 252

        #4
        try class :
        Code:
        class Sample
        {
               public:
                       Sample() { cout <<"Hello;" ; }
        };
        
        
        Sample S;
        
        void main(void)
        {
        }

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #5
          But that's C++, not C.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            This must be one of the weirdest (and possibly most pointless) problems I have seen.

            I have no idea.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Ganon11
              This must be one of the weirdest (and possibly most pointless) problems I have seen.

              I have no idea.
              I hope it's possible

              Comment

              • Coldfire
                Contributor
                • Nov 2006
                • 289

                #8
                Originally posted by r035198x
                I hope it's possible
                i think it would work.

                replace in the above sample (with constructor)
                > class with struct
                > "cout" with appropriate "printf" statement
                ...........that s it

                i havnt tried though

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by Coldfire
                  i think it would work.

                  replace in the above sample (with constructor)
                  > class with struct
                  > "cout" with appropriate "printf" statement
                  ...........that s it

                  i havnt tried though
                  You mean to call a method inside a struct declaration?

                  Comment

                  • nickyeng
                    Contributor
                    • Nov 2006
                    • 252

                    #10
                    why not the thread starter searching in searc engine(google.c om) first, and try the sample there...

                    Comment

                    • Coldfire
                      Contributor
                      • Nov 2006
                      • 289

                      #11
                      Originally posted by r035198x
                      You mean to call a method inside a struct declaration?
                      yes...may be..i mean this
                      struct Sample
                      {
                      public:
                      Sample() {
                      printf("Hello World");
                      }
                      };


                      Sample S;

                      void main(void)
                      {
                      }

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Coldfire
                        yes...may be..i mean this
                        struct Sample
                        {
                        public:
                        Sample() {
                        printf("Hello World");
                        }
                        };


                        Sample S;

                        void main(void)
                        {
                        }
                        Works alright. I wonder when it might come in handy...

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by Coldfire
                          yes...may be..i mean this
                          struct Sample
                          {
                          public:
                          Sample() {
                          printf("Hello World");
                          }
                          };


                          Sample S;

                          void main(void)
                          {
                          }
                          This is still C++ not C, I get these errors

                          c:\myprojects\c onsoletester\co nsoletester.c(3 ) : error C2061: syntax error : identifier 'public'
                          c:\myprojects\c onsoletester\co nsoletester.c(7 ) : error C2059: syntax error : '}'
                          c:\myprojects\c onsoletester\co nsoletester.c(1 0) : error C2061: syntax error : identifier 'S'
                          c:\myprojects\c onsoletester\co nsoletester.c(1 0) : error C2059: syntax error : ';'


                          The answer is that in C it is not possible, the only thing you can do in C or C++ without putting code in main is declare variables. In C++ you can declare a class which will envoke it's constructor (and in fact could envoke the whole program) but in C there is no way that declaring a variable envokes any user code.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by Banfa
                            This is still C++ not C, I get these errors

                            c:\myprojects\c onsoletester\co nsoletester.c(3 ) : error C2061: syntax error : identifier 'public'
                            c:\myprojects\c onsoletester\co nsoletester.c(7 ) : error C2059: syntax error : '}'
                            c:\myprojects\c onsoletester\co nsoletester.c(1 0) : error C2061: syntax error : identifier 'S'
                            c:\myprojects\c onsoletester\co nsoletester.c(1 0) : error C2059: syntax error : ';'


                            The answer is that in C it is not possible, the only thing you can do in C or C++ without putting code in main is declare variables. In C++ you can declare a class which will envoke it's constructor (and in fact could envoke the whole program) but in C there is no way that declaring a variable envokes any user code.
                            Gosh, I ran it on a C++ compiler

                            Comment

                            Working...