C for Java ProgrammersCS 414 / CS 415Alin DobraDepartment of Computer ScienceCornell [email protected] 21, 2002Why 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 JavaAll this make C a far better choice for system programming.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 mis-takes– C programming requires strict discipline• Provide additional information to get you started– compilation and execution– printf debuggingHello World Example/* Hello World program */#include <stdio.h>void main(void){printf("Hello World.\n");}$ ./hello$ Hello World.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 (architecturedependent)– can be signed or unsigned• Floating point types: float and double like in Java.• No boolean type, int or char used instead.– 0 ⇒ false– 6= 0 ⇒ truePrimitive Types Exampleschar c=’A’;char c=100;int i=-2343234;unsigned int ui=100000000;float pi=3.14;double long_pi=0.31415e+1;Arrays and Strings• Arrays:/* declare and allocate space for array A */int A[10];for (int i=0; i<10; i++)A[i]=0;• Strings: arrays of char terminated by \0char[] name="CS415";name[4]=’5’;– Functions to operate on strings in string.h.∗ strcpy, strcmp, strcat, strstr, strchr.printf function• Syntax: printf(formating_string, param1, ...)• Formating string: text to be displayed containing special mark-ers 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","CS415", 80);enum: enumerated data-typesenum month{JANUARY,FEBRUARY,MARCH};• Each element of enum gets an integer value and can beused as an integer.enum month{JANUARY=1,FEBRUARY=3,MARCH};Pointers• address of variable: index of memory location where vari-able is stored (first location).• pointer: variable containing address of another variable. type*means pointer to variable of type type.• Example:int i;int* ptr_int; /* ptr_int points to some random location */ptr_int = &i; /* ptr_int points to integer i */(*ptr_int) = 3; /* variable pointed by ptr_int takes value 3 */• & address operator, * dereference operator.• Similar to references in Java.Pointers (cont.)• Attention: dereferencing an uninitialized pointer can havearbitrary effects (including program crash).• Good programming advice:– if a pointer is not initialized at declaration, initialize it withNULL, the special value for uninitialized pointer– before dereferencing a pointer check if value is NULLint* p = NULL;...if (p == NULL){printf("Cannot dereference pointer p.\n");exit(1);}Structures• The record type of C, like Java classes with only members:struct birthday {char* name;int month;int day;int year;};struct birthday mybirthday = {"Alin",1,1,1990};char FirsLetter = mybirthday.name[0];mybirthday.month = 10;Structures (cont.)• Structures can have as elements types already defined.• Structures can refer to pointer to themselves:struct list_elem{int data;struct list_elem* next;};• -> is syntax sugaring for dereference and take element:struct list_elem le={ 10, NULL };struct list_elem* ptr_le = ≤printf("The data is %d\n", ptr_le->data);Data-type Synonyms• Syntax: typedef type alias;• Example:typedef int Bool;Bool bool_var;typedef int* Intptr;Intptr p; /* p is a pointer to int */typedef struct list_el list_el; /* list_el is alias for struct list_el */struct list_el {int data;list_el* next; /* this is legal */};• Advantage: easier to remember, cleaner code.void* and Type Conversion• Type conversion syntax: (new_type)expression_old_type• Examples:float f=1.2;int i = (int)f; /* i assigned value 1 */char c=i; /* implicit conversion from int to char */float g=i; /* implicit conversion; g=1.0 */• Extremely useful conversion is to and from void* (pointerto unspecified type):#include <string.h>char str1[100];char str2[100];memcpy( (void*) str2, (void*) str1, 100);• Always do explicit conversions.Common Syntax with Java• Operators:– Arithmetic:∗ +,-,*,/,%∗ ++,--,*=,...– Relational: <,>,<=,>=,==,!=– Logical: &&, ||, !, ? :– Bit: &,|,ˆ,!,<<,>>Common Syntax with Java (cont.)• Language constructs:– if( ){ } else { }– while( ){ }– do { } while( )– for(i=0; i<100; i++){ }– switch( ) { case 0: ... }– break, continue, return• No exception handling statements.Memory Allocation and DeallocationGlobal variables:• Characteristic: declared outside any function.• Space allocated statically before program execution.• Initialization done before program execution if necessary also.• Cannot deallocate space until program finishes.• Name has to be unique for the whole program (C has flatname space).Memory Allocation and Deallocation(cont.)Local variables:• Characteristic: are declared in the body of a function.• Space allocated when entering the function (function call).• Initialization before function starts executing.• Space automatically deallocated when function returns:– Attention: referring to a local variable (by means of apointer for example) after the function returned can haveunexpected results.• Names have to be unique within the function only.Memory Allocation and Deallocation(cont.)Heap variables:• Characteristic: memory has to be explicitly:– allocated: void* malloc(int) (similar to new in Java)– deallocated: void free(void*)• Memory has to be explicitly deallocated otherwise all thememory in the system can be consumed (no garbage col-lector).• Memory has to be deallocated exactly once, strange behav-ior can result otherwise.Memory Allocation and Deallocation(ex.)#include <stdio.h>#include <stdlib.h>int no_alloc_var; /* global variable counting number of allocations */void main(void){int* ptr; /* local variable of type int* *//* allocate space to hold an int */ptr = (int*) malloc(sizeof(int));no_alloc_var++;/* check if successfull */if (ptr == NULL)exit(1); /* not enough memory in the system, exiting */*ptr = 4; /* use the memory allocated to store value
View Full Document