An Ungentle Introduction to C

Similar documents
CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CSCI 171 Chapter Outlines

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

EL6483: Brief Overview of C Programming Language

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Compiling and Running a C Program in Unix

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Kurt Schmidt. October 30, 2018

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Topic 6: A Quick Intro To C

A Fast Review of C Essentials Part I

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

BLM2031 Structured Programming. Zeyneb KURT

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Programming Review CSC 4320/6320

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Input And Output of C++

6.096 Introduction to C++ January (IAP) 2009

Computers Programming Course 5. Iulian Năstac

Lecture 03 Bits, Bytes and Data Types

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Object-Oriented Programming

COMP26912: Algorithms and Imperative Programming

Review of the C Programming Language

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

PRINCIPLES OF OPERATING SYSTEMS

Heap Arrays. Steven R. Bagley

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Review of the C Programming Language for Principles of Operating Systems

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Introduction to Programming Using Java (98-388)

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Lecture 2: C Programming Basic

Chapter 11 Introduction to Programming in C

Understanding Pointers

CSE 374 Programming Concepts & Tools

Short Notes of CS201

Motivation was to facilitate development of systems software, especially OS development.

Introduction to C++ with content from

CS3157: Advanced Programming. Announcement

Class Information ANNOUCEMENTS


Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

High Performance Programming Programming in C part 1

Page 1. Agenda. Programming Languages. C Compilation Process

CS201 - Introduction to Programming Glossary By

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Main differences with Java

Chapter 11 Introduction to Programming in C

211: Computer Architecture Summer 2016

Final CSE 131B Spring 2004

COMP 2355 Introduction to Systems Programming

Chapter 11 Introduction to Programming in C

CS349/SE382 A1 C Programming Tutorial

The C Programming Language

Programming Language Basics

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CMPE-013/L. Introduction to C Programming

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Chapter 11 Introduction to Programming in C

C: How to Program. Week /Mar/05

Friday, February 10, Lab Notes

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Type Checking. Prof. James L. Frankel Harvard University

C & Data Structures syllabus

Computer Systems Principles. C Pointers

COMP 2355 Introduction to Systems Programming

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Programming in C. What is C?... What is C?

a data type is Types

Motivation was to facilitate development of systems software, especially OS development.

Ch. 3: The C in C++ - Continued -

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Computer Organization & Systems Exam I Example Questions

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CMPSC 311- Introduction to Systems Programming Module: Build Processing

EL2310 Scientific Programming

Data Representation and Storage

Chapter 2 - Introduction to C Programming

Contents. A Review of C language. Visual C Visual C++ 6.0

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Transcription:

Warum C? - Sicherheit auf allen Systemschichten Applikationen Hilfssysteme BS-Werkzeuge BS-Kern HW Evtl. Hochsprachen Skripte C Assembler - je tiefer die kompromittierte Schicht, umso größer der Schaden An Ungentle Introduction to C

What you should know and what you will learn You should know how to program in an imperative language, syntax and semantic of Java, shell-based interaction with a Unix system. You will learn a rough overview about C. What to do next? Get a got book about C and read. Try it (do the assignments). Use the online manual pages. General Properties no OOP functions (with side effects) and kind-of modules no garbage collection, manual memory management no memory protection no internal debug support only standardized basic libraries To understand how it works, forget abstraction For usage, apply abstraction whenever possible

