Table of Contents

cprog Keywords

cprog Keyword: Pointers

Definition

Pointers to me are used as a way of referencing something without calling it directly, and they are useful as I have seen because you can change the location a pointer points at depending on the situation and outcome you desire. You could even use an array of pointers to complete a task. I look at pointers as a reference or indirect direct way to change and manipulate something without using the thing that is being changed or manipulated.

Demonstration

To use pointers, there are a couple of things to consider:

To demonstrate pointers, I will show a code block using pointers and its output.

#include <stdio.h>                                                                                      
#include <stdlib.h>                                                                                    
 
int main(){                                                                                             
        char *value, i;                                                                                 
        value = (char *)malloc(sizeof(char)*4);                                                         
        *(value+0)=0;                                                                                   
        *(value+1)=1;                                                                                  
        fprintf(stdout, "Please enter a value (-128 - +127): ");                                       
        fscanf(stdin, "%hhd", &i);                                                                      
        *(value+2)=i;                                                                                   
        *(value+3)=(2*i)+3;
        for (i = 0; i <= 3; i++){                                                                       
                printf("%hhd\n", *(value+i));
        }                                                                                               
        return(0);                                                                                     
} 
lab46:~$ gcc -o prog7 prog7.c
lab46:~$ ./prog7
Please enter a value (-128 - +127): 8                                                                   
0                                                                                                       
1
8                                                                                                       
19
lab46:~$ 

cprog Keyword: Selection Structures (if, case/switch)

Definition

Selection Structures are conditional statements that determine a path to take depending on the state of the condition. (True/False)

Demonstration

To demonstrate a Selection Structure, I will be using the If Selection Structure.

if (input > 4 || input < 0){
   printf("You have entered an incorrect value.\n");
   check = false;
}else{
   for (i = 0; i < argc; i++){
   printf("argv[%hhu][%hhu]: %c\n", i, input, *(*(argv+i)+input));
}

cprog Keyword: Repetition/Iteration Structures (for, while, do while)

Definition

Repetition/Iteration Structures are a way of repeating certain contents of code until a condition is satisfied. They are usually defined mostly by whether a statement is True or False, and in the case of a while, as long as the statement is True, the while will continue to iterate.

Demonstration

As a demonstration, I will show a code example of a while loop and a for loop.

while (check == false){
   printf("Enter a value (0-4):");
   scanf("%d", &input);
   if (input > 4 || input < 0){
      printf("You have entered an incorrect value.\n");
      check = false;
   }else{
      for (i = 0; i < argc; i++){
         printf("argv[%hhu][%hhu]: %c\n", i, input, *(*(argv+i)+input));
      }
      check = true;
   }
}

cprog Keyword: Header Files (Local and System), C Standard Library (Libc), Libraries

Definition

Header Files are files that are applied to c code to incorporate libraries for use of their functions and data structures.

Demonstration

This demonstration will show a couple header files being included in a c code environment.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

cprog Keyword: logic and operators (and, or, not, xor)

Definition

Logic and operators include devices for comparing and assigning variables. For example: int x = 0 (The '=' assigns the integer value of 0 to the integer variable x).

Demonstration

Demonstration for this keyword will include using AND (&&), OR (||), GREATER THAN (>), LESS THAN (<), and the Assignment Operator ( = ).

if (input > 4 || input < 0){
   check = false;
}
if (input > 4 && input < 0){
   check = true;
}

cprog Keyword: Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)

Definition

Arrays can be thought of as containers with indexes to each element in their ordered container. The ability to store data separately within a container and be able to access it easily with an index is the reason why Arrays are so useful. Arrays to me are dynamic storage containers with easily accessed schemes of data.

Demonstration

In this demonstration, I will be showing in c code some different Array types.

int argc; //Array of integers
char **argv; //Array of character pointers
int x[2]; //Array of integers with a size of 2
int t[2][2]; //Multidimensional Array with a Row size of 2 and Column size of 2

cprog Keyword: Standard I/O (STDIO, STDOUT, STDERR)

Definition

Standard I/O is a library of input, output, and error message types to be prompted to the terminal screen. Standard I/O must be included in a programming file such as .c for its input, output, and error message functions to work.

Demonstration

What follows is a demonstration of including Standard I/O to a .c file and using some of its functions to perform prompting tasks.

#include <stdio.h>
 
fprintf(stdout, "Please enter a value (-128 - +127): ");
fscanf(stdin, "%hhd", &i);

cprog Keyword: Functions

Definition

Functions as used by me are a set of instructions that can be defined by a name and referenced by the main part of a program to complete some sort of sub task to a greater full task.

Demonstration

Here is an example of functions in the language c:

  1 #include <stdio.h>
  2
  3 int sum(int, int, int, int);
  4 float average(int, int, int, int);
  5
  6 int main(){
  7         int a, b, c, d, high, low;
  8         a = b = c = d = 0;
  9         printf("Enter first value: ");
 10         fscanf(stdin, "%d", &a);
 11         printf("Enter second value: ");
 12         fscanf(stdin, "%d", &b);
 13         if (a > b){
 14                 high = a;
 15                 low = b;
 16         }else{
 17                 low = a;
 18                 high = b;
 19         }
 20         printf("Enter third value: ");
 21         fscanf(stdin, "%d", &c);
 22         if (c > high){
 23                 high = c;
 24         }else{
 25                 if (c < low){
 26                         low = c;
 27                 }
 28         }
 29         printf("Enter fourth value: ");
 30         fscanf(stdin, "%d", &d);
 31         if (d > high){
 32                 high = d;
 33         }else{
 34                 if (d < low){
 35                         low = d;
 36                 }
 37         }
 38         fprintf(stdout, "The sum of %d, %d, %d, and %d is: %d\n", a, b, c, d, sum(a, b, c, d));
 39         fprintf(stdout, "The average of %d, %d, %d, and %d is: %f\n", a, b, c, d, average(a, b, c, d));
 40         fprintf(stdout, "The highest input is: %d\n", high);
 41         fprintf(stdout, "The lowest input is: %d\n", low);
 42         return(0);
 43 }
 44
 45 int sum(int n1, int n2, int n3, int n4){
 46         return(n1 + n2 + n3 + n4);
 47 }
 48
 49 float average(int n1, int n2, int n3, int n4){
 50         return((float)(n1 + n2 + n3 + n4)/4);
 51 }

cprog Objective

cprog Objective

Write and compile code that utilizes programming constructs.

Definition

This objective entails the use of nano or vi along with the use of gcc to write and compile code within a certain standard of syntax and purpose.

Method

The method to which I will use to measure my success in this objective is to show the steps and examples of how I have already completed this objective.

Measurement

Steps to completing this objective include:

1. The use of nano or vi to edit text and create a .c file type.

lab46:~$ nano test.c 
lab46:~$ vim test.c 
#include <stdio.h>
 
int main()
{
    printf("Testing a printf.");
    return(0);
}

2. The use of gcc to compile code and check the syntax of the code.

lab46:~$ gcc -o test test.c
lab46:~$ 

3. Testing the code by running the file that I have compiled the .c file to.

lab46:~$ ./test
Testing a printf.
lab46:~$ 

Analysis

Reflections: