Results 1 to 5 of 5

Thread: How to make a Hello World program in C/C++

  1. #1
    Join Date
    Oct 2014
    Beans
    3

    How to make a Hello World program in C/C++

    Hello there, it's Aaron, and I'm here to show you how to make a Hello World program in the C and C++ programming languages.

    First, go to the text editor of your choice, but personally, in my opinion, I recommend a IDE like Code::Blocks or even MonoDevelop (Which requires the Mono Runtime which you can get off Ubuntu Software Centre.) Then input this into the text editor window below. *NOTE IN C.

    Code:
    #include <stdio>
    
    int main(char argc, int argv[1])
    {
           printf("Hello World!\n");
    ; return 0;
    }

    Then save this as a .c in the folder of your choice.


    But if you want to make a hello world program in C++, however, which, in my opinion, I don't like very much.

    Repeat step 1, but this time, input the code below, instead of the code above:


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       std::cout << "Hello World" << std::endl;
    
    ; return 0;
    }
    This time, for step 3, save it AS A .CPP or it will not compile with GNU GCC or any compiler in general.


    Word of advice, I heavily suggest putting a semicolon both before and after the return statement, as compiling it without that will result in a error if you try to compile it and/or run it.


    Aaron out!!!!

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How to make a Hello World program in C/C++

    What error do you get exactly if you remove the semicolon before the return statement?

    The arguments to `main` should have types

    Code:
    int main(int argc, char *argv[])
    I think. The first is a count of the arguments, and the second is a pointer to an array of character strings representing the arguments themselves.

    And shouldn't <stdio> be <stdio.h>?

    Also from the man pages, the recognized C++ extensions are

    Code:
           file.cc
           file.cp
           file.cxx
           file.cpp
           file.CPP
           file.c++
           file.C
               C++ source code that must be preprocessed.  Note that in .cxx, the
               last two letters must both be literally x.  Likewise, .C refers to
               a literal capital C.
    although in fact you can use any extension so long as you indicate the language using the -x option.

  3. #3
    Join Date
    Jul 2015
    Beans
    15

    Re: How to make a Hello World program in C/C++

    Does Code::Blocks have the same functions and features as DevC++ offers?

  4. #4
    Join Date
    Aug 2015
    Location
    USA
    Beans
    213
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How to make a Hello World program in C/C++

    If you're using namespace std; you do not have to include the std:: in this line. -
    std::cout << "Hello World" << std::endl;

    You can just write it like - cout << "Hello World" << endl;

  5. #5
    Join Date
    Oct 2014
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: How to make a Hello World program in C/C++

    Personally, I think it's better with C -

    PHP Code:
    #include <stdio.h>

    int main(int argcchar *argv[])
    {
        
    puts("Hello, world!");                 // or printf("Hello, world!\n");

        
    return 0;

    and personally for C++ -

    PHP Code:
    #include <iostream>

    int main(int argcchar *argv[])
    {
        
    std::cout << "Hello, world\n";

        return 
    0;

    then you can compile both of them with

    Code:
    make filename
    or
    Code:
     g++ yourfilename.cpp -o yourfilename       # or g++ yourfilename.c -o yourfilename

    For IDE, I agreed with Aaron for Code::Blocks, it's nice and elegant, also cross platform(says you want to share the project structure so other people on different platforms can compile it too).

    and Anjuta if I want to dealing with Vala + GTK or gtkmm. Anyway, I just use vim for general editing.
    Last edited by flaymond; September 11th, 2015 at 04:12 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •