c language summarycmichael/c_lecture/part1.pdfhere, just keep the basics in mind. csc 4103 operating...
Post on 18-Jun-2018
214 Views
Preview:
TRANSCRIPT
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
C Language Summary
Chris J Michael
cmicha1@lsu.edu
28 August 2008
Heavily Influenced by the GNU C Reference Manual: http://www.gnu.org/software/gnu-c-manual/
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Introduction
2
-C98, or the original ANSI C standard
-Lower level than C++ & Java
-gcc: The GNU C compiler
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
3
$ gcc hello.c -o hello.out
$ ./hello.out
Hello World
$ _
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
4
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Whitespace is ignored
#include <stdio.h>
int main(void){printf("Hello World\n");return 0;}
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
5
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Include the library that
provides printing functions
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
6
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Every C program must have
a main function
int main(void)
The main function returns an integer value.
It must be named “main”.
In this case, it does not receive any parameters.
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
7
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
The scope encloses the
function body
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
8
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Call the print function with
the desired argument
printf("Hello World\n");
Call the function “printf”
Send it the string to print
End statements with a semicolon
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
9
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Call the print function with
the desired argument
printf("Hello World\n");
Strings literalsare enclosed
in double-quotes.
“\n” represents a new line
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Hello World
10
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Since main is defined to
return an integer – usually a
zero is returned
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Identifiers
11
-Used to name variables, functions, and user-
defined data types
-A group of letters, decimal digits, “_”
-Cannot start with a number
-Examples:foo my_var a1 Something_Clever_1 x
C is case-sensitive!
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Constants
12
-Integer Constants:
-Character Constants:
-Real Number Constants:
-String Constants:
138 23 4002145 032 0x1f1f
Octal
Hexidecimal
'c' 'X' '\n' '\'' '\\' '\"'
Enclosed in
single-quotes
„ \ “
3.14 5. .1 0.2 500e9 3e-5
Exponent
"It’s a trap."
"It’s a" " trap."
"It’s\
a trap."
"It’s\na\ntrap."
"\"It’s a trap\""
Equivalent
It‟s
a
trap.
“It‟s a trap.”
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Constants
13
-Integer Constants:
-Character Constants:
-Real Number Constants:
-String Constants:
138 23 4002145 032 0x1f1f
Octal
Hexidecimal
'c' 'X' '\n' '\'' '\\' '\"'
Enclosed in
single-quotes
„ \ “
3.14 5. .1 0.2 500e9 3e-5
Exponent
"It’s a trap."
"It’s a" " trap."
"It’s\
a trap."
"It’s\na\ntrap."
"\"It’s a trap\""
Equivalent
It‟s
a
trap.
“It‟s a trap.”
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Primitive Data Types
14
-Integer types
-Real number types
unsigned char /* Holds a value from 0 to 256 */
signed char /* Holds a value from -127 to 128 */
char /* May either be unsigned char or signed car depending on
the system. This type should be used for character
constants. */
int /* Holds a value from -2,147,483,648 to 2,147,483,647 */
unsigned int /* Holds a value from 00000000000000 to 4,294,967,647 */
Sizes may change from
system to system!
float /* Single precision floating point */
double /* Double */Enclose comments in these
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Enumeration Definitions
15
-Used to name integer valuesenum operating_system
{
BSD, /* The value of BSD will be 0 */
LINUX, /* LINUX will be 1, etc. */
MS_WIN,
DOS,
TOS
};
enum
{
SLACKWARE=23, /* Here we set the first item to 23... */
FEDORA, /* so FEDORA will be 24, etc. */
GENTOO,
DEBIAN
};
Naming is optional
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Structure Definitions
16
-A programmer defined type made of
variables and other data types.struct particle
{
int x, y, z;
float velocity_x;
float velocity_y;
float velocity_z;
};
Members are declared like
variables are normally declared
You should provide a name
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Structure Declarations
17
-At definition
-After definition
struct particle
{
int x, y, z;
float velocity_x;
float velocity_y;
float velocity_z;
} particle_a, particle_b;
struct particle
{
int x, y, z;
float velocity_x;
float velocity_y;
float velocity_z;
};
...
struct particle particle_a, particle_b;
This way is usually preferred
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Structure Member Access
18
-Use the member access operator, the '.'struct particle
{
int x, y, z;
float velocity_x;
float velocity_y;
float velocity_z;
};
...
struct particle particle_a, particle_b;
particle_a.x = 4; particle_a.y = -4; particle_a.z = 1;
particle_a.velocity_x = 1.0;
particle_a.velocity_y = 12e3;
particle_a.velocity_z = 0;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Structure Member Access
19
-Of course, structures may contain other structuresstruct point
{
int x, y, z;
};
struct velocity
{
float x, y, z;
};
struct particle
{
struct point p;
struct velocity v;
};
...
struct particle particle_a;
particle_a.p.x = 4; particle_a.v.x = 1.0;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Array Declarations and Access
20
-Specify type and amount when declaring
-Specify array element when accessing
int my_int_array[10];
struct particle particles[10][10];
my_int_array[0] = 9; /* The first element */
my_int_array[9] = 46; /* The last element */
particles[3][7].p.x = 4;
This structure was defined in the last slide
Careful not to overstep array boundaries! The
compiler may not warn you about this!
my_int_array[10] = 6;
my_int_array[-1] = 0;
Multidimensional, 10x10 array
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Strings
21
-Strings are just arrays of characters
char my_string[20] = "Hello World";
my_string = "Hello World";
H e l l o W o r l d \0
my_string[5] = '\0';
H e l l o \0 W o r l d \0
The null character
Hello World
Hello
You cannot assign string literals after initialization!
Initialization
But don't worry! There are some library
functions that help us copy string literals
into arrays. More on that later.
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Strings
22
-Strings are just arrays of characters
char my_string[20] = "Hello World";
my_string = "Hello World";
H e l l o W o r l d \0
my_string[5] = '\0';
H e l l o \0 W o r l d \0
The null character
Hello World
Hello
You cannot assign string literals after initialization!
Initialization
But don't worry! There are some library
functions that help us copy string literals
into arrays. More on that later.
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Strings
23
-Strings are just arrays of characters
char my_string[20] = "Hello World";
my_string = "Hello World";
H e l l o W o r l d \0
my_string[5] = '\0';
H e l l o \0 W o r l d \0
The null character
Hello World
Hello
You cannot assign string literals after initialization!
Initialization
But don't worry! There are some library
functions that help us copy string literals
into arrays. More on that later.
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointers
24
There is nothing difficult
here, just keep the basics
in mind.
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointers
25
-Pointers hold memory addresses of stored variables
-You can create a pointer for any data type.
-The two basic operators:
Indirection Address
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointers
26
-Keep the memory model in mind
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
These are addresses of the
memory locations
These are the contents of
the memory locations
The denotes uninitialized
or junk data in memory
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Declarations
27
-To declare a pointer of any type, simply use the
indirection operator before the
identifier
-Use caution when declaring
multiple pointers at once
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
int *p_to_int;
float *p_to_f1, *p_to_f2;
int *p1, *p2, i1;
These are pointers
This is an integer
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
28
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
29
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 0
0x06
0x07
0x08
0x09
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
30
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 0
0x06
0x07 1
0x08
0x09
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
31
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 0
0x06
0x07 1
0x08
0x09 0x05
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
32
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 0
0x06
0x07 1
0x08
0x09 0x05
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
Don't let this
initialization confuse
you. All we're really
doing is
int *p;
...
p = &i;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
33
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 0
0x06
0x07 1
0x08
0x09 0x05
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
*p = 4;
p = &j;
*p = 7;
i = *p;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
34
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 4
0x06
0x07 1
0x08
0x09 0x05
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
*p = 4;
p = &j;
*p = 7;
i = *p;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
35
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 4
0x06
0x07 1
0x08
0x09 0x07
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
*p = 4;
p = &j;
*p = 7;
i = *p;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
36
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 4
0x06
0x07 7
0x08
0x09 0x07
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
*p = 4;
p = &j;
*p = 7;
i = *p;
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointer Access
37
-Use the address operator to set a pointer
0x00
0x01
0x02
0x03
0x04
0x05 7
0x06
0x07 7
0x08
0x09 0x07
int i = 0; /* Will be located at 0x05 */
int j = 1; /* Will be located at 0x07 */
int *p = &i; /* Will be located at 0x09 */
*p = 4;
p = &j;
*p = 7;
i = *p;
This is called a
dereference, it obtains
the value stored at the
memory location the
pointer holds
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointers to Structures
38
-Similar concept
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Pointers to Structures
39
-Similar concept
0x00
0x01
0x02 0
0x03 1
0x04 0
0x05
0x06
0x07
0x08
0x09
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
Pointers to Structures
40
-Similar concept
0x00
0x01
0x02 0
0x03 1
0x04 0
0x05
0x06
0x07
0x08 0x02
0x09
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
p->x = 2;
p->y = 4;
Pointers to Structures
41
-Similar concept
0x00
0x01
0x02 0
0x03 1
0x04 0
0x05
0x06
0x07
0x08 0x02
0x09
Indirect member
access operator
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
p->x = 2;
p->y = 4;
Pointers to Structures
42
-Similar concept
0x00
0x01
0x02 2
0x03 1
0x04 0
0x05
0x06
0x07
0x08 0x02
0x09
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
p->x = 2;
p->y = 4;
Pointers to Structures
43
-Similar concept
0x00
0x01
0x02 2
0x03 4
0x04 0
0x05
0x06
0x07
0x08 0x02
0x09
p->y is the same as (*p).y
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
p->x = 2;
p->y = 4;
l.x = p->z;
Pointers to Structures
44
-Similar concept
0x00
0x01
0x02 2
0x03 4
0x04 0
0x05
0x06
0x07
0x08 0x02
0x09
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
struct loc
{
int x, y, z;
};
struct loc l; /* Will start at 0x02 */
struct loc *p; /* Will be at 0x08 */
l.x = 0; l.y = 1; l.z = 0;
p = &l
p->x = 2;
p->y = 4;
l.x = p->z;
Pointers to Structures
45
-Similar concept
0x00
0x01
0x02 0
0x03 4
0x04 0
0x05
0x06
0x07
0x08 0x02
0x09
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
-Common Type Specifier: const
-Common Storage Specifier: extern
const float pi = 3.14159;
Type and Storage Specifiers
46
extern int seconds;
...
int seconds = 3600;
pi is declared as
read only
seconds is visible
to all files linked the
your project
Must have an "extern" and
"non-extern" declaration
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
-An expression is one operand and zero or more operators
13
36 + 16
calculate_sum(1, 1)
(8 * (12 + 4))
Expressions
47
addition operator
constants may be operands
functions with return values may be operands
use parentheses to group
expressions -- innermost expressions
are evaluated first
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Unary Operators
48
Increment: ++x x++
Decrement: --x x--
Positive: +x
Negative: -x
Logical Negation: !x
Bitwise Negation: ~x
Address: &x
Indirection: *x
Size of: sizeof(x)
Type casting: (int)x (float)x (type)x
Array Subscript: x[3]
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Binary Operators
49
Addition: x + y
Subtraction: x - y
Multiplication: x * y
Division: x / y
Modulus: x % y
Bit shift: x << y
Bitwise AND: x & y
Bitwise OR: x | y
Bitwise XOR: x ^ y
Comparison: x == y x != y x < y
x <= y x > y x >= y
Logical: x && y x || y
Assignment: x = y
Compound Assignment: x += y x -= y x *= y
x /= y etc.
Be careful about types
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Binary Operators
50
Comma: x, y
Member Access: x.y x->y
The Ternary Operator
x ? y : z
CSC 4103 Operating SystemsFall 2008 – Lecture 2 – C Summary
Next Time
51
-A closer look at the highlighted operators
-Statements: actions and control flow
-Functions
-Program structure
-Helpful code snippets
top related