C/C++ made easy with GoGooSE 1.0

CONTENT

1.0 BASICS

1.1 Whats C/C++?? 

1.2 How to make proggies out of source code??


2.0 QUICKSTART

2.1 Take a Quickstart:

2.2.1 So lets begin... 

2.2.2 The Hello World Proggie

2.2.3 Hello C

2.2.4 Hello C++

2.2 What does that mean??


3.0 A CLOSER LOOK:

3.1 Overview

3.2 Includes:

3.3 Defines:

3.4 Variables: 

3.5 Functions:

3.6 Table of possible function assignments


4.0 LIBRARY SURFIN:

4.1 C

4.1.1 Output

4.1.3 Returning

4.2 C++

4.3 Tables

4.4 Standard


5.0 OPERATORS:

5.1 Basic

5.2 Mathematical

5.3 Logical

5.4 Relational

6.0 LOOPS

6.1 Basics

6.2 IF

6.3 FOR

6.4 WHILE

6.5 SWITCH

One of the early Turorials by GoGooSE. please mail, if links are broken,
or with any open Questions


1.0 BASICS

1.1 What´s C/C++??

If somebody is really interested in how C/C++ could get so damn popular, read
the links below.

I´m not in the mood to list the whole History.

http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

http://www.hitmill.com/programming/cpp/cppHistory.asp


1.2 How to make proggies of out source code??

Well, on Unix the solve of this Problem isn´t far off. You simply type 

cc -o [input file] [output file]

With Windows, you will get the Problem, that C Compiling isn´t implemented by Microsoft.
So you need to do that work for yourself...

Go on Google and type in something like "C+Compiler" or "compile C code".

Or just try these links; case they´re working:

http://www.borland.com/products/downloads/download_cbuilder.html
http://www.c-compiler.com/
http://sdcc.sourceforge.net/ 

2.0 QUICKSTART

2.1 Take a Quickstart:

Instead of writing to you the whole stdlib of C, I will still give you a glance on it,
and focus the Syntax. I think the understanding of the Syntax is much more usefull
than stupidly learn some words. or am i wrong??

2.1.1 So lets begin...

C/C++ isn´t that dificult as it may be suggested by some "Hardcore Programming-freaks". 
Lets start with a simple "Hello World Programm, wich is the most known, and 
easiest to proram Example

C-Source

#include <stdio.h>

void main(void) {
printf("Hello World, thats me");
return;
}


C++-Source

#include <iostream.h>

int main(void);

int main(void) {
cout<<"Hello World, thats me";
return 0;
}

2.2 What does that mean??

Now we will discuss every single step of the two sources:

#include <included library> --> You use this to define standard library´s, used in your Program: These 
Library´s are full of useable function´s wich will make your work with 
C/C++ very handy.

In our two example-sources, we used <stdio.h> (including the printf() funtion)
<iostream.h> (including definitions for >> and << Operators)

void main(void) --> definition of main function. This "main" function is needed by every programm as default function

printf("string") --> This function is used to write characters to stdout.

cout<<"string" --> The C++ Version of printf() 

return --> Return from a function (Can also give back values, but later on..) 

3.0 A CLOSER LOOK:

3.1 Overview

A C-Source is splited in several parts. Some of them are optional. 

1. The "Includes Section", where header files are defined.
i.e.: #include <stdio.h> (Would define the stdio header file to be included)

2. The "Defines Section", where defines where made
i.e.: #define RAND_MAX 200 (Would define the Constant RAND_MAX to be 200)

3. The "Variabs Section" here you,ll define global Variabs, structs or classes

4. The "Function Section" Here is the part of your Program, where you define, what to execute.
i.e.: void main(void) {} // int here(char c) {}

Now lets take every single Section:

3.2 Includes:

The string "#include" starts the definition of a header files. Read the documentation of your 
C/C++ Compiler to get Information about, what´s in your Header Files. You can easily extend 
your compiler, by adding new librarys, i.e. from the net

3.3 Defines:

To understand this you need a basic Knowledge of Variables. Just remember, that you can define a Constant
with the string "#define [CONST-NAME] [CONST-VALUE]"

3.4 Variables: 

Variables are one of the basic features of every programming language. You can easily save and retrieve
Information, by giving it a name and a adress on the heap.

In C/C++ you need to define every Variable type-specific. That means, that the compiler needs to know
wich type of Information you put into a variable. Following types are present.

int /* simple integer type */
long int /* long integer type */
short int /* short integer type */
unsigned int /* unsigned integer type */
char /* character type */
float /* floating point type */
double /* double precision floating point */

char 1 -128 to 127
signed char 1 -128 to 127
unsigned char 1 0 to 255

int 2 -32,768 to 32,767
unsigned int 2 0 to 65,535

short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535

long int 4 -2,147,483,648 to
2,147,483,647
unsigned long 4 0 to 4,294,967,295

float 4 3.4E-38 to 3.4E+38

double 8 1.7E-308 to 1.7E+308

long double 10 1.2E-4932 to 1.2E+4932




You can assign a Variable like this:

type | name | (optional)= | (optional)value ;
------------------------------------------------
int number = 30 ;

char abc = 'abc' ;

Now you´ll also understand the misterious defines.

3.5 Functions:

This is the most important part. It will include every thing, your program does.

To define a new function you must specify some things:

1. The "Return-type" i.e. The function rand() returns a random integer. it would be defined as follows:

int rand(void)

2. The Name and ..

3. The "Call-type" i.e. The function printf(variab) needs to know what to put out. So you will tell it the value of
your Message or the variab, teh mess is saved in, by doing this:

printf("string(Your message)") or printf("var_type", variab_with_mess_in_it)


3.6 Table of possible function assignments

