CC Basics Basic Concepts Basic functions of each

C/C++ Basics

Basic Concepts • Basic functions of each language: Input, output, math, decision, repetition • Types of errors: Syntax errors, logic errors, runtime errors. • Debugging • Machine language, assembly language, high level languages

Procedural Programming Data Element Procedure A 1 -3 Procedure B

Procedural languages … • • Global variables Primitive data types, Arrays, Records/Structures lots of functions/procedures/modules Uniqueness of “names” requirement Struct + protection + methods ~= class (OOP)

SW Complexity reality SW Complexity Size of the application – Lines of Code

SW complexity? Lots of lines in code – hard to understand Not many meaningful comments Too many variables – not named well Complex functions – too nested, too lengthy, too many if conditions • Inter-dependancies – changes that have to be done together in multiple files • When you fix a bug, you make a few more. • •

SW Complexity ideal Size of the application – Lines of Code

Object-Oriented Programming Interface Object Attributes (data) typically private to this object Other objects Methods (behaviors / procedures) 1 -8

C vs. C++ • C++ is backward compatible with C • C++ supports OOP • C++ standard library contains lot more functionality • Improved I/O mechanism • Lot of new header files • C is very efficient, C++ is close too. • …

C++ vs. Java: Similarities • Both support OOP. Most OOP library contents are similar, however Java continues to grow. • Syntax is very close – Java has strong influence of C/C++. Easy to learn the other language when you know one of these.

C++ vs. Java: differences C++ Java Write once, compile everywhere unique executable for each target Write once, run anywhere same class files will run above all target-specific JREs. No strict relationship between class names and filenames. Typically, a header file and implementation file are used for each class. Strict relationship is enforced, e. g. source code for class Pay. Roll has to be in Pay. Roll. java I/O statements use cin and cout, e. g. cin >> x; cout << y; I/O input mechanism is bit more complex, since default mechanism reads one byte at a time (System. in). Output is easy, e. g. System. out. println(x); Pointers, References, and pass by value are supported. No array bound checking. Primitive data types always passed by value. Objects are passed by reference. Array bounds are always checked. Explicit memory management. Supports destructors. Automatic Garbage Collection. Supports operator overloading. Specifically operator overloading was left out.

C standard library • http: //en. wikipedia. org/wiki/C_standard_library • stdio. h: scanf(), printf(), getchar(), putchar(), gets(), puts(), … • stdlib. h: atof(), atoi(), rand(), srand(), malloc(), free(), … • math. h: sin(), cos(), sqrt(), … • string. h: strcpy(), strcmp(), … • ctype: isdigit(), isalpha(), tolower(), toupper() • …

Sample C programs: prog 1. c These are in ~veerasam/students/basics #include <stdio. h> main() { int in 1, out; double in 2; puts("Enter values: "); scanf("%d%lf", &in 1, &in 2); // & means "address of" // %d int, %f float, %lf double out = in 1 + in 2; printf("Output is %dn", out); }

Sample C programs: prog 2. c #include <stdio. h> #include <stdlib. h> main() { char line[10]; int out; gets(line); puts(line); out = 2 * atoi(line); printf("%dn", out); }

Sample C programs: prog 3. c #include <stdio. h> #include <stdlib. h> main() { srand(getpid()); printf("%dn", rand()); }

