#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Size: px
Start display at page:

Download "#include <stdio.h> int main() { printf ("hello class\n"); return 0; }"

Transcription

1 C #include <stdio.h> int main() { printf ("hello class\n"); return 0; }

2 Faculty אחראי קורס: ד"ר אופיר פלא מרצים: ד"ר אופיר פלא גב' חרות סטרמן מתרגלים: גב' חרות סטרמן מר נהור גולן מר גיא כהן צדק 2

3 Communications Moodle Forums: News must read Q & A ask the staff about course material 3

4 Course Objectives C programming language Practice of programming: Style Testing & Debugging Efficiency & Portability Modularity Basic computer structure 4

5 Books The C Programming Language, Brian W. Kernighan & Dennis M.Ritchie More books in website 5

6 Course Grading Exercises Frontal checking of the exercises with the T.A. וכו ) כל העבודה ביחידים )אסור זוגות, שלשות חלק מהתרגילים ייבדקו פרונטלית עם המתרגל! 6

7 Late Submission שבוע אחד: קנס 3 אין צורך באישור שבועיים: קנס של 5 אין צורך באישור סיבות אישיות: נקודות. נקודות. ללא קנס. צריך לשלוח בקשה למתרגל על סיבות כמו: מילואים, מחלה, חתונה, ברית, עם אישור )טופס מחלה, אישור מילואים וכו'( עומס לימודים ומועדי ב: לא סיבה לאיחור לא לשלוח לי מייל עם בקשות להארכה!

8 Plagiarism policy You may discuss your exercise with friends You must not exchange any written material (or code) between students You must understand all the code that you submit 8

9 How 2 get a 100 for the hw? Be honest! Organize your time = start as early as possible Read very carefully the instructions. Ask in the forum if something is unclear. Check each part of your code, including edge cases. Learn & explore by yourself! 9

10 Working environment Linux, gcc slides 0_linux_and_gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul 10

11 History C C++ Java 70 s Development of UNIX. (Richie+Kernigham Bell labs) 80 s Large efficient code. (Stroustrup Bell labs) 90 s Language for the web. (Sun Microsystems) Simple to convert to machine code. VM Fast and Efficient Secure and Safe 11 Welcome to C Easy to avoid bugs Easy to Debug

12 History C: First "standard" Default int Enums Return struct void // VLA variadic macros inlining multithreading Anonymous structs _Generic K&R 1 st ed. ('78) ANSI C ('89) C99 ('99) C11) 11) This course, without the VLA!

13 C Design Decisions Bare bones the language leaves maximal flexibility with the programmer Efficient code (operating systems) Full control on memory & CPU usage High-level Type checking High-level constructs Portable Standard language definition Standard library 13

14 C Warning Signs No run-time checks Array boundary overruns Illegal pointers No memory management Programmer has to manage memory 14

15 First Program in C // This line is a comment, /* and this line also. */ // This line defines standard I/O library #include <stdio.h> // main program entry point. Start form here int main() { // { } define a block printf("hello class!\n"); return 0; } 15

16 The basic syntax Similar to java you should know it already. 16

17 Basic Case sensitive White-space not important. End statements with ; Code block defined with { and } Return value from function using return X; String with " All lines below are legal: int x,y; x=5; { x; } ; 17

18 Statements - conditional if (expression) //statement or block else if (expression) //statement or block else (expression) //statement or block switch (integer value) later... 18

19 Statements - loops The usual suspects: int x,y; //in ANSI C you cannot declare inside the for! for (x=0,y=0;x<10 && y<5;x++,x+=2) //statement or block while (condition) //statement or block 19 do //statement or block while (condition)

20 Variables Statically typed each variable has a type. Declare by: <type> <name>. int x; int x,y; Optionally initialize (otherwise undefined!) 20 int x=0; Important word in C. Everything may happen. Probably won t format your disk, but may very well give a hacker a way to do it

21 Variables Where to declare? 1. Inside a block (C89 - block beginning), will be visible only in block. 2. Outside all blocks global - will be visible everywhere. int x=0; // global int main() { } int x=1; //local hides global { int x=2; //local hides outer scope //x is 2 } //x is 1 again! 21