This table shows how to do this

return type name call type

int main void main function, wich returns with integer value

int check char a gets a character and returns witch value

void find int index gets integer value and returns without anything


"VOID" means nothing more than "nothing" you use it to tell the compiler, that you dont need a variable.


4.0 LIBRARY SURFIN

In the end i will give you definitions of some usefull basic functions to try programming.

4.1 C

4.1.1 Output

C gives you several tools to do Output to the system

Basically use the printf() function. Here a list:

NAME SYNTAX DESCRIPTION

printf() printf("string" or variable) view list of var_types
printf("var_type", variable) 

putchar() var[x]=putchar() Puts single char


4.1.2 Input

NAME SYNTAX DESCRIPTION

scanf() scanf("var_type", variable) view list of var_types

getchar() var[x]=getchar()


4.1.3 Returning (C/C++)

NAME SYNTAX DESCRIPTION

exit() exit(code) returns completly from a program 

return rerturn code returns from a function, or if used in main() from the program
4.2 C++

4.2.1 Output

NAME SYNTAX DESCRIPTION

cout cout<<"string" put string to stdout
cout<<var put var to stdout

4.2.2 Input

NAME SYNTAX DESCRIPTION

cin cin>>var get var from stdin

4.3 Tables

printf "var-type" Table

name variab-type

%d Integer
%f Float
%c char
%s string 


4.4 Standard

NAME SYNTAX DESCRIPTION INCLUDE

rand() var=rand() gets random number math.h






5.0 OPERATORS:

5.1 Basic

Table of Basic C/C++ Operators

= is (asign value to variab) [var]=[value]
; defines end of codeline
* value of (i.e. to a pointer)
& adress of -""-

5.2 Mathematical

Table of Mathematical C/C++ Operators

+ addition
++ increment
- unary minus and subtraction
-- decrement
* multiplication
/ division
% remainder (modules operator)

5.3 Logical

| or (a | b) == a or b
&& and (a && b) == a && b
! not (a ! b) == a not b


5.4 Relational

Table of Relational C/C++ Operators

== equal; compare Operator i.e. a==b;; is value a = value b??
< smaller
<= smaller or equal
> greater
>= greater or equal
!= not is

6.0 LOOPS

6.1 Basics

Loops help you doing many Operations with only a
few lines of code. I.e., we want to put out the numbers 
from 1 to 100. We could easily do that by typing cout<<"1"<<"2"<<"3"...
But that would take very much typingtime. You would use a for or a while
loop instead. 3 lines code!!

6.2 IF

With the if statement, you can perform a test on a peace of information (the statement).
It looks like this

this is the statement 

V
if([info1]relational_operator[info2]) {
}
else {
}

[info1], [info2] points to a variable, string, number, etc...
relational_operator is !=, ==, <=...
the else part shows the program wich code to execute, if the statement is false.
the brackets are only needed if you process more than on line with your if
statement; otherwise you would use if(xxx) exit(0);

IF performs the requested operation only if the statement in brackets is true.

6.3 FOR

with FOR you can perform repeated operations. FOR repeats the operations in {} brackets
until the statement in () Brackets is false.

for(int a=0; a<=100; a++) {
cout<<a;
}

this would print out the numbers 0-100 to stdout.

for( ..... initializes the FOR loop
int a=0; ..... gives a the value 0 at first time running; Note that C only allow variable
declarations at the beginning of a function(local), or in front of the main
function (global) the upper statement int a=0; would lead to an error, because
need to be defined in the beginning of the function. In C++ this is legal.

a<=100; ..... as long as a is smaller or equal to 100
a++; ..... increment a at every loop

6.4 WHILE

with WHILE you can perform repeated operations. WHILE repeats the operations in {} brackets
until the statement in () Brackets is true.

do {
a++;
} while(a!=100); 

or

while(a<=100) {
a++;
}

do {, while { ..... initializes the WHILE loop
a!=100 ..... while a not is 100
a++
..... increment a
6.5 SWITCH

the SWITCH loop lets you define multiple operation for multiple values.

int a;
cout<<"\nWhat you wana do?";
cin>>a;
switch(a){
case 1: makeone();
c++;
cout<<"\nNice day huh??";
break; 

case 2: maketwo();
break;

default: cout<<"\nwhat??"
break;
}

switch(var) { ..... opens SWITCH on variable a
case 1: ..... like if but without {} brackets, use 'x' to check for chars
break ..... breaks out of the case. (needed to start new case)
default: ..... what to do if none of the cases is true??


GSO
Written on Saturday, 03 October 2009 20:21 by GSO

Viewed 232 times so far.
Like this? Tweet it to your followers!

Rate this article

Latest articles from GSO

Latest 'tweets' from GovernmentSecurity

  • News Update: Cyber war is coming, the impact could be huge: CBS News reports that cyber.. http://bit.ly/1tx1kr | #Security Link Monday, 09 November 2009 07:35
  • News Update: Tenable Network #Security Podcast - Episode 11: Welcome to the Tenable Netw.. http://bit.ly/2Iqd6G | Security Link Monday, 09 November 2009 07:35
  • News Update: Consent will be required for cookies in Europe: EDITORIAL: A law that dema.. http://bit.ly/3JYgip | #Security Link Monday, 09 November 2009 07:35
  • News Update: CBS 60 Minutes tackles cyber-terrorism: Could hackers get into the compute.. http://bit.ly/2d5Y21 | #Security Link Monday, 09 November 2009 07:35
  • Blog Update: We have launched the new GovernmentSecurity.org: We decided to launch th.. http://bit.ly/2G1SSF | #Security Link Saturday, 07 November 2009 17:38
blog comments powered by Disqus

Site Search

Disqus Tools