CS class accounts - faculty and C Basics: Difference between pages

From Computer Science at Indiana State University
(Difference between pages)
Jump to navigation Jump to search
m 1 revision imported
 
wiki_previous>Jkinne
 
Line 1: Line 1:
This page is for faculty teaching CS courses that will use class accounts on the CS server. If you are a student, see [[CS Accounts and CS Lab Computers]].
on the basics of C for you to study. We do not aim to present this information completely, but only to remind of things you should have learned reading.


=Getting Started=
=C Data Types=
For each CS course that uses the CS server, there will be a separate account for each student. The usernames start with the course number. For example, the accounts for CS 151 are cs15100, cs15101, cs15102, etc. The instructor account to manage assignments is just the course number (so for CS 151, the instructor account to manage assignments is cs151). You should normally be given more student accounts (cs15100, cs15101, etc.) than there are students in the class.  
Note - # of bytes is for on the CS server using gcc (as of 2022).  This varies depending on your system and compiler.
* '''integer data types''': <code>char</code> (1 byte), <code>short</code> (2 bytes), <code>int</code> (4 bytes), <code>long</code> (8 bytes)
** '''signed/unsigned''': integer types are <code>signed</code> by default, use <code>unsigned</code> before any of the above for unsigned.  
** '''max/min values''': for unsigned types, the value ranges from 0 to 2<sup># of bits</sup>-1. For signed types, the value ranges from (-2<sup># of bits -1</sup>) to (2<sup># of bits - 1</sup>). E.g., <code>char</code> ranges from -128 to 127, while <code>unsigned char</code> ranges from 0 to 255.
* '''floating point data types:''' <code>float</code> (4 bytes), <code>double</code> (8 bytes), <code>long double</code> (16 bytes)
** '''precision and max/min values''': each floating point type uses 1 bit for the sign, most of the bits for the mantissa (aka significand), and the remainder for the exponent.  <code>float</code> uses 23 bits for the significand, resulting in about 7 digits of precision, and 8 bits for the exponent, meaning the exponent can range from -128 to 127 and the maximum value is around 10<sup>38</sup>.  <code>double</code> uses 52 bits for the mantissa and 11 bits for the exponent. 
* '''pointers''' - memory address, note that <code>sizeof(char *)</code> is however many bytes a memory address takes up, which in 2022 on the CS server is 8.
* any type of data in C can be interpreted as an integer and printed that way
* '''C string''' - array of characters, "last" character in the string is <code>'\0'</code> (literally all 0 bits in the byte), also can use <code>NULL</code> (a synonym for <code>'\0'</code>)
* '''array''' - block of memory large enough for multiple int's (or float's, etc.).  Declared like <code>int A[10];</code>  In the program, <code>A</code> evaluates to the address of the first element in the array.


It is normal to reserve one of the student accounts for instructor use, so that you can test things out the way things are for students. For example, for CS 151, the account cs15100 might be reserved for use by the instructor.
=Operators=
* '''Bit operations -''' <code>&</code> and, <code>^</code> xor, <code>|</code> or, <code>~</code> complement, <code><<</code> left bit shift, <code>>></code> right bit shift
* '''Logical (Boolean) operations -''' <code>&&</code> and, <code>||</code> or, <code>!</code> not
* '''Arithmetic -''' <code>+</code> <code>-</code> <code>*</code> <code>/</code> <code>%</code>
* '''Assignment -''' <code>=</code>, also = after all bit/arithmetic operations (<code>+=</code>, <code>-=</code>, <code>&=</code>, etc.)
* '''Memory access -''' <code>&</code> gives the address of a variable, <code>*</code> dereferences (aka gets the value of) a pointer variable
* '''Precedence and associativity -''' see https://en.cppreference.com/w/c/language/operator_precedence


=Account Logins=
=Grouping=
The scripts that the CS sys admin runs to create the accounts will leave the login names and passwords in the instructor's permanent CS account. For example, Jeff Kinne's permanent CS account is jkinne; if Jeff Kinne is teaching CS 151, then the login information for the cs151 class accounts will be left in ~jkinne/accts/. The file ~jkinne/accts/cs151.out contains login name and password for all of the cs151 class accounts (cs151 is the instructor account, and the student accounts are cs15100, cs15101, etc.).
* <code>{</code> <code>}</code> for compound statements
* <code>(</code> <code>)</code> for order of operations / grouping expressions
* <code>(</code> <code>,</code> <code>)</code> for defining functions, calling functions
* <code>[</code> <code>]</code> for indexing arrays