22 Scopes 22 Code block defined with { and }. Only declarations inside current or outer scopes are visible. Declarations in inner scope hide declarations in outer scopes: Outmost scope (global) had no brackets. Keep in mind that a function is also a scope. int y=5,x=0; { int x=y; //x is 5 { int y; } } // x is 0

23 Second Program #include <stdio.h> int main() { int i; // declares i as an integer int j = 0; // declares j as an integer, // and initializes it to 0 // for( initial ; test condition ; update step ) for( i = 0; i < 10; ++i ) { j += i; // shorthand for j = j + i printf("%d %d %d\n", i, j, (i*(i+1))/2); } return 0; } 23

24 Running

25 Functions C allows to define functions Syntax: Return type int power( int a, int b ) { return 7; } Return statement Parameter declaration 25

26 Procedures Functions that return void void power( int a, int b ) { } return; Return w/o value (optional) 26

27 Example printing powers 27 #include <stdio.h> int power( int base, int n ) { int i, p; p = 1; for( i = 0; i < n; i++ ) { p = p * base; } return p; } int main() { int i; for( i = 0; i < 10; i++ ) { printf("%d %d %d\n", i, power(2,i), power(-3,i) ); } return 0; }

28 Functions Declaration void funca() {... } void funcb() { funca(); } void funcc() { funcb(); funca(); funcb(); } void funca() {... } void funcb() { funcc(); } void funcc() { funcb(); } Rule 1 : A function knows only functions which were declared above it. Error: funcc is not known yet. 28

29 Functions Declaration Amendment to Rule 1 : Use forward declarations. void funcc(int param); // or: void funcc(int); void funca() { 29 } void funcb() { funcc(7); } void funcc(int param) { }

30 Boolean variables non! int main() { int a = 5; while(1) { if(!(a-3)) { printf("** a=3 **\n"); break; } printf("a=%d\n",a--); } return 0; } 30

31 Building a program in C: Preprocessor, Compilation and Linkage 31

32 Building a program in C Preprocessor A text processor Commands start with # // copy & paste the file here #include <stdio.h> hello.c Preprocessor tmpxq.i (C code) stdio.h 32

33 Building a program in C Compiling Takes input C-code and produces machine code (object file) The object file does not contain all external references: It leaves names, such as printf, area, etc. as undefined references hello.c Preprocessor tmpxq.i (C code) Compiler 33 stdio.h hello.o (object file)

34 Linking Combines object file with external references into an fully executable file, with no unresolved references Main.c Main.o libc.a Main Preprocessor, Compiler Linker 34

35 Link errors Error 1 error LNK2019: unresolved external symbol _foo referenced in function _main c:\users\ofirpele\documents\visual studio 2012\Projects\ConsoleApplication5\Console Application5\ConsoleApplication5.obj ConsoleApplication5 35

36 36 The Preprocessor

37 Compilation in C hello.c Preprocessor tmpxq.i (C code) Compiler stdio.h hello.o (object file) 37

38 Preprocessor A single-pass program that: 1. Include header files 2. Expands macros 3. Control conditional compilation 4. Remove comments Outputs a code ready for the compiler to work on. 38

39 Preprocessor In linux with gcc, we can test what the preprocessor does: > gcc E hello.c will print the C code after running the preprocessing 39

40 #include directive #include "foo.h" Include the file foo.h, from current directory #include <stdio.h> Include the file stdio.h from the standard library directory (part of compiler installation) 40

41 Modules & Header files Square.h // interface of function int area... (int x1,int y1, int x2, int y2); Complex.c Main.c #include "Square.h" int main() { area (2,3, 5,6); } Square.c #include "Square.h" #include <math.h> // implementation int area (int x1,int y1,int x2, int y2) {... } 41

42 Do all files get compiled every time we build the program??? $ gcc c Square.c o Square.o $ gcc c Main.c o Main.o $ gcc Square.o Main.o o Main Square.c Main.c Square.o Main.o libc.a Main Preprocessor Compiler Linker 42

43 Header files Header file contain 1. Definition of data types 2. Declarations of functions & constants 3. That are shared by multiple modules. #include directive allows several modules to share the same set of definitions/declarations 43

44 #define directive #define FOO 1 int x = FOO; is equivalent to int x = 1; 44

45 #define with arguments #define SQUARE(x) x*x b = SQUARE (a); is the same as b = a*a; 45

46 #define cautions #define SQUARE (x) x*x b = SQUARE (a+1); c = SQUARE (a++); Is it what we intended? b = a+1*a+1; //Actually: b = 2*a+1; c = a++*a++; //Actually: c = a*a; a+=2; 46

47 #define cautions #define SQUARE (x) ((x)*(x)) b = SQUARE (a+1); c = SQUARE (a++); Is it what we intended? b = ((a+1)*((a+1)); //Now ok c = ((a++)*(a++)); //Did not help: c = a*a; a+=2; 47

48 #define #define directive should be used with caution! Alternative to macros: Constants enum { FOO = 1 }; or const int FOO = 1; Functions inline functions (C99,C++) 48

49 #define Multi-line: All preprocessor directive effect one line (not c statement). To insert a line-break, use \ : BAD: #define x (5 + 5) // x == 10! GOOD: #define x (5 + \ 5) // x == 10! 49

50 #if directive Allows to have conditional compilation #if defined(debug) // compiled only when DEBUG exists printf("x = %d\n", X); #endif 50

51 #ifndef for header safety Complex.h: struct Complex {... MyStuff.h: #include "Complex.h" Main.c: #include "MyStuff.h" #include "Complex.h" Error: Complex.h:1: redefinition of `struct Complex' 51

52 #ifndef header safety Complex.h (revised): #ifndef COMPLEX_H #define COMPLEX_H struct Complex {... #endif Main.c: #include "MyStuff.h" #include "Complex.h" // no error this time 52

53 #pragma once header safety Complex.h (revised): #pragma once struct Complex {... Main.c: #include "MyStuff.h" #include "Complex.h" // no error this time 53

54 Preprocessor summary Text processing program. Does not know c rules Operates before compilation, output passed to compiler. Can do copy and paste / cut #include paste the included file here Commonly included file (.h by convention) contains forward declarations #define copy the macro body, paste it where macro name appears - constants, simple "functions" #if condition not fulfilled, cut the code - Conditional compilation e.g. debugging code 54

55 Debug/Test mode vs Release mode 55

56 Debug/Test mode vs Release mode #include <assert.h> #define MAX_INTS 100 int main() { int ints[max_ints]; // i should be in bounds, but is it really? i = foo(<something complicated>); // safety assertions assert(i>=0); assert(i<max_ints); ints[i] = 0;... 56

57 Debug/Test mode vs Release mode #include <assert.h> #define MAX_INTS 100 int main() { int ints[max_ints]; // i should be in bounds, but is it really? i = foo(<something complicated>); // safety assertions assert(i>=0); assert(i<max_ints); ints[i] = 0;... 57

58 #define NDEBUG #include <assert.h> #define MAX_INTS 100 int main() { int ints[max_ints]; // i should be in bounds, but is it really? i = foo(<something complicated>); // safety assertions assert(i>=0); assert(i<max_ints); ints[i] = 0;... 58

59 assert // bad, foo() will not be called // if the compiler removes the assert() // if NDEBUG is defined assert(foo() == 0); // good int errcode = foo(); assert(errcode == 0); 59

60 assert Use for: Catching bugs Don't use for: checking malloc, user input,... 60

61 assert.h #include <assert.h> // Sqrt(x) - compute square root of x // Assumption: x non-negative double sqrt(double x ) { assert( x >= 0 ); // aborts if x < 0 61

62 assert.h // procedure that actually prints error message void assert(char* file,int line,char* test); #ifdef NDEBUG #define assert(e) ((void)0) #else #define assert(e) \ ((e)? (void)0 : \ assert( FILE, LINE, #e)) #endif 62

63 assert.h Important coding practice Declare implicit assumptions Sanity checks in code Check for violations during debugging/testing 63

64 64 Compilation

65 Compilation Translate the c code to machine code. Every machine architecture has a different code. Need separate compilation. x86 : older but still common PCs 32-bit (Intel/AMD) x64 : newer PCs 64-bit )Intel/AMD( ARM : (many versions) Phones, tablets (ARM) Translated code is an executable the OS can load it to the machine memory, and the let the CPU do whatever you wrote in the code. 65

66 Assembly int y=2; int mult(int x) { return x*y; mov eax,dword ptr [x] imul eax,dword ptr [ h] } int main() { return mult(5); push 5 call 11711A4h } 66 Only part of the assembly code is shown here. Actually each command has binary code, and the sequence of these code is the object file.

67 Compiling warnings

68 Compiling warnings Add -Wall flag to catch things like this: if (i=3) { //bug, we meant i==3 } 68

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

#include <stdio.h> int main() { printf (hello class\n); return 0; } C #include int main() printf ("hello class\n"); return 0; Working environment Linux, gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul

More information

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Reliable C++ development - session 1: From C to C++ (and some C++ features) Reliable C++ development - session 1: From C to C++ (and some C++ features) Thibault CHOLEZ - thibault.cholez@loria.fr TELECOM Nancy - Université de Lorraine LORIA - INRIA Nancy Grand-Est From Nicolas

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 6: Introduction to C (pronobis@kth.se) Overview Overview Lecture 6: Introduction to C Roots of C Getting started with C Closer look at Hello World Programming Environment Schedule Last time (and

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (yaseminb@kth.se) Overview Overview Roots of C Getting started with C Closer look at Hello World Programming Environment Discussion Basic Datatypes and printf Schedule Introduction to C - main part of

More information

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32

More information

CS 113: Introduction to

CS 113: Introduction to CS 113: Introduction to Course information MWF 12:20-1:10pm 1/21-2/15, 306 Hollister Hall Add/drop deadline: 1/28 C Instructor: David Crandall See website for office hours and contact information Prerequisites

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

More information

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information

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

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

The C standard library

The C standard library C introduction The C standard library The C standard library 1 / 12 Contents Do not reinvent the wheel Useful headers Man page The C standard library 2 / 12 The Hitchhiker s Guide to the standard library

More information

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016 Oregon State University School of Electrical Engineering and Computer Science CS 261 Recitation 2 Spring 2016 Outline Programming in C o Headers o Structures o Preprocessor o Pointers Programming Assignment

More information

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

Programming in C. What is C?... What is C? C Programming in C UVic SEng 265 Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version

More information

Programming in C UVic SEng 265

Programming in C UVic SEng 265 Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier,

More information

Programming in C week 1 meeting Tiina Niklander

Programming in C week 1 meeting Tiina Niklander Programming in C week 1 meeting 2.9.2015 Tiina Niklander Faculty of Science Department of Computer Science 3.9.2015 1 Course structure Based on C programming course in Aalto, but with some exercises created

More information

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

C Introduction. Comparison w/ Java, Memory Model, and Pointers CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language

More information

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

Programming in C. What is C?... What is C? Programming in C UVic SEng 265 C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

Programming in C First meeting

Programming in C First meeting Programming in C First meeting 8.9.2016 Tiina Niklander Faculty of Science Department of Computer Science www.cs.helsinki.fi 8.9.2016 1 Course structure Weekly exercise deadline on Wednesday, lectures

More information

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above

More information

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSci 4061 Introduction to Operating Systems. Programs in C/Unix CSci 4061 Introduction to Operating Systems Programs in C/Unix Today Basic C programming Follow on to recitation Structure of a C program A C program consists of a collection of C functions, structs, arrays,

More information

C Programming Review CSC 4320/6320

C Programming Review CSC 4320/6320 C Programming Review CSC 4320/6320 Overview Introduction C program Structure Keywords & C Types Input & Output Arrays Functions Pointers Structures LinkedList Dynamic Memory Allocation Macro Compile &

More information

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE HEADER FILE A header (.h,.hpp,...) file contains Class definitions ( class X {... }; ) Inline function definitions ( inline int get_x() {... } ) Function declarations ( void help(); ) Object declarations

More information

1 You seek performance 3

1 You seek performance 3 Why? 2 1 You seek performance 3 1 You seek performance zero-overhead principle 4 2 You seek to interface directly with hardware 5 3 That s kinda it 6 C a nice way to avoid writing assembly language directly

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 2: Hello World! Cristina Nita-Rotaru Lecture 2/ Fall 2013 1 Introducing C High-level programming language Developed between 1969 and 1973 by Dennis Ritchie at the Bell Labs

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

CS Basics 15) Compiling a C prog.

CS Basics 15) Compiling a C prog. CS Basics 15) Compiling a C prog. Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 1 Compiling a C program Example of a small

