User Tools

Site Tools


opus:spring2012:brobbin4:cprogpart2

cprog Keywords

typedef, enum, union

Definition

Typedef allows a user to define there own identifiers. These new identifiers can be used in place of type specifiers such as int, float, and double. A typedef declaration doesn't allocate any additional storage and the names defined using typedef are not new data types rather they are merely synonyms for the data types they represent. When an object is defined using a typedef identifier, the properties of the defined object are exactly the same as the original data type they represent.

Enum short for “enumerated data”. enums allow a user to define a fixed set of words that a variable of type enum can use as its value. The words are assigned integer values by the compiler so enum values can be compared.

A union is comprised of a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at a time. Once a new value is assigned to a field, any existing data is overwritten with new data. The use of unions can save memory if you have a grouping of data and only one of the types is used at a time. The size of a union is dependent on the size of it's largest data member.

Type Casting

Definition

Type casting also known as type conversion is the concept of converting one data type to another data type. For example you might have a float data type that needs to be an int data type for use in some part of a code.

Demonstration

Below is an example of Type Casting in C.

#include <stdio.h> 
 
int main()       
{
  /* The (char) is a typecast. Its telling the computer to interpret the 32 as a
     character, not as a number.  The output of will be the ASCII equivalent of
     the number 65  (It should be the letter A for ASCII). Note that the %c below
     is the format code for printing a single character
   */
  printf( "%c\n", (char)32 );
  getchar();
}

Scope

Definition

Scope refers to the way data is declared and accessed within a program.

Global scope, found in C++, occurs when a name is declared outside of all blocks, namespaces, and classes. The name is accessible anywhere after its declaration.

Local scope also know as Block Scope, occurs when a name is declared within a block. The name can be used within the block that it was created in and any child blocks that are enclosed within the parent block.

File scope, found in C, occurs when a name is declared outside all blocks or classes. The name is accessible anywhere after it is declaration.

Demonstration

Below is a demonstration of Scope.

/*
 * Sample code block
 */
#include <stdio.h>
 
void test{int} //This function prototype has file scope because it is defined outside any block, namespace, or class.
 
int main()
{
   int z=0; //This variable has been declared within a block of code this giving this block scope.
 
   return(0);
}

Selection Structures

Definition

Selection structures also known as control structures are special structures found in the C and C++ programming language. These structures allow for one of multiple possible scenarios to occur based on the value of a variable.

Demonstration

Below is an example of an If structure.

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    int a;
    int b;
    int c;
 
    if (a > b)
    {
        c = a * b;
    }
    else
    {
        a = b * c;
    }
 
    return(0);
}

Below is an example of a case/switch structure.

/*
 * Sample code block
 */
#include <stdio.h>

int main()
{
    char a;
    char b;
    int c;
    int d;
    
    switch(d)
        {
        
            case 1:
            
                statement;
            
                break;
            
            case 2:
        
                statement;
            
                break;
            
            case 3:
        
                statement;
            
                break;      
                
        }
        
    return(0)
}

Logic and Operators

Definition

Logic can be defined as the non-arithmetic operations performed by a computer, such as sorting, comparing, and matching, that involve true-false decisions.

Operators can be defined as symbols that are used to perform operations. There are 8 logic operators available in the C and C++ programming language and those are !, &&, ||, ^, !=, &=, |=, ^=.

The “!” notates logical “NOT”

The “&&” notates logical “AND”

The “||” notates logical “OR”

The “^” notates logical “XOR”

The “!=” notates logical “NOT_EQ”

The “&=” notates logical “AND_EQ”

The “|=” notates logical “OR_EQ”

The “^=” notates logical “XOR_EQ”

Arithmetic

Definition

Arithmetic is the portion of mathematics that deals usually with the non-negative real numbers including sometimes the transfinite cardinals and with the application of the operations of addition, subtraction, multiplication, and division to them

Operators can be defined as symbols that are used to perform arithmetic operations.

Demonstration

Below is an example of arithmetic in C along with some of the basic mathematical operators available in the C language.

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    int a=20;
    int b=5;
    int c=0;
 
    a + b = c; //Add
 
    a - b = c; //Subtract
 
    a * b = c; //Multiply
 
    a / b = c; //Divide
 
    a % b = c; //Modulo(Remainder)
 
    return(0);
}

Structures

Definition

A structure can be defined as a collection of variables that can be accessed under one variable name thus providing a way to keep related information grouped together.

Demonstration

Below is an example of structures found in the C language.

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    int t;
 
    struct //This keyword tells the system a structure is being declared.
    {
        char name[30];
        char street[40];
        char city[20];
        char state[3];
        unsigned long int zip;
    } addr_info;
 
    addr_info.zip = 14830; //Here the zip element is accessed and the zip is written to it.
 
    gets(addr_info.name); //This statement passes a character pointer to the beginning of name.
 
    for(t=0; addr_info.name[t]; ++t)
 
    putchar(addr_info.name[t]);
 
    return(0);
}

Namespaces

Definition

Namespaces are an optionally named scope. Names can be declared inside a namespace just as you would for a class or an enumeration. You can access names declared inside a namespace in the same manner you access a nested class name by using the scope resolution (::) operator. Namespaces however, do not have the additional features that classes or enumerations possess. The main purpose of namespaces are to add an additional identifier (the name of the namespace) to a name.

Demonstration

Below is an example of namespaces as found in the C++ language.

/*
 * Sample code block
 */
#include <iostream>
using namespace std;
 
namespace primary
{
  int var = 5;
}
 
namespace secondary
{
  double var = 3.1416;
}
 
int main () 
{
 
    cout << primary::var << endl;
 
    cout << secondary::var << endl;
 
    return 0;
}

cprog Objective

opus/spring2012/brobbin4/cprogpart2.txt · Last modified: 2012/05/09 06:43 by brobbin4