Static variable Another class of local variable is the static type.. It is specified by the keyword static in the variable declaration.. An exampleint global = 10; //global variable
Trang 1C/C++ Tutorial
CSU480
Trang 2Who is the TA?
Name: Jingjing Duan
Office Hours: (might change)
Tue & Fri 2:00-4:00
Office: Room 266, WVH
Email: duanjj@ccs.neu.edu
Class web: www.ccs.neu.edu/course/csu480
Trang 4Hello World Program
The source code
Trang 5Hello World Program
$ gcc hello.c –o hello
hello.c source file
hello compiler-generated executable file
Trang 6 How to execute?
./hello
“./ ” indicates the following file “hello” resides under the current directory
Hello World Program
Q: why “.” is not included in $PATH
Trang 7Hello World Program
A: security consideration.
system
Trang 8Name Description Size* Range*
char Character or small
integer 1 byte signed: -128 to 127unsigned: 0 to 255 short int
(long)
Long integer 4 bytes signed: -2147483648 to
2147483647 unsigned: 0 to 4294967295 float Floating point
number 4 bytes 3.4e +/- 38 (7 digits)
Data types
Trang 9unsigned char value = -1;
printf(“The value is %d \n”, value);
unsigned char value = 300;
printf(“The value is %d \n”, value);
Trang 10value = 300 00101100 44 overflow
Trang 11 Local variable
Local variables are declared within the body of a function, and can only be used within that function.
Static variable
Another class of local variable is the static type It is
specified by the keyword static in the variable declaration The most striking difference from a non-static local
variable is, a static variable is not destroyed on exit from the function
Trang 12 An example
int global = 10; //global variable
int func (int x)
{
static int stat_var; //static local variable int temp; //(normal) local variable int name[50]; //(normal) local variable
……
Trang 13Variable Definition vs Declaration
Definition Tell the compiler about the variable: its type
and name, as well as allocated a memory cell for the variable
Declaration Describe information ``about'' the variable,
doesn’t allocate memory cell for the variable
Trang 15The printf() function can be instructed
to print integers, floats and string
properly
The general syntax is
printf( “format”, variables);
An example
int stud_id = 5200;
char * name = “Mike”;
printf(“%s ‘s ID is %d \n”, name, stud_id);
Trang 16printf(“The student id is %5d \n”, stud_id); The value of stud_id will occupy 5
Trang 17 Why “\n”
It introduces a new line on the terminal screen.
escape sequence
Trang 18Arithmetic Operations
Trang 19Arithmetic Assignment Operators
Trang 20Increment and Decrement Operators
Trang 24Logical Operations
In C, there is no specific data type to represent
“true” and “false” C uses value “0” to represent
“false”, and uses non-zero value to stand for
Trang 25&& and || have different meanings from & and |.
& and | are bitwise operators.
Trang 27int i = 10; int j = 15; int k = 15; int m = 0;
Trang 28int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) => false
if( i != j || k < j) => true
if( j<= k || i > k) => true
if( j == k && m) => false
if(i) => true
if(m || j && i ) => true
Did you get the correct answers?
Trang 29statement … }else{
statement …
}
Trang 32 The switch statement
Trang 35 The while statement
Trang 36expression3;
}
equals
Trang 37Arrays & Strings
Trang 38 Strings
Strings are defined as arrays of
characters.
The only difference from a character array
is, a symbol “\0” is used to indicate the
end of a string
For example, suppose we have a
character array, char name[8], and we
store into it a string “Dave”.
Note: the length of this string 4, but it
occupies 5 bytes.
Trang 39Functions are easy to use; they allow complicated
programs to be broken into small blocks, each of which is easier to write, read, and maintain This is called modulation.
Trang 41C, but also brings most trouble to C
programmers Over 90% bugs in the C
programs come from pointers.
”
(http://www.ioccc.org/)
A pointer is a variable which contains the
address in memory of another variable
In C we have a specific type for pointers.
Trang 42 Declaring a pointer variable
where & is called address of operator
How to get the value of the variable
indicated by the pointer?
int y = *pointer;
Trang 4333 22 00 00
0x5200
pointer
What happens in the memory?
Suppose the address of variable x is 0x5200 in the above
example, so the value of the variable pointer is 0x5200
X
Trang 44swap the value of two variables
Trang 45Why is the left one not working?
Trang 46Why is the right one working?
Trang 47 Pointers and Arrays
Pointers and arrays are very closely linked in
A string can be represented by a char *
pointer.
Trang 49Command-Line Argument
In C you can pass arguments to main()
function
int main(int argc, char * argv[]);
argc indicates the number of arguments argv is an array of input string pointers
./hello 10
Trang 50
What value is argc and argv?
Let’s add two printf statement to get the value of argc and argv.
printf(“The argc is %d \n”, argc);
for(i=0; i < argc; i++){
printf(“The %dth element in argv is %s\n”, i, argv[i]);
}
Trang 51 The output
The argc is 2
The 0th element in argv is /hello
The 1th element in argv is 10
The trick is the system always passes the name of the executable file as the first argument to the
main() function.
How to use your argument?
Be careful Your arguments to main() are always in string format
Taking the above program for example, the
argv[1] is string “10”, not a number You must
convert it into a number before you can use it
Trang 53 A data structure is also a data type
struct stud_record my_record;
struct stud_record * pointer;
pointer = & my_record;
Accessing a field inside a data structure
or pointer->id = 10; “->”
Trang 54Memory Allocation
Non-static local variable is an example of stack memory allocation
Such memory allocations are placed in a
Static local variable and global variable require static memory allocation Static memory allocation happens before the
Trang 55 Dynamic memory allocation
It allows the program determine how
much memory it needs at run time, and allocate exactly the right amount of
storage.
The region of memory where dynamic
allocation and deallocation of memory
can take place is called the heap.
Note: the program has the responsibility
to free the dynamic memory it allocated
Trang 56Memory arrangement
Trang 57 Functions for the dynamic memory allocation void *malloc(size_t number_of_bytes);
allocates dynamic memory size_t sizeof(type);
returns the number of bytes of type void free(void * p)
releases dynamic memory allocation
An example of dynamic memory allocation int * ids; //id arrays
int num_of_ids = 40;
ids = malloc( sizeof(int) * num_of_ids);
…… Processing …
free(ids);
Trang 58 Allocating a data structure instance
struct stud_record * pointer;
pointer = malloc(sizeof(struct stud_record)); pointer->id = 10;
Never calculate the size of data structure
yourself The reason is the size of data types
is machine-dependent Give it to sizeof()
function.
size of int
Trang 59Programming Tips
Replacing numbers in your code with macros
- don’t use magic numbers directly
#define MAX_NAME_LEN 50;
char name[MAX_NAME_LEN];
Avoiding global variables
- modulation is more important
Giving variables and functions a nice name
Trang 60 Indenting your code (clearance)
Don’t rush into coding Plan first
Printing out more debugging information
Trang 61C vs C++
C++ is a superset of C
C++ has all the characteristics of C
Using g++ to compile your source code
Trang 62Books recommended
Kernighan and Dennis Ritchie Second edition Prentice-Hall, 1988 (C Bible)
Bjarne Stroustrup Third edition
Addison-Wesley, 1997 (C++ Bible)
Trang 63Thanks