More information

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

just a ((somewhat) safer) dialect.

just a ((somewhat) safer) dialect. Intro_to_C Page 1 Intro to C Tuesday, September 07, 2004 5:30 PM C was developed specifically for writing operating systems Low level of abstraction. "Just above machine language." Direct access to the

More information

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2 Compiling a C program CS Basics 15) Compiling a C prog. Emmanuel Benoist Fall Term 2016-17 Example of a small program Makefile Define Variables Compilation options Conclusion Berner Fachhochschule Haute

More information

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017 Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles CS449 Fall 2017 Multi-file Development Multi-file Development Why break code into multiple source files? Parallel development

More information

Have examined process Creating program Have developed program Written in C Source code

Have examined process Creating program Have developed program Written in C Source code Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives

More information

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c! Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Hello Word! ~/ctest/ In addition to syntax you need to learn: the Tools the Libraries. And the Documentation. Maria Hybinette,

More information

The C Preprocessor (and more)!

The C Preprocessor (and more)! The C Preprocessor (and more)! Peter Kristensen 2012-11-19 Peter Kristensen The C Preprocessor (and more)! Outline 1 C Pre Processor Compiler Assembler Linker Frontend 2 Simple directives Headers Macros

More information

6.s096. Introduction to C and C++

6.s096. Introduction to C and C++ 6.s096 Introduction to C and C++ I know C and C++ Java Python C C++ Java Python C C++ Idioms & Best Practices Tom Lieber Kyle Murray Frank Li Damon Doucet Leonid Grinberg Evan Wang Chen Zhao web.mit.edu/6.s096/

