Synapseindia Dotnet Development-Programming Language
Synapseindia Dotnet Development-Programming Language
Programming Language
Introduction
ALGOL 60 (1960),
CPL (Cambridge, 1963),
BCPL (Martin Richard, 1967),
B (Ken Thompson, 1970)
Standard C
Elements of a C Program
A Simple C Program
inlined code
class definitions
const definitions
/usr/include/stdlib.h
/* prevents including file
* contents multiple
* times */
#ifndef _STDLIB_H
#define _STDLIB_H
... definitions and protoypes
#endif
./try g 2 fred
argc = 4,
argv = <address0>
try\0
[0]
[1]
[2]
[3]
[4]
argv:
<addres1>
<addres2>
<addres3>
<addres4>
NULL
-g\0
2\0
fred\0
The Preprocessor
Preprocessor: Macros
Source: k = mysq(i++)
Post preprocessing: k = (i++)*(i++)
#elif defined(sun)
static inline int64_t
gettime(void) {return
(int64_t)gethrtime()}
#else
of course you will have to learn the new interfaces and utility
functions defined by the C standard and UNIX
Pointers will give you the most trouble
3
4
Pointers
Generic pointers:
x
y
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
4
0x3dc
NA
NA
0
0
0
0
4
3
2
1
0x3dc
0x3d8
0x3d4
0x3d0
0x3cc
0x3c8
0x3c4
0x3c0
0x3bc
0x3b8
0x3b4
0x3b0
Pointers Continued
Step 1:
int main (int argc, argv) {
int x = 4;
int *y = &x;
int *z[4] = {NULL, NULL, NULL, NULL};
int a[4] = {1, 2, 3, 4};
Step 2: Assign addresses to array Z
z[0] = a;
// same as &a[0];
z[1] = a + 1;
// same as &a[1];
z[2] = a + 2; // same as &a[2];
z[3] = a + 3; // same as &a[3];
x
y
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
4
0x3dc
NA
NA
0x3bc
0x3b8
0x3b4
0x3b0
4
3
2
1
0x3dc
0x3d8
0x3d4
0x3d0
0x3cc
0x3c8
0x3c4
0x3c0
0x3bc
0x3b8
0x3b4
0x3b0
Pointers Continued
Step 1:
int main (int argc, argv) {
int x = 4;
int *y = &x;
int *z[4] = {NULL, NULL, NULL, NULL};
int a[4] = {1, 2, 3, 4};
Step 2:
z[0] = a;
z[1] = a + 1;
z[2] = a + 2;
z[3] = a + 3;
Step 3: No change in zs values
z[0] = (int *)((char *)a);
z[1] = (int *)((char *)a
+ sizeof(int));
z[2] = (int *)((char *)a
+ 2 * sizeof(int));
z[3] = (int *)((char *)a
+ 3 * sizeof(int));
x
y
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
4
0x3dc
NA
NA
0x3bc
0x3b8
0x3b4
0x3b0
4
3
2
1
0x3dc
0x3d8
0x3d4
0x3d0
0x3cc
0x3c8
0x3c4
0x3c0
0x3bc
0x3b8
0x3b4
0x3b0
\
\
\
\
\
\
\
\
\
3 words in memory
QNODE_INIT(head, alist) <integer> state
<address> next
<address> prev
QNODE Manipulations
before
head
0x100 0
0x104 0x100
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
QNODE Manipulations
before
head
0x100 0
0x104 0x100
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
head
0x100 0
0x104 0x1a0
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
QNODE Manipulations
before
head
0x100 0
0x104 0x100
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
head
0x100 0
0x104 0x1a0
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x104
QNODE Manipulations
before
head
0x100 0
0x104 0x100
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
head
0x100 0
0x104 0x1a0
0x108 0x1a4
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x104
QNODE Manipulations
before
head
0x100 0
0x104 0x100
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
head
0x100 0
0x104 0x1a0
0x108 0x1a4
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
QNODE Manipulations
before
head
0x100 0
0x104 0x100
0x108 0x104
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
head
0x100 0
0x104 0x1a0
0x108 0x1a4
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node1
0x200 0
0x204 0x200
0x208 0x204
head
0x100 0
0x104 0x1a0
0x108 0x1a4
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node1
0x200 0
0x204 0x200
0x208 0x204
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node1
0x200 0
0x204 0x200
0x208 0x204
head
0x100 0
0x104 0x1a0
0x108 0x1a4
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
(1)
node1
0x200 0
0x204 0x200
0x208 0x204
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node1
0x200 0
0x204 0x200
0x208 0x204
head
0x100 0
0x104 0x1a0
0x108 0x1a4
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
(1)
node1
0x200 0
0x204 0x200
0x208 0x1a4
(2)
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node1
0x200 0
0x204 0x200
0x208 0x204
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
(1)
node1
0x200 0
0x204 0x200
0x208 0x1a4
(3)
(2)
node0
0x1a0 0
0x1a4 0x100
0x1a8 0x104
node1
0x200 0
0x204 0x200
0x208 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
(1)
node1
0x200 0
0x204 0x100
0x208 0x1a4
(3)
(2)
Removing a Node
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 ??
0x108 ??
node0
0x1a0 0
0x1a4 ??
0x1a8 ??
node1
0x200 0
0x204 ??
0x208 ??
Removing a Node
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
Removing a Node
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 0x200
0x108 0x204
(1)
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
Removing a Node
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 0x200
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x104
(2)
Removing a Node
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 0x200
0x108 0x204
(3)
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x104
Removing a Node
head
0x100 0
0x104 0x1a0
0x108 0x204
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 0x200
0x108 0x204
(4)
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
node1
0x200 0
0x204 0x100
0x208 0x104
node0
0x1a0 0
0x1a4 0x200
0x1a8 0x104
node1
0x200 0
0x204 0x100
0x208 0x1a4
QREMOVE(node0, alist);
head
0x100 0
0x104 0x200
0x108 0x204
node0
0x1a0 0
0x1a4 0x1a0
0x1a8 0x1a4
node1
0x200 0
0x204 0x100
0x208 0x104
Functions
If performance is an issue then use inline functions, generally better and safer
than using a macro. Common convention
Enumeration:
Arithmetic: +, -, *, /, %
Relational and logical: <, >, <=, >=, ==, !=, &&, ||
th
Edition)
Associates
Tokens
Operator
Class
Pr
ec
ed
en
ce
primary
n/a
(type)
casts
unary
14
right-to-left
subscripting
postfix
left-to-right
* / %
multiplicative
binary
13
left-to-right
f(...)
function call
postfix
left-to-right
+ -
additive
binary
12
left-to-right
direct selection
postfix
left-to-right
<< >>
binary
11
left-to-right
->
indirect selection
postfix
left to right
relational
binary
10
left-to-right
++ --
increment, decrement
postfix
left-to-right
== !=
equality/ineq.
binary
left-to-right
(type){init}
compound literal
postfix
left-to-right
&
bitwise and
binary
left-to-right
increment, decrement
prefix
right-to-left
bitwise xor
binary
left-to-right
sizeof
size
unary
right-to-left
bitwise or
binary
left-to-right
bitwise not
unary
right-to-left
&&
logical and
binary
left-to-right
logical not
unary
right-to-left
||
logical or
binary
left-to-right
- +
negation, plus
unary
right-to-left
?:
conditional
ternary
right-to-left
&
address of
unary
right-to-left
indirection
(dereference)
unary
right-to-left
= += -=
*= /= %=
&= ^= |=
<<= >>=
assignment
binary
right-to-left
sequential eval.
binary
left-to-right
Tokens
Operator
Class
names,
literals
simple tokens
a[k]
++
--
16
15
Associates
structures
struct MyPoint {int x, int y};
typedef struct MyPoint MyPoint_t;
MyPoint_t point, *ptr;
point.x = 0;point.y = 10;
ptr = &point; ptr->x = 12; ptr->y = 40;
unions
union MyUnion {int x; MyPoint_t pt;
struct {int 3; char c[4]} S;};
union MyUnion x;
Can only use one of the elements. Memory will be
allocated for the largest element
if (a < 10)
printf(a is less than 10\n);
else if (a == 10)
printf(a is 10\n);
else
printf(a is greater than 10\n);
If you have compound statements then use brackets (blocks)
if (a < 4 && b > 10) {
c = a * b; b = 0;
printf(a = %d, a\s address = 0x%08x\n, a, (uint32_t)&a);
} else {
c = a + b; b = a;
}
Is this correct?
if (a) x = 3; else if (b) x = 2;
else (z) x = 0; else x = -2;
Loops
for (i = 0; i < MAXVALUE; i++) {
dowork();
}
while (c != 12) {
dowork();
}
do {
dowork();
} while (c < 12);
flow control
Makefiles
Defining variables
MyOPS := -DWTH
MyDIR ?= /home/fred
MyVar
= $(SHELL)
Using variables
MyFLAGS := $(MyOPS)
Built-in Variables
$@ = filename of target
$< = name of the first prerequisites
Patterns
WARNLIGHT
WARN
ALLFLGS
Makefile
# Project specific
include ../Makefile.inc
INCLUDES = ${WUINCLUDES} I.
LIBS
= ${WILIBS} ${OSLIBS}
CFLAGS
= ${WUCLFAGS} DWUDEBUG
CC
= ${WUCC}
HDRS
CSRCS
SRCS
COBJS
OBJS
CMDS
APPCFLGS
WUCC
WUCFLAGS
WUINCLUDES
WULIBS
=
$(WARN)
$(ALLFLGS) \
all :
$(OBJDIR) $(CMDS)
:= gcc
:= -DMyOS=$(MyOS) \
$(OSFLAGS) \
$(ALLFLGS) $(WARN)
install : all
:=
:= -lm
$(OBJS)
:= util.h
:= testapp1.c testapp2.c
:= util.c callout.c
= $(addprefix ${OBJDIR}/, \
$(patsubst %.c,%.o,$(CSRCS)))
= $(addprefix ${OBJDIR}/, \
$(patsubst %.c,%.o,$(SRCS)))
= $(addprefix ${OBJDIR}/, $(basename $(CSRCS)))
$(OBJDIR) :
mkdir $(OBJDIR)
$(COBJS) : ${OBJDIR}/%.o : %.c $(HDRS)
${CC} ${CFLAGS} ${INCLUDES} o $@ -c $<
Project Documentation
Section A: Introduction
describe the project, paraphrase the requirements and state your understanding of
the assignments value.
Section B: Design and Implementation
List all files turned in with a brief description for each. Explain your design and
provide simple psuedo-code for your project. Provide a simple flow chart of you
code and note any constraints, invariants, assumptions or sources for reused code or
ideas.
Section C: Results
For each project you will be given a list of questions to answer, this is where you do
it. If you are not satisfied with your results explain why here.
Section D: Conclusions
What did you learn, or not learn during this assignment. What would you do
differently or what did you do well.
Attacking a Project
Requirements and scope: Identify specific requirements and or goals. Also note
any design and/or implementation environment requirements.
Approach: How do you plan to solve the problem identified in the first step.
Develop a prototype design and document. Next figure out how you will verify
that you did satisfy the requirements/goals. Designing the tests will help you to
better understand the problem domain and your proposed solution
Iterative development: It is good practice to build your project in small pieces.
Testing and learning as you go.
Final Touches: Put it all together and run the tests identified in the approach
phase. Verify you met requirements. Polish you code and documentation.
Turn it in: