4. 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
5.
6. 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)
7. More on ANSI 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 to incorporate assembly to directly access the memory
Numerous library functions available that may be imported
9. Consists of one or more functions (not methods as they are called in Java)
Program consists of executing the main function
int main (void) { … }
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)
10. 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)
Expressions must end with a semlcolon (same as Java)
Compound instructions must be enclosed with curly braces { … }
Comments delimited by // or by multi line comments /* */
17. 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 name hello
“hello” is now created, now how do we run it?
./hello
Executes hello
./ is necessary (.’ specifies the directory you’re in currently)
18. 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
Many more which I’ve not included here…
20. Off campus?
Download the Cisco VPN https://its.gmu.edu/service/virtual-private-network-
vpn/
Connecting by SSH to Zeus via command line
ssh username@zeus.vse.gmu.edu
Download the SSH client
https://labs.vse.gmu.edu/index.php/FAQ/SSH
21. Copying files from/to Zeus
From your computer to Zeus
scp local user_zeus.vse.gmu.edu:~/.
From Zeus to your computer
scp user@zeus.vse.gmu.edu:~/remote local
Windows users need to use Putty
https://its.gmu.edu/knowledge-base/how-to-install-putty-ssh-for-
windows/
22. Shell
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:
clear pwd ls cd cp mv rm mkdir rmdir cat
man
24. 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
25. 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)
27. 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
29. Java C
Strings String s = “Hello”; char * s = “Hello”;
char s2[6];
strcpy(s2, “Hello”)
String Concatenation s1 + s2 #include <string.h>
strcat(s1,s2)
Arrays Int [] x = new int[10]; Int x[10];
Array bounds checks Occurs at run time No run time checks
Pointers Objects are implicit pointers Int *p
31. Variables
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
32. 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;
Macro defined constants:
#define id value
33. Data Types
char – Character (1 byte)
Int – Integer (4 bytes)
float – Floating point numbers (4 bytes)
double – Floating point with double precision (8 bytes)
These data type may be coupled with the words – unsigned, signed, short, and long
before the data type
The bit size assigned to the data type usually depends on the capacity of the computer
and the compiler used. However, a char is always 1 byte.
36. printf(format string, val1, val2);
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
46. Tasks this week
Familiarize yourself with a text editor
Learn to work with Unix commands
Lab 1
Working with Zeus
“All theory and no practice makes Jack a dull boy”