More information

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

CS133 C Programming. Instructor: Jialiang Lu   Office: Information Center 703 CS133 C Programming Instructor: Jialiang Lu Email: jialiang.lu@sjtu.edu.cn Office: Information Center 703 1 Course Information: Course Page: http://wirelesslab.sjtu.edu.cn/~jlu/teaching/cp2014/ Assignments

More information

Advanced Pointer & Data Storage

Advanced Pointer & Data Storage 18, 19: storage classes 14: Preprocessor & Polymorphism in C) 15 : command line building 26 : stdarg Advanced Pointer & Data Storage (for ch. 14, 15 18, 19, 26) Contents Preprocessor & Polymorphism in

More information

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems Programs CSCI 4061 Introduction to Operating Systems C Program Structure Libraries and header files Compiling and building programs Executing and debugging Instructor: Abhishek Chandra Assume familiarity

More information

CSE 303 Lecture 8. Intro to C programming

CSE 303 Lecture 8. Intro to C programming CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, 2.2-2.4, 2.6, 3.1, 5.1, 7.1-7.2, 7.5.1-7.5.4, 7.6-7.9, Ch. 8; Programming in C Ch. 1-6 slides created by Marty Stepp http://www.cs.washington.edu/303/

More information

EE 209: Programming Structures for Electrical Engineering