=Assigning Accounts to Students=
=Statements=
The instructor can assign accounts to students however they like. The usual way is to assign them in alphabetical order (so cs15101 would be assigned to the first student in the gradebook, cs15102 for the next, and so on). Note that students can change their course schedule up to 7 days after the start of term, so if you assign accounts before that point, you may need to assign additional ones as students add the course to their schedule.
* <code>for</code> - <code>for</code> <code>(</code> ''initializer'' <code>;</code> ''loop condition'' <code>;</code> ''increment'' <code>)</code> ''loop body statement''
* <code>while</code> - <code>while</code> <code>(</code> ''loop condition'' <code>)</code> ''loop body statement''
* <code>do</code> <code>while</code> - runs loop body at least once, before checking the loop condition
* <code>continue</code> - for the innermost currently executing loop, go back to the beginning of that loop
* <code>break</code> - for the innermost currently executing loop, break out of the loop and start with the code right after the loop
* <code>break</code> - used in a switch statement to break out of the switch statement
* <code>switch</code> - only for integer types (including char's), note that must use <code>break</code> with each <code>case</code> (or multiple case's will run the same code).  If none of the cases's given, and if a <code>default</code> is given, then the default is run.
* <code>if</code> / <code>else</code>


Once you have decided how to assign the accounts, you need to share the username and initial password with each student. Options for how to do this... One option - create a 0 point assignment in the course in Canvas, and leave the username and password as a comment to the grade. Another option - store the usernames and password in a spreadsheet that also has the student name and email addresses, and make a script to email the initial username and password to each student.
=Variables, Data, Memory=
* '''Initialization''' - is not guaranteed, you must initialize all variables.
* '''Scope''' - variables exist within the <code>{</code> <code>}</code> they are declared within, globals are declared outside of functions and have scope for the whole file
* <code>malloc</code>/<code>free</code> - for allocating and freeing dynamically allocated memory (from the heap)
* '''pointers''' / memory addresses - <code>&</code> gets the address (aka memory location) of a variable, <code>*</code> gets the value at a given memory address, <code>int *</code> declares a pointer variable that stores a memory address
* '''array''' - if declared as <code>int A[10];</code> then <code>A[i]</code> is the (i+1)st slot in the array, and <code>A</code> (on its own, no brackets) is the address of the first slot in the array
* '''Basic 4 types of memory''' - local variables (kept on ''"the stack"''), global variables (kept in ''"data"''), the program code (kept in ''"code"''), dynamic memory (kept in ''"the heap"'')
* '''The stack''' - local variables automatically allocated space, stack pointer keeps track of the top of the stack, at end of <code>}</code> local variables are "free'd" by changing the stack pointer (so next allocated variables will overwrite previous ones).  Stack memory is limited (e.g., 1MB), so large arrays and such should be allocated in the heap or as global variables.  Stack memory is reused, which is one of the reasons that variables should always be initialized.
* '''The heap''' - big chunk of memory, <code>malloc</code> allocates space, <code>free</code> frees it, it's up to program to keep track of malloc'ed memory.  Can be as large as memory.
* '''Code''' - memory that has the program code itself is read/execute-only.
* '''Data''' - global variables are known at compile time, space is allocated when the program is loaded and persists until program is finished, can declare large arrays as globals.  Can be larger than the stack, still best practice to store really large things in the heap.


=Student First Time Logging in=
=Functions=
Students will connect to the server the first time using ssh, putty, or some other ssh client. Students can see here for options - [[Linux - System Setup]].
* Parameters are ''passed by value'' (whatever changes are made in the function do not get "sent back" to where the function was called from), use pointers to have modified variables "returned"
* <code>return</code> - returns value from function, stops function


The first time each student logs into the system, it will ask for the password an additional time and then ask for their name, office, etc. You should have them type in their real name (just like it shows up in Canvas), and leave the other fields blank.
=Pre-processor=
* Is executed ''before'' compiling - first pre-processor is run through the entire file(s), second the compiler runs
* <code>#include</code> - it is as if that file were copy/pasted into the present file
* <code>#define PI 3.14</code> - when pre-processor is run, all occurrences of <code>PI</code> in the program will be replaced with <code>3.14</code>, before the compiler is run.