Compilation Stages Sourcecode - declarations (*.h) - functions (*.c) Preprocessor Objectfiles - compiled source code (*.o) - third party libraries (lib*.a, lib*.so) Compiler Executable Linker Runner - bind with dynamic libraries - execute the function main(..) Basic Types, Literals, Expression Basic scalar types (# Bytes) (signed unsigned) char (1), short (2), int (4) float (4), double(8), long double Casting of basic types often does what you want, but you may loose precision, you may interfer with the sign bit. Literals are similar to Java, but no Unicode. Enumerations enum boolean {YES, NO}; Expressions are very similar to Java. No instanceof operator sizeof(<typeid>) returns the amount of memory used by an instance

Aggregates Arrays char str[50]; can be multidimensional indices run from 0 to (n-1) Structures struct Person{int age; int socid}; struct Person fred, carl; structures can create new types typedef struct Person person_t; person_t anja; assignment of structures (fred = carl):copy of every field comparison of structures is not allowed Unions union value {int ival, float fval}; Structures where each field uses the same memory carry no type information => dangerous to use Structure of a Sourcecode File // read declarations of other modules #include <mod1.h> #include../mod2.h... // function and variable definitions int globalvar=-1; int add(int para) { // local variables int help = 0;... // expressions... return para++; }... Search system path Uses current path Variable declarations and expressions must not be intermixed. Every source file is a module.

Scope and Lifetime of Variables... int aglobal; extern int hisglobal; static int alocal;... void function(int p1, char p2) { int m1; static int m2;... }... Global and unique in the whole application Imported from another module Only available in this module Available only in the function, valid only when the function executes value is saved between calls By-Value semantic for parameters Pointers (1/2) What is a Pointer? A pointer is a typed reference to memory Type declaration: int *iptr; void *ptr; Operators: <type> *id // pointer to an integer // pointer to something access the referenced content *(ptr) get the reference of a variable &(id) Example: int i=1; int *iptr; iptr = &i; // set iptr to point at i *(iptr) = 2; // set i from 1 to 2

Pointers (2/2) Pointers,Structures, Like in Java pointers/references allow for efficient handling of structures (objects without methods). void workonperson(person_t *personptr) {} Recursive aggregates: struct Tree { int value; struct Tree *left,*right; }; Shortcut: struct Tree *id; (*id).left is equals to id->left Pointers to functions are possible (but out of scope here) Pointer Arithmetic Arrays are very similar to Pointers Arrays support [] operator An array is no variable, it cannot be changed (its values can) // Example: add 1 to every member of an array int arr[10], i, *iptr;... iptr=arr; // arrays are some kind of pointers for (i=0;i<10;i++) {*iptr += 1; iptr++;} pointers are typed, +1 will shift the pointer according to the length of the type (arr++ is forbidden) all integer arithmetic is allowed casting + pointer arithmetic opens strange possibilities

Strings Strings are: either pointers to character char *str; or arrays of characters char str[80]; strings are mutable (=> side effects are dangerous) By convention a string is terminated by \0 hallo is actually { h, a, l, l, o, \0 } you need to reserve memory for the termination character if you forget termination, the strangest things can happen the standard library provides numerous functions for strings do a man string on a Unix box Basic I/O Formatted output to stdout: int printf(char *format,... ); (C supports arbitrary many parameters; out of scope) str = STR ; num = 4712; printf( String: %s, Num: %d \n, str, num); will print: String: STR, Num: 4712 Input from stdin:int scanf(char *format,... ); char name[80]; int age; float pi; scanf( %s,name); scanf( %d %f,&age, &pi); input a string from stdin until newline/eof parse the string according to the specifier store the results in the given variables read the documentation!!!

Memory Allocation void *malloc(size_t size) allocates some memory and returns a pointer to it void free(void *ptr) frees the memory struct Person *createperson(char *name) { struct Person *res; res = (struct Person *) malloc(sizeof(struct Person)); res->name = name; return res; } Preprocessor Constants #define MAXSIZE 100 Macros #define ASSERT(cond,msg) if(cond) {printf(msg);} Conditional inclusion #define DEBUG #ifdef DEBUG #define ASSERT(cond,msg) if(cond){printf(msg);} #else #define ASSERT(cond,msg) #endif

How to compile and start Start of an executable by calling the function int main(int argc,char **argv) with the command line parameters (Array of strings). Create an executable from a C-File > gcc file.c -o file Create an object file file.o > gcc -c file.c Link object files > gcc file1.o file2.o file3.o -o file Start the executable > file