EE 209: Programming Structures for Electrical Engineering EE 209: Programming Structures for Electrical Engineering 1 Goals for Today s Class Course overview Introductions Course goals Resources Grading Policies Getting started with C C programming language overview

More information

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/13 Scope Variables and functions are visible from the point they are defined until the end of the source

More information

Programming in C First meeting Tiina Niklander

Programming in C First meeting Tiina Niklander Programming in C First meeting 5.9.2016 Tiina Niklander Faculty of Science Department of Computer Science www.cs.helsinki.fi 5.9.2018 1 Learning goal objectives Language structures, data structures, modules,

More information

ELEC 377 C Programming Tutorial. ELEC Operating Systems

ELEC 377 C Programming Tutorial. ELEC Operating Systems ELE 377 Programming Tutorial Outline! Short Introduction! History & Memory Model of! ommon Errors I have seen over the years! Work through a linked list example on the board! - uses everything I talk about

More information

Summer May 11, 2010

Summer May 11, 2010 Summer 2010 Department of Computer Science and Engineering York University Toronto May 11, 2010 1 / 40 What we did last time Overview of C to the language Program structure Types in C Operators in C IO

More information

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

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang Topics History of Programming Languages Compilation Process Anatomy of C CMSC 104 Coding Standards Machine Code In the beginning,

More information

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva Software Project Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva Emails: (canetti/benriva)@post.tau.ac.il nathan.manor@gmail.com gideon@mta.ac.il http://www.cs.tau.ac.il/~roded/courses/soft-project10.html

More information

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers Introduction to C Geared toward programmers Sean Ogden Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Ayush Dubey Cornell CS 4411, August 30, 2013 Administrative Information

More information

C introduction: part 1

C introduction: part 1 What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation

More information

2 Compiling a C program

2 Compiling a C program 2 Compiling a C program This chapter describes how to compile C programs using gcc. Programs can be compiled from a single source file or from multiple source files, and may use system libraries and header

More information

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers Introduction to C Geared toward programmers Ayush Dubey Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Cornell CS 4411, August 31, 2012 Administrative Information Outline

More information

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp) A software view User Interface Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll Materials from CMU and Dr. Butler How it works hello.c #include int main() { printf( hello, world\n

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMING LANGUAGES A programming language is a formal language that specifies a set of instructions that can be used to produce various kinds of output. Programming languages

More information

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers Introduction to C Geared toward programmers Zhiyuan Teo Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Cornell CS 4411, August 26, 2011 1 Administrative Information 2 Why C? 3

More information

Fall, $ cat welcome.c #include <stdio.h>

Fall, $ cat welcome.c #include <stdio.h> $ cat welcome.c #include int main(int argc, char *argv[]) { printf( COS 217\n ); printf( Introduction to Programming Systems\n\n ); } printf( Fall, 2018\n ); return 0; $ gcc217 welcome.c o welcome

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Course Information and Introduction

Course Information and Introduction August 22, 2017 Course Information 1 Instructors : Email : arash.rafiey@indstate.edu Office : Root Hall A-127 Office Hours : Tuesdays 11:30 pm 12:30 pm. Root Hall, A127. 2 Course Home Page : http://cs.indstate.edu/~arash/cs256.html

More information

The C Programming Language

The C Programming Language The C Programming Language What is C? "High-level" programming language developed by Dennis Ritchie with Brian Kernighan Bell Labs, New Jersey, 1970s Developed in conjunction with Unix Intended to provide

More information

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember... Types Page 1 "ode to C" Monday, September 18, 2006 4:09 PM 0x0d2C ------ May your signals all trap May your references be bounded All memory aligned Floats to ints round remember... Non -zero is true ++

More information

Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types Lecture 03 Bits, Bytes and Data Types Computer Languages A computer language is a language that is used to communicate with a machine. Like all languages, computer languages have syntax (form) and semantics

More information

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) CSE 374 Programming Concepts & Tools Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) Linked lists, trees, and friends Very, very common data structures

More information

COMP1917: Computing 1 1. Introduction

COMP1917: Computing 1 1. Introduction COMP1917: Computing 1 1. Introduction Reading: Moffat, Chapter 1. COMP1917 12s2 Introduction 1 Course Web Site http://www.cse.unsw.edu.au/~cs1917/12s2 Please check this Web Site regularly for updated information,

More information

Page 1. Agenda. Programming Languages. C Compilation Process

Page 1. Agenda. Programming Languages. C Compilation Process EE 472 Embedded Systems Dr. Shwetak Patel Assistant Professor Computer Science & Engineering Electrical Engineering Agenda Announcements C programming intro + pointers Shwetak N. Patel - EE 472 2 Programming

More information

Programming. Projects with Multiple Files

Programming. Projects with Multiple Files Programming Projects with Multiple Files Summary } GCC } Multiple source files 2 Source Code with Multiple Files } Make multiple, separate source code files which can be combined into one executable }

More information

Computer Labs: Debugging

