2
Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level features like complex data-structures Access to all the details of the implementation  Explicit memory management  Explicit error detection Better performance than Java All this make C a far better choice for system programming.

3
Goals of Tutorial Introduce basic C concepts:  Need to do more reading on your own Warn you about common mistakes:  More control in the language means more room for mistakes  C programming requires strict discipline Provide additional information to get you started  Compilation and execution

5
Introduction A C Program contains functions and variables. The functions specify the tasks to be performed by the program. Ex: The above program has one function called main. This function tells your program where to start running. main functions are normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main function.

6
Introduction C is case-sensitive. C also denotes the end of statement with a semi- colon like Java. The // or /* comment */ designates a comment. #include simply includes a group of functions from the filename specified by( ). Ex:above stdio.h contains a list of standard functions for C to use, the function that our program above uses is printf. Printf takes a string of characters between quotation marks, and outputs them to the screen.

7
Primitive Types Integer types:  char : used to represent characters or one byte data(not 16 bit like in Java)  int, short and long : versions of integer (architecture dependent)  can be signed or unsigned Floating point types: float and double like in Java. No boolean type, int or char used instead.  0 => false  ≠ 0 => true

10
printf function Syntax: printf(formating_string, param1,...) Formating string: text to be displayed containing special markers where values of parameters will be filled: %d for int %c for char %f for float %lf for double %s for string Example: printf("The number of students in %s is %d.\n", “IT421", 95);

12
Pointers (cont.) Attention: dereferencing an uninitialized pointer can have arbitrary effects (including program crash). Good programming advice:  if a pointer is not initialized at declaration, initialize it with NULL, the special value for uninitialized pointer  before dereferencing a pointer check if value is NULL int* p = NULL;. if (p == NULL){ printf("Cannot dereference pointer p.\n"); exit(1); }

15
Functions Provide modularization: easier to code and debug. Code reuse. Additional power to the language: recursive functions. Arguments can be passed:  by value: a copy of the value of the parameter handed to the function  by reference: a pointer to the parameter variable is handed to the function Returned values from functions: by value or by reference

20
Variable Before a variable can be used it must be declared. Declaring a variable in C is easy, specify the type and the name for your variables. (int, double, char) Ex: double age; int number; char * name; char x;

21
Input & output function scanf function simply gets a value from the user. Printf takes a string of characters between quotation marks, and outputs them to the screen. Notice some special character sequences contained in both the scanf and printf :  %f, %c, %s and %d. These tell printf and scanf what type of variables to expect.  %f corresponds to double, %d is for int % s for string and %c for character.

24
Programs with Multiple Files (cont.) File main.c: #include #include "mypgm.h" void main(void){ myproc(); } Have to compile files mpgm.c and main.c to produce object files mpgm.obj and main.obj (mpgm.o and main.o on UNIX). Have to link files mpgm.obj, main.obj and system libraries to produce executable. Compilation usually automated using nmake on Windows and make on UNIX.

25
Things to remember Initialize variables before using, especially pointers. Make sure the life of the pointer is smaller or equal to the life of the object it points to.  do not return local variables of functions by reference  do not dereference pointers before initialization or after deallocation C has no exceptions so have to do explicit error handling. Need to do more reading on your own and try some small programs.

26
Compile and run your C program under Linux The program hello.c can be compiled with the GCC as follows: gcc -o hello hello.c The -o option informs the compiler of the desired name for the executable (i.e., runnable) file that it produces. The name used in this example is hello. If the -o option is omitted, the compiler will give the name a.out to the executable file.executable./hello you run by the program typing 'hello' at the command line.