Preprocessor Assignments

Preprocessor assignments are similar to that of other languages, except they are prepended with a # sign.

Example:

#include <iostream>

Which is similar to Python and Groovy/Java, except they would read with a syntax more like: import “iostream.”  Ruby would use a require ‘iostream’ syntax, but in C++ it’s # sign, then “include” and the library you wish to include in <>.

Variable Assignment

Unlike Groovy and Ruby (where you don’t need to statically type your variables), in C you do need to statically define them.  For example:

int x;

Which creates a variable named x and sets it to an integer.  There is a danger here.  When x is created in memory, it has no default value so it takes whatever value is in that spot in memory.

You can and should assign a default value to the variable like so:

int x = 0;

A tip from learncpp.com, is to set the value to something that would help debug the application. For example… if you set:

int employees = -1;

You wouldn’t expect to have negative employees. This could find potential problems, if the variable assignment and usage isn’t correct int he program.  I.e. you create the variable employees and set it to default to -1… but it is never assigned a real value, and hence will error or provide inappropriate results.

Define a Variable only Once

If a variable has been defined:

int x = 1;

It can not be redefined later:

int x = 0;
int x = 1;

Will throw an error.

Variables can be assigned new values by simply doing:

int x = 0;
x = 1;

In the above example, x is now 1 and no longer 0.

Main Method

All C++ programs must have a main method like so:

int main() {
    cout << “Hello!” << endl;
    return 0;
}

Return

In C/C++ a return tells whether a function had an error.  A return of 0 is a response that the function worked. You could assign a value to return… such as return 5;  Unlike Groovy/Grails and other languages, C does not allow multiple items returned.

Output to Screen

In Ruby we use “puts” or “print” and in groovy it’s println or print.  In Python 2.7* we use print “something” or print(“something”) in Python 3.  In C it’s a bit less intuitive. The output to screen requires an include of the iostream library.  The command to output to screen is cout << “something”;

In Ruby puts (or p) auto sets end of line…. similarly in Groovy println sets an end of line automatically.  However in C, this must be specified with endl – for example:

cout << “something to write to screen” << endl;
cout << “and this is a new line” << endl;

Gathering Input

In Python we can get user input by assigning a variable like so: username = input(“What is your name? “)

In C this is handled with the cin (“C IN”) call… like so:

cout << “What is your age?” << endl;
int age = 0;
cin >> age;
cout << “You entered ” << age << endl;

Functions

A void function is one that returns no value. For me this was odd… as the example in the tutorial I was following would be returning a value in the Groovy sense.  Example – in Groovy the last calculated or expressed value in a method is what is returned (need not state ‘return’, it just returns it.)  Void in C means nothing is returned… but here is the C example of calling a void method:

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void DoPrint()
{
    using namespace std;
    cout << “In DoPrint()” << endl;
}
// Declaration of main()
int main()
{
    using namespace std;
    cout << “Starting main()” << endl;
    DoPrint(); // This is a function call to DoPrint()
    DoPrint(); // This is a function call to DoPrint()
    DoPrint(); // This is a function call to DoPrint()
    cout << “Ending main()” << endl;
    return 0;
}

In my mind I was thinking that DoPrint is returning the output “In DoPrint()”  … but I guess it’s not returning a value to a variable… it is simply executing a command.  Although I’m not too clear on that.

In this case, main is called first and runs through the main function printing “Starting main()” then it calls DoPrint and outputs In DoPrint(), and it does that 3 times… finally outputting Ending main() before exiting.

Parameters in C

As in Python, Groovy and Ruby, params can be taken by a method/function.  The main difference is, that in C, you statically define the params, just as you statically define variables… so:

int numbers(int x, int  y)
{
return x + y;
}
int main()
{
cout << numbers(6,66) << endl;
return 0;
}

 

#

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Archives
Categories