Computer Labs: Debugging Computer Labs: Debugging 2 o MIEIC Pedro F. Souto (pfs@fe.up.pt) October 29, 2012 Bugs and Debugging Problem To err is human This is specially true when the human is a programmer :( Solution There is none.

More information

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

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

More information

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

G52CPP C++ Programming Lecture 6. Dr Jason Atkin G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1 Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2 Visibility is different from lifetime Just

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011 Two s Complement Review CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part I) Instructor: Michael Greenbaum http://inst.eecs.berkeley.edu/~cs61c/su11 Suppose we had

More information

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS) COMP1917 14s2 Introduction 1 COMP1917: Computing 1 1. Introduction Reading: Moffat, Chapter 1. Course Web Site http://www.cse.unsw.edu.au/~cs1917/14s2 Please check this Web Site regularly for updated information,

More information

HW1 due Monday by 9:30am Assignment online, submission details to come

HW1 due Monday by 9:30am Assignment online, submission details to come inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 04: Introduction to C Readings: Chapter 1.5-1.7 What is C? C is a general-purpose, structured

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

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

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers Introduction to C Geared toward programmers Robert Escriva Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Cornell CS 4411, August 30, 2010 1 Why C? 2 A Quick Example 3 Programmer s Responsibilities

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 MODULARIZATION MODULAR PROGRAMMING Modularization is a method to organize large programs in smaller parts, i.e. the modules. Every module has a well defined interface toward

More information

QUIZ. Source:

QUIZ. Source: QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That

More information

ANITA S SUPER AWESOME RECITATION SLIDES

ANITA S SUPER AWESOME RECITATION SLIDES ANITA S SUPER AWESOME RECITATION SLIDES 15/18-213: Introduction to Computer Systems Dynamic Memory Allocation Anita Zhang, Section M UPDATES Cache Lab style points released Don t fret too much Shell Lab

More information

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics Stuff Last Time Homework due next week Lab due two weeks from today Questions? Interrupts Inline assembly Intrinsics Today Safety-Critical Systems MISRA-C Subset of C language for critical systems System

More information

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 15: Files and Preprocessor Directives/Pointers 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained

More information

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

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems Deep C Multifile projects Getting it running Data types Typecasting Memory management Pointers Fabián E. Bustamante, Fall 2004 Multifile Projects Give your project a structure Modularized design Reuse

More information

Final C Details. CSE 333 Autumn 2018

Final C Details. CSE 333 Autumn 2018 Final C Details CSE 333 Autumn 2018 Instructor: Hal Perkins Teaching Assistants: Tarkan Al-Kazily Renshu Gu Trais McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu Administriia Today:

More information

COMP1917: Computing 1 1. Introduction

COMP1917: Computing 1 1. Introduction COMP1917: Computing 1 1. Introduction Reading: Moffat, Chapter 1. COMP1917 15s2 Introduction 1 Course Web Site http://www.cse.unsw.edu.au/~cs1917/15s2 Please check this Web Site regularly for updated information,

More information

EE 209: Programming Structures for Electrical Engineering. (Many Slides Borrowed from Princeton COS 217)

EE 209: Programming Structures for Electrical Engineering. (Many Slides Borrowed from Princeton COS 217) EE 209: Programming Structures for Electrical Engineering (Many Slides Borrowed from Princeton COS 217) 1 Goals for Today s Class Course overview Introduction Course goals Resources Grading Policies Getting

More information

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2 CS 220: Introduction to Parallel Computing Beginning C Lecture 2 Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 2 Today s Schedule

More information

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead. Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files The Make Utility Independent compilation Large programs are difficult to maintain Problem solved by breaking the program into separate files Different functions placed in different files The main function

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

The University of Nottingham

The University of Nottingham The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, AUTUMN SEMESTER 2008 2009 C/C++ for Java Programmers Time allowed TWO hours Candidates may complete the front cover of their answer

More information

Undefined Behaviour in C

Undefined Behaviour in C Undefined Behaviour in C Report Field of work: Scientific Computing Field: Computer Science Faculty for Mathematics, Computer Science and Natural Sciences University of Hamburg Presented by: Dennis Sobczak

More information

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang Macros and Preprocessor CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang Previously Operations on Linked list (Create and Insert) Agenda Linked List (More insert, lookup and delete) Preprocessor Linked List

More information