You should have the students change their password to something they will remember. They use the passwd command to do this.
=Command-Line Arguments=
* <code>argc</code> is count, <code>argv</code> is array of C strings, <code>argv[0]</code> is the name of the program itself, <code>argv[1]</code> is first actual argument, arguments are all C strings so use <code>atoi</code> or <code>atof</code> if need to convert to int or float.


=Handin=
=Other Punctuation=
The handin system is a series of scripts that can be used for students to handin assignments on the server. The assignments get copied into the instructor account (i.e., for CS 151 they get copied into the cs151 account). To understand how this works, read [[Handin]], and then read the man pages on the server: handin, handin.config, ccu, igf.
* <code>//</code> comment to the end of the line
* <code>/*</code> multiple lines <code>*/</code>


=Student Forgets Password=
=Other Keywords, Operators=
If a student changes their password and forgets it, the CS sys admin can reset their password to what it was originally (which the instructor will also have from when the accounts were created).
See https://en.cppreference.com/w/c/keyword for a list of all keywords in C. Some on the list are not commonly used. If that link's explanation is not making sense, you can try one of these: https://www.programiz.com/c-programming/list-all-keywords-c-language, or http://aboutc.weebly.com/keywords.html


=TA or Second Faculty Member=
For a list of more "language reference" information, see https://en.cppreference.com/w/c/language
If you will have a TA or second faculty member that needs access to the instructor account for the course, you can edit the bin/.config file in the instructor account (so for CS 151, this would be ~cs151/bin/.config). For example, if the user SOME_USER is the TA for CS 151, then the ~cs151/bin/.config file would be edited to contain "SOME_USER" in the list that is in that file. Any users that are listed in ~cs151/bin/.config are able to run the command ~cs151/bin/ccu, which makes the user become the cs151 instructor user (so we don't need to do password sharing for the instructor accounts).


=Read Access to Student Accounts=
=Standard Library=
The instructor account for the course can be giving rx access to the student accounts. This might be done by default, but if it is not, you can ask the CS sys admin to do this. So, if Jeff Kinne is teaching CS 151, the permissions can be set so that the user cs151 has rx access to cs15100, cs15101, etc.
* string functions - strlen, strcmp, strstr, strtok, atoi, atof, strcpy
* I/O - printf, scanf, fgetc, getline, fgets, fprintf, fscanf, fputc
* math functions - (use -lm flag when compiling with gcc), log, log2, log10, sqrt, pow
 
=Assignments=
It is traditional to have a series of quizzes over the operators, evaluating expressions, keywords, etc.  A few are listed here that you can practice with.
 
* Practice quizzes from here - https://indstate.instructure.com/courses/12565/quizzes
** C Operators and Expressions Quiz
** C Keywords and Data Types Quiz
** C Fixing Errors Quiz
** C Errors Quiz
 
'''Pass rating check''' If you are assigned these quizzes for your course, then ''pass'' is at least 95%.

Revision as of 14:19, 13 January 2023

on the basics of C for you to study. We do not aim to present this information completely, but only to remind of things you should have learned reading.

C Data Types

Note - # of bytes is for on the CS server using gcc (as of 2022). This varies depending on your system and compiler.

  • integer data types: char (1 byte), short (2 bytes), int (4 bytes), long (8 bytes)
    • signed/unsigned: integer types are signed by default, use unsigned before any of the above for unsigned.
    • max/min values: for unsigned types, the value ranges from 0 to 2# of bits-1. For signed types, the value ranges from (-2# of bits -1) to (2# of bits - 1). E.g., char ranges from -128 to 127, while unsigned char ranges from 0 to 255.
  • floating point data types: float (4 bytes), double (8 bytes), long double (16 bytes)
    • precision and max/min values: each floating point type uses 1 bit for the sign, most of the bits for the mantissa (aka significand), and the remainder for the exponent. float uses 23 bits for the significand, resulting in about 7 digits of precision, and 8 bits for the exponent, meaning the exponent can range from -128 to 127 and the maximum value is around 1038. double uses 52 bits for the mantissa and 11 bits for the exponent.
  • pointers - memory address, note that sizeof(char *) is however many bytes a memory address takes up, which in 2022 on the CS server is 8.
  • any type of data in C can be interpreted as an integer and printed that way
  • C string - array of characters, "last" character in the string is '\0' (literally all 0 bits in the byte), also can use NULL (a synonym for '\0')
  • array - block of memory large enough for multiple int's (or float's, etc.). Declared like int A[10]; In the program, A evaluates to the address of the first element in the array.

Operators

  • Bit operations - & and, ^ xor, | or, ~ complement, << left bit shift, >> right bit shift
  • Logical (Boolean) operations - && and, || or, ! not
  • Arithmetic - + - * / %
  • Assignment - =, also = after all bit/arithmetic operations (+=, -=, &=, etc.)
  • Memory access - & gives the address of a variable, * dereferences (aka gets the value of) a pointer variable
  • Precedence and associativity - see https://en.cppreference.com/w/c/language/operator_precedence

Grouping

  • { } for compound statements
  • ( ) for order of operations / grouping expressions
  • ( , ) for defining functions, calling functions
  • [ ] for indexing arrays

Statements

  • for - for ( initializer ; loop condition ; increment ) loop body statement
  • while - while ( loop condition ) loop body statement
  • do while - runs loop body at least once, before checking the loop condition
  • continue - for the innermost currently executing loop, go back to the beginning of that loop
  • break - for the innermost currently executing loop, break out of the loop and start with the code right after the loop
  • break - used in a switch statement to break out of the switch statement
  • switch - only for integer types (including char's), note that must use break with each case (or multiple case's will run the same code). If none of the cases's given, and if a default is given, then the default is run.
  • if / else

Variables, Data, Memory

  • Initialization - is not guaranteed, you must initialize all variables.
  • Scope - variables exist within the { } they are declared within, globals are declared outside of functions and have scope for the whole file
  • malloc/free - for allocating and freeing dynamically allocated memory (from the heap)
  • pointers / memory addresses - & gets the address (aka memory location) of a variable, * gets the value at a given memory address, int * declares a pointer variable that stores a memory address
  • array - if declared as int A[10]; then A[i] is the (i+1)st slot in the array, and A (on its own, no brackets) is the address of the first slot in the array
  • Basic 4 types of memory - local variables (kept on "the stack"), global variables (kept in "data"), the program code (kept in "code"), dynamic memory (kept in "the heap")
  • The stack - local variables automatically allocated space, stack pointer keeps track of the top of the stack, at end of } local variables are "free'd" by changing the stack pointer (so next allocated variables will overwrite previous ones). Stack memory is limited (e.g., 1MB), so large arrays and such should be allocated in the heap or as global variables. Stack memory is reused, which is one of the reasons that variables should always be initialized.
  • The heap - big chunk of memory, malloc allocates space, free frees it, it's up to program to keep track of malloc'ed memory. Can be as large as memory.
  • Code - memory that has the program code itself is read/execute-only.
  • Data - global variables are known at compile time, space is allocated when the program is loaded and persists until program is finished, can declare large arrays as globals. Can be larger than the stack, still best practice to store really large things in the heap.

Functions

  • Parameters are passed by value (whatever changes are made in the function do not get "sent back" to where the function was called from), use pointers to have modified variables "returned"
  • return - returns value from function, stops function

Pre-processor

  • Is executed before compiling - first pre-processor is run through the entire file(s), second the compiler runs
  • #include - it is as if that file were copy/pasted into the present file
  • #define PI 3.14 - when pre-processor is run, all occurrences of PI in the program will be replaced with 3.14, before the compiler is run.

Command-Line Arguments

  • argc is count, argv is array of C strings, argv[0] is the name of the program itself, argv[1] is first actual argument, arguments are all C strings so use atoi or atof if need to convert to int or float.

Other Punctuation

  • // comment to the end of the line
  • /* multiple lines */

Other Keywords, Operators

See https://en.cppreference.com/w/c/keyword for a list of all keywords in C. Some on the list are not commonly used. If that link's explanation is not making sense, you can try one of these: https://www.programiz.com/c-programming/list-all-keywords-c-language, or http://aboutc.weebly.com/keywords.html

For a list of more "language reference" information, see https://en.cppreference.com/w/c/language

Standard Library

  • string functions - strlen, strcmp, strstr, strtok, atoi, atof, strcpy
  • I/O - printf, scanf, fgetc, getline, fgets, fprintf, fscanf, fputc
  • math functions - (use -lm flag when compiling with gcc), log, log2, log10, sqrt, pow

Assignments

It is traditional to have a series of quizzes over the operators, evaluating expressions, keywords, etc. A few are listed here that you can practice with.

Pass rating check If you are assigned these quizzes for your course, then pass is at least 95%.