element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ Tutorial - Hello Raspberry Pi
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: oneleggedredcow
  • Date Created: 2 Jan 2013 2:53 AM Date Created
  • Views 11360 views
  • Likes 9 likes
  • Comments 13 comments
  • Code Exchange
  • raspberry_pi
  • code
  • rpi
  • c++
  • learnc++
Related
Recommended

C++ Tutorial - Hello Raspberry Pi

oneleggedredcow
oneleggedredcow
2 Jan 2013

Background

In this tutorial series, we are going to learn modern c++ using the raspberry pi.  So, what do I mean when I say modern?  In August of 2011, a new version of the c++ standard was released that included some exciting new features.  So, in this tutorial series, we will be taking advantage of these enhancements.

 

This tutorial series will start with the basics and then quickly move on to more advanced topics. Occasionally, we will take a step back and use what we have learned so far to do a project, so that you can exercise the skills that you have learned and build something exciting.  By the end, you should have a great foundation in c++ that you can use to build your own projects.

Setup

For this tutorial series, we are going to be using the Raspbian OS.  It’s very easy to install and set up and it seems to be the most popular OS for the raspberry pi.  Raspbian comes with all of the tools that we need to start programming, so let’s dive in.

Code

Here’s the code to print out a message to the screen:

 

#include <iostream>

using namespace std;

 

 

int main()

{

          cout << "Hello Raspberry Pi!" << endl;

          return 0;

}

 

The first line includes a library.  Libraries are prewritten code that you can leverage to do things faster and easier. Many common functions, such as writing a message to the screen, have already been written, so you can just reuse them and focus on aspects of your program that are unique to the problem that you are trying to solve.

 

The next line makes it so that you don’t have to prefix the functions in the library with the name of the namespace that it is in.  This is to help fix name collision problems.  Imagine if I wrote a function called foo and someone else wrote a function with the exact same name.  If I tried to call that function the computer wouldn’t know which one I wanted, so to help with this namespaces were created.  This line tells the computer that if it is looking for a function, that it should start by looking in the std namespace.

 

The next line declares a function.  A function is a group of code that takes a set of inputs and produces an output.  A simple example would be a sine function. The function would take in an angle, do some computations on that angle and then return the sine of the given angle. This function is called main and it doesn’t take any inputs, so there are just empty parenthesis after the name of the function.  The function returns an error status.  This is used to let other programs know if the program succeeded or failed.  The main function is a very special function. It lets the computer know where your program starts, so every program must have a main function.

 

The curly braces signify the start and end of the function.  Inside the function we only have two lines. The first one writes the phrase given to the screen, and the second one tells the computer that the program succeeded.  If you change the text in between the two double quotes, you can make the computer write anything that you want to the screen.

Compiling and Running

So, take the above code and paste it into a file called 01_HelloRPi.cpp.  Now open up a command prompt and navigate to the directory that contains the file.  In the command prompt type:

 

g++ -std=c++0x 01_HelloRPi.cpp -o01_HelloRPi

 

This will take the code that you have written and create a program that can be run.  The name of the program to create is specified with the –o flag, in this case we called it 01_HelloRPi.  The –std=c++0x flag tells the compiler to use the latest version of the c++ standard.  Then to run the program type in:

 

./01_HelloRPi

 

And that will write your message to the screen.

Summary

In this first tutorial we set up our environment to get ready to code and wrote a small program that prints a message to the screen.

 

In the next tutorial, we discuss how to read in data from the user to make an interactive program.

 

If you have any questions or comments about what was covered here, post them to the comments.  I watch them closely and will respond and try to help you out.

Tutorial Index

01 - Hello Raspberry Pi

02 - Reading User Input

03 - If Statement

04 - For Loops

05 - While Loops

06 - Functions

07 - Data Types

08 - Arrays

09 - Structures

10 - Classes

11 - Pointers

12 - Dynamic Arrays

13 - Linked List

14 - Linked List Operations

15 - STL List/Vector

16 - Templates

17 - Inheritance

18 - File I/O

19 - Strings

20 - Constants

21 - Hash Tables

Attachments:
01_HelloRPi.cpp.zip
  • Sign in to reply

Top Comments

  • oneleggedredcow
    oneleggedredcow over 10 years ago in reply to shabaz +3
    Thanks shabaz , it was great to see your comments today! I also updated my previous response. (That's what I get for trying to answer a question in a rush!)
  • mcb1
    mcb1 over 10 years ago in reply to shabaz +3
    Well said Shabaz!. Strangely it takes some people a lot of time to learn that constructive critisism usually results in a better outcome. Using your own experiences with poor service/experience and projecting…
  • Anonymous-342052
    Anonymous-342052 over 10 years ago +3
    This is a REALLY COOL tutorial
  • oneleggedredcow
    oneleggedredcow over 9 years ago in reply to Former Member

    Yes, it is the error code that is sent to the OS after the program is run.  This is a bit special since it is the main function that is run when the program is executed.

     

    In general, functions either return void or return a value.  The value can be of any type (int, double, etc.).  Generally it is a good idea to return status if the execution of the function can fail.  (The other option is exceptions.)

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago

    Thanks for this nice tutorial. It is admirable that somebody spents so much time to help others. I'm a total newbie, and it's my first contact with c++.

    So my first question to chapter 1 is : what is the meaning of the "int" in front of the function main? Does it refer to the error code as integer? So is it always used to define a return code for a function?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • workless
    workless over 10 years ago

    Thanks Shaun and others.  I found the bug in my code and I am now happily compiling C code on my raspberry pi.  Great advice!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Anonymous-342052
    Anonymous-342052 over 10 years ago

    This is a REALLY COOL tutorial image

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 10 years ago in reply to shabaz

    Well said Shabaz!. image

     

    Strangely it takes some people a lot of time to learn that constructive critisism usually results in a better outcome.

    Using your own experiences with poor service/experience and projecting that forward onto other experiences will often result in the same problem ... another poor experience.

     

    Many clever people lack some of the other life skills, and many with good life skills lack technical skills, so sadly they need each other.

    It can be learnt, but does take some time and a few hints from either side to arrive at the right direction.

     

    Mark

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube