Table of Contents

cprog Keywords

The following is a list of the major topics being covered in this course:

  1. Standard I/O (STDIO, STDOUT, STDERR) (done)
  2. Header Files (Local and System), C Standard Library (Libc), Libraries (done)
  3. arithmetic (equations, operators) (done)
  4. logic and operators (and, or, not, xor) (done)
  5. Variables (types, ranges, sizes) (done)
  6. Scope (Block, Local, Global, File)
  7. Pointers (address of, assignment, dereferencing)(done)
  8. Type Casting (done)

cprog Keyword 1

Standard I/O

Definition

Standard I/O covers a the broad range of standard input, standard output and standard error. With a Standard I/O all data is treated and read the same way without having to compile sources of data. STDIO is what is typed or fed to the machine. For example, you enter a command at the prompt and run the program. Another example, the standard allows the flexibility of shell scripting to be entered and treated the same as input from the keyboard. The standard format is what gives the system flexibility.

The two general ways to print or output data are Standard Output (STDOUT) and Standard Error (STDERR). The result of STDIO is that once data is entered it is then output (STDOUT) to the screen or file. Simple in meaning; however, there are many ways to direct the output depending on your needs. The last output form is the error handling STDIN. This sub group allows standard message and handling of errors that are imputed to the terminal.

Demonstration

An example of standard error output is shown bellow. Here a program was written to add two numbers. The compiler has gone through and looked at the code and then presented this output to the console. Giving the results of incompatibility within the written structure.

lab46:~/junk$ gcc dem1.c -o dem1.out
dem1.c:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
dem1.c:11: error: stray '\' in program

cprog Keyword 2

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

Definition

Header files contain functions that can be used within the program. Each header file is called out in the following manner within the code.

#include <stdio.h>
#include <math.h>

Demonstration

There are many macros, functions and variables that are included in header files. The following example is a list of what is the stdio.h header file.

Macros:

NULL _IOFBF _IOLBF _IONBF BUFSIZ EOF FOPEN_MAX FILENAME_MAX L_tmpnam SEEK_CUR SEEK_END SEEK_SET TMP_MAX stderr stdin stdout

Functions:

clearerr(); fclose(); feof(); ferror(); fflush(); fgetpos(); fopen(); fread(); freopen(); fseek(); fsetpos(); ftell(); fwrite(); remove(); rename(); rewind(); setbuf(); setvbuf(); tmpfile(); tmpnam(); fprintf(); fscanf(); printf(); scanf(); sprintf(); sscanf(); vfprintf(); vprintf(); vsprintf(); fgetc(); fgets(); fputc(); fputs(); getc(); getchar(); gets(); putc(); putchar(); puts(); ungetc(); perror();

Variables:

typedef size_t typedef FILE typedef fpos_t

Header files start with a Copyright declaration of the GNU license.

/* Define ISO C stdio on top of C++ iostreams.
   Copyright (C) 1991, 1994-2007, 2008, 2009 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

This example is a declaration of the if statement syntax and usage from stdio.h

#if (defined __USE_POSIX2 || defined __USE_SVID  || defined __USE_BSD || \
     defined __USE_MISC)
/* Create a new stream connected to a pipe running the given command.

   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern FILE *popen (__const char *__command, __const char *__modes) __wur;

/* Close a stream opened by popen and return the status of its child.

   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern int pclose (FILE *__stream);
#endif

cprog Keyword 3

Arithmetic (equations, operators)

Definition

There are many simple statements and functions in C that allow for easy use of arithmetic. The C environment uses the math.h header file to present the additional functions needed.

Math Functions

acos(); asin(); atan(); atan2(); ceil(); cos(); cosh(); exp(); fabs(); floor(); fmod(); frexp(); ldexp(); log(); log10(); modf(); pow(); sin(); sinh(); sqrt(); tan(); tanh();

Demonstration

This is an exercise in using basic math functions comparing values. There are many more called out in the <math.h> file. For example, the trig functions of sin(x), cos(x) and tan(x).

#include <stdio.h>
#include <math.h>
 
int main()
{
 
        int a = 2 == 2;
        int b = 2 != 3;
        int c = 4 < 5;
        int d = 4 > 9;
        int e = 3 >= 3;
        int x = 11;
        int y = 33;
//basic math statements
       printf("basic math expressions\n");
       printf("%d\n", x - y);
       printf("%d\n", x + y);
       printf("%d\n", x * y);
       printf("%d\n", y / x);
       printf("%d\n", (x*y)/2);
       printf("%d\n", ((y-x)/20)*100);
 
 
//compare represetned vaule
        printf("compare held values\n");
        printf("%d\n", a);
        printf("%d\n", b);
        printf("%d\n", c);
        printf("%d\n", d);
        printf("%d\n", e);
        return 0;
}
basic math expressions
-22
44
363
3
181
100
compare held values
1
1
1
0
1

cprog Keyword 4

Logic and Operators (and, or, not, xor)

Definition

A Logical Operators are symbols that are used to do some level of logic check within a statement. They can tie together the results of several comparison expressions into one logical expression. There are many different types of operators that meet the needs of many situations.

List of different operators

  1. Unary Operators
  2. Prefix Incrementation Operators
  3. Address Operators
  4. Shift Operators
  5. Relational Operators
  6. Equality Operators
  7. Bitwise AND Operators
  8. Bitwise Exclusive OR Operators
  9. Bitwise Inclusive OR Operators
  10. Logical AND Operators
  11. Logical OR Operators
  12. Conditional Operators
  13. Assignemnt Expresssion Operators
  14. Comma Operator
  15. Constant Expression Operators

The Logical Operators for and, or, not, and xor are very common (and a part of this assignment).

This double ampersand is used to put two comparisons together.

The double pipes are use to represent one or the other.

The exclamation point represents the not operator.

The xnor is a symbolic constant that represents not.

Demonstration

The Following code shows and example of the Operators in action.

int main()
{
 
        int a = 2;
        int b = 3;
        int c = 4;
        int d = 5;
 
//comparision operators
        printf("compare held values\n");
        if (a != 0)
          {
           printf("the value is not equal to zero %d\n", a );
          }
        if ((b > c) || (a > d))
          {
          printf("b and a are less then d. the values are %d %d\n ", b,a);
          }
        if ((a && b && c) < 2)
          {
         printf("vaule for a, b and c are larger then 2\n");
          }
        return 0;
}

The code results in the following

compare held values
the vaule is not equal to zero 2
vaule for a, b and c are larger then 2

cprog Keyword 5

Variables (types, ranges, sizes)

Definition

A variable is a type of declaration and a place holder for a value. For example you declare a variable with the declarations section of your code. Another word for a variable is an object and refers to a location and name of a point in storage. The attributes of the variable is determined but its storage class and type. The value limits are held in limits.h There are four basic types of declared variables the int, char, float, and double types.

  1. The int type can hold a whole number up to 32 bits or one machine word.
  2. The char type typically has the size of one bit
  3. The float type or floating point can store one machine word as a real number
  4. the double type is like a float type but it allows you to store real numbers larger then float.

Variable floats are used when you set a vault to a floating amount that will be used and changed by the program. This example is a simple one, using the float within the main part of the program. Other times the float is used on a global level within the program.

Demonstration

The following is an example or int and char variables.

int main()
{
 
        int bowl = 324;
        char cup = 'a';
 
       printf("a char hold one char such as the letter %d\n", cup);
       printf("int can hold full value such as %d\n", bowl);
        return 0;
}

The code output is as follows

lab46:~/junk$ ./dem4.out
a char hold one char such as the letter 97
int can hold full value such as 324

The following is an example of a variable float

#include <stdio.h>
int main()
{
        float num = 9.87;
 
        printf("the number \"num\" is %.2f\n", num);
        return 0;
}

The output for the variable float example

lab46:~/src/cprog/project$ ./example9.out
the number "num" is 9.87

cprog Keyword 6

Scope (Block, Local, Global, File)

Definition

The scope within a program is the boundaries or references that assign and determine use of variables within it's borders. There are different levels of scope within the program that determine what level of declaration the code can be utilized at.

Block scope, a higher level of function scoping, gives variables the ability to be focused only on part of a function.

The term local refers to the declaration being available within the direct code, or within “{ }”.

The Global level sets the declaration above the codes local level and is used by all areas of the code. When you int a variable you typically declare it at the global level.

A file declaration is something that is used outside a block that can be used very much like the global level declaration.

Demonstration

This is an example that was given for char types.

#include <stdio.h> //FILE
#include <math.h>
 
int main() //GLOBAL 
{
    // LOCAL 
 
    unsigned long long int quantity = 0;
    unsigned char uc = 0;
    signed char sc = 0;
 
    printf("An unsigned char is %d bytes\n", sizeof(uc));
 
    printf("The range of an unsigned char is %hhu to %hhu\n", uc, (uc-1));
    quantity = (unsigned char)(uc-1) + 1;
 
    printf("An unsigned char can store %llu unique values\n\n", quantity);
 
    printf("A signed char is %d bytes\n", sizeof(sc));
    quantity = (unsigned long long int)pow(2, (sizeof(sc)*8));
 
    printf("The range of a signed char is %hhd to %hhd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 
    printf("A signed char can store %llu unique values\n\n", quantity);
 
    return(0);
}
lab46:~/src/cprog/classproject$ ./var.out
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
A signed char can store 256 unique values

cprog Keyword 7

Pointers (address of, assignment, dereferencing)

Definition

Pointers are values that are declared and referred to from within the program. In the following program I have used a simple int of two values. The values are then assigned a meaning and used within the program. The prinf function then calls on these pointers to output the assigned value.

Demonstration

#include<stdio.h>
int main()
{
        int x, y;
        x = 256;
        y = 23;
        {
        printf("%d \n", x);
        printf("%d \n", y);
        }
return(0);
}
lab46:~/src/cprog/classproject$ ./char.out
256
23

cprog Keyword 8

Type Casting

Definition

Type Casting is converting a variable of one type act like another type. For example making the int variable act like a char.

Demonstration

#include<stdio.h>
 
int main()
{
       int a=3,b=9;
        float c = 0, d = 0;
 
        c = b/a;
        printf("\n [%f] \n", c);
 
        d = (float)b/(float)a;
        printf("n\ [%f] \n", d);
return(0);
}
lab46:~/src/cprog/classproject$ ./typec.out
 [3.000000]
n [3.000000]

cprog Objective

cprog Objective

Right a script that will log and store all users on the server.

Definition

Logging users IP and account information

Method

Use nano and gcc to to compile the script

Measurement

The script successfully runs and appends to a file.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.