User Tools

Site Tools


opus:spring2012:swilli31:cprogpart2

cprog Keywords

typedef, enum, union

keywords

Definition

typedef: rename function in code aka alias union: a container that can contain more than one data type. it allocates memory to the largest data type within and only works with one data type at a time. can only be used when the things inside need not be used at the same time

Demonstration

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
     int i;
     union var{
          int x;
          float f;
     };
     typedef union var Uif;
     Uif value;
     value.x=0;
     for(i=0; i<24; i++)
     {
          value.x=value.x+rand()%51+1;
     }
     printf("total is %d\n", value.x);
     for(i=0; i<73; i++)
     {
          value.f=value.f+rand()%27+0.1;
     }
     printf(total is %f\n, value.f);
     }
     return(0);
}
 

this program utilizes typedef and a union

arithmetic

equations, operations

Definition

the basic arithmetic functions learned in math class applied to bits and code

Demonstration

#include <stdio.h>
 
int main()
{
    int i=value=0;
    for(i=0; i<33; i++)
    {
         value=i+3
    }
    return(0);
}

This code increments the integer i by 3 and cycles through the loop until i=33 then exits

Variables

types, ranges, sizes

Definition

the basic deciding factors of how much memory needs to be allocated for a program

Demonstration

lab46:~/src/cprog$ $ ./range 
An unsigned char is 1 bytes
The range of an unsigned char is 0 to 255
An unsigned char can store 256 unique values

A signed char is 1 bytes
The range of a signed char is -128 to 127
An unsigned short int is 2 bytes
The range of an unsigned short int is 0 to 65535
An unsigned short int can store 65535 unique values

A signed short int is 2 bytes
The range of a signed short int is -32768 to 32767
An unsigned int is 4 bytes
The range of an unsigned int is 0 to 255
An unsigned int can store 4294967295 unique values

A signed int is 4 bytes
The range of a signed int is 0 to -1
An unsigned long int is 8 bytes
The range of an unsigned long int is 0 to 255
An unsigned long int can store 18446744073709551615 unique values

A signed long int is 8 bytes
The range of a signed long int is 1 to -2
An unsigned long long int is 8 bytes
The range of an unsigned long long int is 0 to 255
An unsigned long long int can store 18446744073709551615 unique values

A signed long long int is 8 bytes
The range of a signed long long int is 1 to -2
lab46:~/src/cprog$ 

This is a printout of the results of project 0

Functions

return types, pass by value, address reference

Definition

contains smaller operations within a bigger operation

Demonstration

#include <stdio.h>
#include <stdlib.h>
 
int sum(int, int, int, int); //function prototype
float avg(int, int, int, int);
int numset(int, int, int, int);
 
int main()
{
     int a, b, c, d;
     a=b=c=d=0;
     printf("Enter first value: ");
     fscanf(stdin, "%d", &a);
     printf("Enter second value: ");
     fscanf(stdin, "%d", &b);
     printf("Enter third value: ");
     fscanf(stdin, "%d", &c);
     printf("Enter fourth value: ");
     fscanf(stdin, "%d", &d);
 
     fprintf(stdout, "the sum of %d, %d, %d, and %d is %d\n", a, b, c, d, sum(a,b,c,d));
     fprintf(stdout, "the average of %d, %d, %d,and %d is %f\n, a,b,c,d, avg(a,b,c,d));
     return(0);
 
int sum(int n1, int n2, int n3, int n4)
{
     int total=0;
     total=n1+n2+n3+n4
     return(total);
}
 
float avg(int n1, int n2, int n3, int n4)
{
     float avgerage=0
     avgerage=(n1+n2+n3+n4)/4);
     return(avgerage);
}
lab46:~src/cprog$ ./function1
Enter first value: 5
Enter second value: 7
Enter third value: 8
Enter fourth value: 9
the sum of 5,7,8, and 9 is 29
the average of 5,7,8, and 9 is 7.000000
lab46:~src/cprog$

This is the code and output of the program function1.c written in class

Compiler

preprocessor, flags, assembly linker…

Definition

takes the code written in a text editor and makes it machine readable and executable

Demonstration

lab46:~/src/cprog$ gcc -o function1 function1.c
lab46:~/src/cprog$

This shows that the code written in function1.c was successfully compiled to executable form in function1. There are flags and warnings and errors that can appear that state if there is a segmentation fault or a syntax error that will not allow the program to compile.

Scope

Block, Local, Global, File

Definition

the part of code in which identifier can be referenced.

From broadest to narrow: application, file, function, block

Taken from lrdev.com

Selection

if, case/switch

Definition

When there is a series of if/else statements in a code a case switch may be used

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

#include <stdio.h>
 
main()
 
int menu numb1, numb2, total;
printf(" enter in two numbers-->");
scanf("%d, %d", &numb1, &numb2);
printf("enter in choice\n")
printf("1=addition\n");
printf("2=subtraction\n");
scanf("%d", &menu);
 
switch(menu){
     case 1: total=numb1+numb2; break;
     case 2: total=numb1-numb2; break;
     default: printf("Invalid option selected\n")
}
 
     if(menu==1)
     {
         printf("%d plus %d is %d\n", numb1, numb2, total);
     }
     else 
     {
          printf(%d minus %d is %d\n", numb1, numb2, total);
     }
     return(0);
}

Example taken from gd.tuwien.ac.at/language/c/programming-bbrown/c_028htm

Structures

Declarations, Accessing Elements, Pointers to

Definition

basically the same as a union except it will allocate memory for each member but does not share memory

Demonstration

#include <stdio.h>

int main()
{
     struct person{
     char *name;
     unsigned char age;
     short int weight;
     float gpa;
     };
     return(0);
}

This is the setup for a struct based on characteristics of a person

cprog Objective

cprog Objective

Distinguish and explain the difference between homogeneous and heterogeneous composite data types

Definition

Can the student correctly identify a homogeneous composite data type and a heterogenous composite data type?

Method

Write a code for both a heterogeneous and homgeneous containers

Measurement

<code> #include <stdio.h>

int main() {

   struct time{
        float sec;
        char min;
        int hour;
        short int day;
   };
   
   long int century[20]
   return(0);

}

Analysis

This is not a complete view of the code but it does show the difference in how each of these types are set up. The struct allocated memory for each of the elements within and accessed them. There are many different data types within the struct. When trying to add different data types to the array century the compiler will not allow because it only a long int can go into century. The array is homogeneous and the struct is heterogeneous.

opus/spring2012/swilli31/cprogpart2.txt · Last modified: 2012/03/29 15:10 by swilli31