Sample C programs: prog 4. c #include <stdio. h> int add(int x, int y) { return x + y; } main() { int in 1, in 2, out; puts("Enter values: "); scanf("%d%d", &in 1, &in 2); out = add(in 1, in 2); printf("Output is %dn", out); }
![Sample C programs: simple. c #include <stdio. h> main() { int i, arr[10]; puts("Let Sample C programs: simple. c #include <stdio. h> main() { int i, arr[10]; puts("Let](http://slidetodoc.com/presentation_image_h2/5d28d24eb6ddcbbbfd1fe05cf29ad861/image-17.jpg)
Sample C programs: simple. c #include <stdio. h> main() { int i, arr[10]; puts("Let me init the array contents. "); for( i=0 ; i<20 ; i++) arr[i] = i; puts("Well, I survived!"); return 0; }
![Sample C programs: simple. c #include <stdio. h> main() { int i, arr[10]; puts("Let Sample C programs: simple. c #include <stdio. h> main() { int i, arr[10]; puts("Let](http://slidetodoc.com/presentation_image_h2/5d28d24eb6ddcbbbfd1fe05cf29ad861/image-18.jpg)
Sample C programs: simple. c #include <stdio. h> main() { int i, arr[10]; puts("Let me init the array contents. "); for( i=0 ; i<20 ; i++) arr[i] = i; puts("Well, I survived!"); return 0; }
![Sample C programs: simple 1. c #include <stdio. h> int i, array[10], array 2[10]; Sample C programs: simple 1. c #include <stdio. h> int i, array[10], array 2[10];](http://slidetodoc.com/presentation_image_h2/5d28d24eb6ddcbbbfd1fe05cf29ad861/image-19.jpg)
Sample C programs: simple 1. c #include <stdio. h> int i, array[10], array 2[10]; main() { puts("n. Array's contents are"); for( i=0 ; i<10 ; i++) printf("%dn", array[i]); puts("n. Array 2's contents are"); for( i=0 ; i<10 ; i++) printf("%dn", array 2[i]); puts("Let me init the array contents. "); for( i=-10 ; i<20 ; i++) array[i] = i; puts("n. Array's contents are"); for( i=0 ; i<10 ; i++) printf("%dn", array[i]); puts("n. Array 2's contents are"); for( i=0 ; i<10 ; i++) printf("%dn", array 2[i]); puts("n. Well, I survived!"); return 0; }
![Sample C programs: simple 2. c #include <stdio. h> main() { int i, array[10], Sample C programs: simple 2. c #include <stdio. h> main() { int i, array[10],](http://slidetodoc.com/presentation_image_h2/5d28d24eb6ddcbbbfd1fe05cf29ad861/image-20.jpg)
Sample C programs: simple 2. c #include <stdio. h> main() { int i, array[10], array 2[10]; print. Array(int *arr) { int i; for(i=0 ; i<10 ; i++) printf("%dn", arr[i]); } } puts("n. Array's contents are"); print. Array(array); puts("n. Array 2's contents are"); print. Array(array 2); puts("Let me init array contents. "); for( i=-10 ; i<20 ; i++) array[i] = i; puts("n. Array's contents are"); print. Array(array); puts("n. Array 2's contents are"); print. Array(array 2); puts("n. Well, I survived!"); return 0;

Sample C programs: simple 3. c #include <stdio. h> print. Array(int *arr) { int i; main() { int i, array[10], array 2[10]; for(i=0 ; i<10 ; i++) printf("%dn", arr[i]); puts("n. Array's contents are"); print. Array(array); puts("n. Array 2's contents are"); print. Array(array 2); } puts("Let me init array contents. "); for( i=-10 ; i<20 ; i++) array[i] = i; puts("n. Array's contents are"); print. Array(array); puts("n. Array 2's contents are"); print. Array(array 2); puts("n. Well, I survived!"); return 0; }

Sample C programs: simple 4. c #include <stdio. h> print. Array(int *arr) { int i; second() { int i, array[10], array 2[10]; for(i=0 ; i<10 ; i++) printf("%dn", arr[i]); puts("n. Array's contents are"); print. Array(array); puts("n. Array 2's contents are"); print. Array(array 2); } puts("Let me init array contents. "); main() for( i=-10 ; i<20 ; i++) { array[i] = i; second(); puts("n. Array's contents are"); puts("n. Well, I survived!"); print. Array(array); return 0; puts("n. Array 2's contents are"); } print. Array(array 2); }

Things to observe • • Memory allocation for arrays Array length is not stored with arrays Potentional issues with scanf() and printf() Different behavior when overshooting arrays in heap vs. in stack

Sample C program: multiple files

Compare with Java file 1. java file 2. java file 3. java … filen. java

C files & runtime environment file 1. c file 2. c file 3. c … filen. c

Debugging : gdb Compile with –g option to use the debugger. Most used commands: • run • list [method name] • break [line_number] • step • next • continue • print [variable]
- Slides: 27