Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C CSCE 343.

Similar presentations


Presentation on theme: "Introduction to C CSCE 343."— Presentation transcript:

1 Introduction to C CSCE 343

2 Hello World

3 Comments Multi-line comments only in standard
/* … */ Single line comments allowed in certain compilers: // …

4 Declarations In C standard, all declarations must appear above regular code. char red; int j; for (int i =0; i < 10 : i++){ } Not required in newer compilers.

5 Compiling using Cygwin gcc
The gcc compiler (GNU Compiler Collection) is on the lab machines through cygwin. gcc –o hello hello.c ./hello On shemp, same process.

6 Cygwin UNIX environment on Windows
Provides many of the UNIX/Linux utilities Differences from regular DOS prompt: Uses forward slash for directory separator Uses ls not dir Must be run from Cygwin icon Available for free download.

7 main Similar to public static void main
Return value indicates success/failure to shell. 0: success Non-zero: failure

8 Arrays No new operator in C: Brackets do not follow type
int a[10]; // array of ints char *b[20]; // array of pointers // to char Brackets do not follow type Except in prototypes No array index bounds checking! No length variable sizeof(a) / sizeof(a[0])

9 Pointers Dereferencing operator: * Address operator: &
Except in declaration Address operator: & Declaring pointers: int * xPtr; // Pointer to int char * cPtr; // Pointer to char int y,x =5; xPtr = &x; y = *xPtr + x; Act very much like references in Java, but not quite the same. Can point to primitive types Can do arithmetic on pointers

10 Arrays and Pointers Array name (without brackets) is an alias to a pointer to the first element in the array. Arrays can be traversed with pointers. Brackets can be used with pointers. int a[10]; int *aPtr; aPtr = a; aPtr[6] // same as a[6] *(aPtr + 6) // same as aPtr[6]

11 Strings Arrays of char Terminated with the null character: ‘\0’
Array can be longer than the string Must be longer by at least one. Can’t concatenate with + Use strncat strcat(st1,”def”); // st1+”def”

12 Types Usual suspects: Unusual: No boolean data type Examples
int, char, double, float, short, long ... Unusual: long long, unsigned int, … No boolean data type 0 false Non-zero true Any numeric or pointer type works! Examples if (0) if (y=0) if (y==0)

13 Input stdin: alias for the console
standard input Two main choices for reading from stdin: scanf: input formatted data directly into variables reading strings the \n is left in the input stream fgets: read a full string, including \n, parse it yourself, \n is part of the resulting string String to int: atoi String to float (double): atof

14 Struct Encapsulation construct Encapsulates data, but not methods
Everything is public (no access restriction) No inheritance struct employee{ int id; char name[NAME_LEN+1]; double salary; } emp1,emp2; struct employee emp3;

15 typedef Define new types
Commonly used to define a type name for struct typedef struct{ int id; char name[NAME_LEN+1]; double salary; } Employee; Employee emp1,emp2;

16 File I/O <stdio.h>
Basic file access tool: file pointers FILE *filePtr; Open/close: fopen, fclose filePtr = fopen(“fileName”, “r/w/a/r+w/…”) fclose(filePtr) Reading: fgets (stg, 100, filePtr) //line at a time: check for NULL getc(filePtr) //char at a time: check for EOF Writing fprintf (filePtr, stg) fprintf(filePtr, “format string”, variable-list)


Download ppt "Introduction to C CSCE 343."

Similar presentations


Ads by Google