Download presentation
Presentation is loading. Please wait.
1
C Basics
2
Historical Background
Created at Bell Labs – to program Unix Professional programmer's language “stays out of programmer's way” Assumption that programmers know what they want to do & how to use language constructs to accomplish it Terse syntax Small language – designed for efficiency Type system & error checking – compile time only Run time is very small (efficiency) Programmer must handle memory management
3
Development Environment
Compiler – gcc Open source compiler OS – unix Need to be able to work at command line IDE Eclipse Codeblocks Any editor (vi, emacs, …..)
4
Integer Types Integer char – 1 byte
short – small integer – at least 16 bits int – default integer – at least 16 bits – hardware implementation long – large integer – at least 32 bits Portability problems Can use typedefs to help
5
Integer Types char constants '\n' – newline '\t' – tab
'\0' – null character '\O12' – character with octal value 12 int constants 0x10 – integer with hex value 10 O12 – integer with octal value 12
6
Integer Types Automatic “promotion” of integer types 'j' + 4
No overflow checking
7
Floating Point Types float – single precision floating point number
double – double precision floating point number long double – possibly even bigger floating point number Constants – default is double 3.14f 3.14l Do not use equality (==) to compare 2 floating point numbers – round off error
8
C Syntax Comments – same as Java Variable declarations – same as Java
Case sensitive Assignment operator – same as Java Truncation – opposite of promotion – allowed in C int n = 1000; char c = n; Value of c is whatever is in the bottom 8 bits of n double pi = 3.14; int n = pi; n has value 3 No boolean – use int
9
Mathematical Operators
Same as Java Logical operators 0 is false Anything else is true Bitwise operators ~ bitwise negation & bitwise and | bitwise or ^ bitwise exclusive or >> right shift << left shift
10
Control Structures Same as Java
11
Complex Data Types Arrays & records (structs) struct fraction {
int numerator; int denominator; }; fraction f; f.numerator = 2; f.denominator = 5; Arrays – similar to Java No index checking Constant pointer
12
Complex Data Types Array of structs struct fraction numbers [100];
13
Pointers * used to indicate a pointer int* n; char* c
struct fraction* f; * to left dereferences pointer *n – integer being pointed to by n (pointee) (*f).numerator or f -> numerator Example struct fraction** f; (*f) -> numerator int* foo[20]
14
Pointers & Operator Returns address of (pointer to)
&n – address of n (pointer to n) NULL Symbolic name for 0 – pointer has no pointee Pitfalls Pointer must be declared & allocated Pointee must be declared & allocated Pointer must point to pointee
15
C Strings Array of characters – null character terminated
string.h library – support for strings defined in this way Developer must manage the string memory & the null character terminator Buffer overflow potential strcpy does not check for size – just copies the characters Better approach is to create the strings dynamically when size is known
16
C Strings char* Pointer to char – pointer to array of characters – string
17
Typedef Way of giving a name to a type
typedef <type> <name> Examples: typedef NULL UGA typedef struct fraction frac typedef struct treenode* tree struct treenode { int data; tree left, right; }
18
Functions Syntax – same as Java All parameters – passed by value
Pass by reference – semantics can be achieved by passing pointer of argument void swap (int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int n = 2; int m = 3; swap (&n, &m);
19
Functions const Qualifier indicating that code cannot change variable
Ex: void foo (const struct fraction* f); foo cannot change fraction pointed by f
20
C Programs Main function int main()
Return value 0 – program terminates normally Other values – error codes Can have arguments for command line arguments Prototype Name & arguments for function Must appear before implementation of function
21
C Programs Preprocessor – executes before compiler Directives
#define – establishes symbolic names #include – copies text from another file #if If else logic used on symbols created by #define Used at compile time to determine which code to compile #once – prevents problems which can occur if a file is included more than once
22
C Programs Header files & source files Header files .h extension
contain prototypes Source files .c or .cc extension contain implementations assert Macro implementation of assertions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.