CS 262 Intro Slides PDF
Document Details
Uploaded by Deleted User
George Mason University
Hamza Mughal
Tags
Summary
This document provides introductory slides for a computer science course (CS 262) at George Mason University, covering the basics of C programming, including concepts, structure and fundamentals. It's intended to be a foundational resource for students learning C.
Full Transcript
CS 262 George Mason University Department of Computer Science Syllabus www.hamzamughal.com Also available on the course blackboard page Why learn C? C is dominating Microcontroller and Embedded systems programming A brief introduction to C Lets take a trip down memory lane! Where? in th...
CS 262 George Mason University Department of Computer Science Syllabus www.hamzamughal.com Also available on the course blackboard page Why learn C? C is dominating Microcontroller and Embedded systems programming A brief introduction to C Lets take a trip down memory lane! Where? in the Computer Science Research Department of Bell Labs in Murray Hill, NJ Who? by Dennis Ritchie Why? Previous languages (such as B) were difficult to implement programs in due to various issues such as hardware constraints and B being incredibly slow due to it being an interpreted language (executed by software running on top of the CPU) C was developed to move code from assembly to a “higher” language, being tied to the development of Unix to provide more control https://www.bell-labs.com/usr/dmr/www/chist.pdf PDP 11 ANSI (American National Standards Institute) standardized C in 1989 to create ANSI C The International standard (ISO) was adopted by ANSI in 1990 which is known as C89 Updates in 1995 (C05) and 1999 (C99), latest being in 2018 (C18) More on C C is a “medium level” programming language that lets us use it at a “low level” Contains the usual structures that many “high level” languages offer to us but also allowing us directly access memory and hardware Numerous library functions available that may be imported C is a Compiled, Imperative, and Statically Typed Language Pitfalls Easy to write spaghetti code since C is procedural Preprocessors can get messy quick Unable to gracefully terminate (meaning no catch/throw/exceptions!) Not much of OOP Quite an unsafe language Many more which I’ve not included here… White House Memorandum circa February 2024 https://www.whitehouse.gov/wp-content/uploads/2024/02/Final-ONC D-Technical-Report.pdf “BACK TO THE BUILDING BLOCKS: A PATH TOWARD SECURE AND MEASURABLE SOFTWARE” “Programmers writing lines of code do not do so without consequence…This report explores how such metrics can shift market forces to improve cybersecurity quality across the ecosystem.” “Experts have identified a few programming languages that both lack traits associated with memory safety and also have high proliferation across critical systems, such as C and C++.” “According to experts, both memory safe and memory unsafe programming languages meet these requirements. At this time, the most widely used languages that meet all three properties are C and C++, which are not memory safe programming languages.” “Through the adoption of memory safe programming languages, creators of software and hardware can better secure the building blocks of cyberspace and proactively eliminate entire classes of bugs Crowdstrike Crowdstrike released an “analysis” on their crash that occurred on July 19th: https://www.crowdstrike.com/wp-content/uploads/2024/08/Channel- File-291-Incident-Root-Cause-Analysis-08.06.2024.pdf “The Rapid Response Content for Channel File 291 instructed the Content Interpreter to read the 21st entry of the input pointer array. However, the IPC Template Type only generates 20 inputs. As a result, once Rapid Response Content was delivered that used a non-wildcard matching criterion for the 21st input, the Content Interpreter performed an out-of-bounds read of the input array” Structure of a C Program Consists of one or more functions (not methods as they are called in Java) Program consists of executing the main function (entry point of your program) int main () { … } int main (int argc, char *argv[]) { … } Functions contain: A heading (data type return, name, optional arguments) Argument declarations (if any optional arguments) Statements (code inside the curly braces) Preprocessors (macros, compiler controls, constant values, libraries, etc) Additional local and external functions and variables Recall control returns to the place where a function was called (may or may not return with a value) Statements must end with a semicolon (same as Java) Compound instructions must be enclosed with curly braces { … } Comments delimited by // or by multi line comments Layout of a C File Hello World in C…. Brief intro to language processing systems Source 4 Steps of compilation Code Pre processer.i file Compiler.s file Assembler.o file Linker.exe file Source 4 Steps of compilation Code Pre processer.i file 1. Removes all comments Compiler 2. Expands out macros in.s file 1. Links code with the together all corresponding 1. Transforms the byte code macro value output of the (.o or object 3. Pulls in any Assembler files) to pre included processing product an.o file header files steps into executable into the assembly that can be source file code 1. Transforms ran assembly Linker code into binary byte code.exe file GCC GNU Compiler Collection (aka gcc) optimizing compiler which we will use to compile our c code How do we utilize gcc exactly? gcc –o hello hello.c This command basically says compile “hello.c” to machine code executable named hello The executable “hello” is now created, now how do we run it?./hello Executes hello./ is necessary (./ specifies the directory you’re in currently) Connecting to Zeus Off campus? First connect to the VPN Download the Cisco VPN https://its.gmu.edu/service/virtual-private-network-vpn/ Connecting by SSH to Zeus via command line ssh [email protected] Once connected to Zeus, the Bash Shell waits for input A command line interpreter which runs commands, programs, shell scripts bash being the standard shell and the $ character being its default prompt Some commands which you may utilize the most within the bash shell: clear pwd ls cd cp mv rm mkdir rmdir cat man Copying files from/to Zeus From your computer to Zeus (in a terminal not connected to Zeus) scp local_path/file_name [email protected]:~/ remote_path From Zeus to your computer (in a terminal not connected to Zeus) scp [email protected]:~/remote_path/file_name local_path Vi/Vim We will utilize vi/vim on Zeus Text editor to create/edit files Two modes: Command mode and Insert Mode Command mode to insert mode – type i Insert mode to command mode – hit the escape key Some commands which you may find useful: vim filename (create a file named filename or edit existing file if it exists) :wq (write file and exit) :q (Quits, warning is printed if current file is not saved prior) :q! (Quits with no warning) :w (Save file) Java and C Comparisons Java runs on the JVM, provides a safe programming environment by allowing stuff such as array bounds checks C runs on the machine directly which does not contain such sanity checks that Java has Java C Portable Efficient Safe Java C Portable True False Efficient False True Safe True False Java C Strings Java C Strings String s = “Hello”; char s2 = “Hello”; Java C Strings String s = “Hello”; char s2 = “Hello”; String Concatenation Java C Strings String s = “Hello”; char s2 = “Hello”; String Concatenation s1 + s2 #include strcat(s1,s2) Java C Strings String s = “Hello”; char s2 = “Hello”; String Concatenation s1 + s2 #include strcat(s1,s2) Arrays Java C Strings String s = “Hello”; char s2 = “Hello”; String Concatenation s1 + s2 #include strcat(s1,s2) Arrays Int [] x = new int; Int x; Java C Strings String s = “Hello”; char * s = “Hello”; char s2; strcpy(s2, “Hello”) String Concatenation s1 + s2 #include strcat(s1,s2) Arrays Int [] x = new int; Int x; Pointers Java C Strings String s = “Hello”; char * s = “Hello”; char s2; strcpy(s2, “Hello”) String Concatenation s1 + s2 #include strcat(s1,s2) Arrays Int [] x = new int; Int x; Pointers Objects are implicit pointers Int *p Operators (nearly identical!) arithmetic: +, -, *, /, % assignment: = augmented assignment: +=, -=, *=, /=, %=, &=, |=, ^=, = bitwise logic: ~, &, |, ^ bitwise shifts: boolean logic: !, &&, || equality testing: ==, != subexpression grouping: () order relations: = increment and decrement: ++ and -- member selection:., -> Slightly different than Java because there are both structures and pointers to structures, more later conditional evaluation: ? : Fundamentals Declarations All variables need to be declared with a type before use If a local variable has no initialization, it will contain garbage! Basic syntax to create a variable: data type var_name; C17/18 ISO Standards: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf Variable naming Consist of letters and/or digits. They should not be key words, they can’t contain special symbols, such as blanks, -, ", etc. A few C Keywords typedef, return, for, char, if, goto, … The first character must be a letter or the symbol (_) An uppercase letter is not equivalent to a lowercase Data Types char – Character (1 byte) Int – Integer float – Floating point numbers double – Floating point with double precision These data type may be coupled with the words – unsigned, signed, short, and long before the data type The byte size assigned to the data type usually depends on the capacity of the computer and the compiler used (i.e. system dependent). However, a char is always 1 byte regardless of the system. Constants Like variables, except their values never change Also known as literals, need to specify the data type of the constant Common practice to define constants in UPPER-CASE const data_type constant_name; Macros Line of code defined near the top of a file using the #define keyword with the syntax being as follows (note there is no semicolon at the end of the value): #define name value #define PI 3.14 Once we define a macro, we can use it in our code where all macro names are replaced with the corresponding macro value What would happen to PI if we were to compile our code? Printing formatted input printf(format string, …); // need to include stdio.h to use printf If you have worked with System.out.printf in Java, then C’s printf is similar! The format string uses Conversion Characters for values (see next slide) printf(“The speed is %d miles per hour.\n”, speed); Placeholders can also specify widths and precisions %10d : add spaces to take up at least 10 characters %010d : add zeros to take up at least 10 characters %.2f : print only 2 digits after decimal point %5.2f : print 1 decimal digit, add spaces to take up 5 chars Format Specifiers To indicate a data type, the sign % is placed before the conversion character The Ascii Table Character variables in C contain ASCII values decimal values between 0-127 Rather than using the ASCII value, simply use its character value instead i.e. char c = ‘A’; Escape sequences Exercises Compute the circumference and area of a circle with a given radius. Choose any value for the radius. Tasks this week Familiarize yourself with a text editor (either Vim or Visual Studio, etc) Learn to work with Unix commands Lab 1 Working with Zeus “All theory and no practice makes Jack a dull boy”