Welcome to C and Computer Structure #include int main() { printf ("hello class\n"); return 0; }

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Introduction to C Programming CE Lecture 1 Introduction to C.
Programming The Development Environment and Your First C Program.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
. Welcome to PLAB. Course Staff Teacher:  Nir Friedman Teaching Assistants:  Yoseph Barash  Liad Blumrosen  Michael Okun.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
C programming Language and Data Structure For DIT Students.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Computer Science 210 Computer Organization Introduction to C.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANCI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
Basics of “C” Programming
H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It.
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Monday, Jan 6, 2003Kate Gregory with material from Deitel and Deitel CO 204 Object Oriented Programming 2003 Trent University Kate Gregory.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
1 Building a program in C: Preprocessor, Compilation and Linkage.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Introduction to C Programming I Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C #include int main() { printf ("hello class\n"); return 0; }
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
Computer Science 210 Computer Organization
C #include <stdio.h> int main() { printf ("hello class\n");
C Programming Hardik H. Maheta.
C #include <stdio.h> int main() { printf ("hello class\n");
C Language By Sra Sontisirikit
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
History of ‘C’ Root of the morden language is ALGOL It’s first
Introduction to C Topics Compilation Using the gcc Compiler
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to C Topics Compilation Using the gcc Compiler
C #include <stdio.h> int main() { printf ("hello class\n");
Introduction to C Programming Language
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANSI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Computer Science 210 Computer Organization
Chapter 11 Introduction to Programming in C
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Programming Fundamentals Lecture #3 Overview of Computer Programming
Your first C and C++ programs
C Programming Getting started Variables Basic C operators Conditionals
C programming Language
Introduction to C Topics Compilation Using the gcc Compiler
C – Programming Language
An Overview of C.
Introduction to C Topics Compilation Using the gcc Compiler
Presentation transcript:

Welcome to C and Computer Structure #include int main() { printf ("hello class\n"); return 0; }

2 Course Staff Teacher: Ofir Pele Teaching assistants: Mark Leshansky Luba Kuznetsov

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

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

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

6 Course Grading Exercise 1: 15% Exercise 2: 25% Exercise 3: 10% Final project: 50% כל העבודה ביחידים (אסור זוגות, שלשות וכו) פרוייקט הסיום ייבדק גם בראיון אישי עם המרצה

Late Submission Your T.A. decision Don’t me about it !

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

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 Also in tirgul

History ’ s – Development of UNIX. (Richie+Kernigham – Bell labs) 70 ’ s – Development of UNIX. (Richie+Kernigham – Bell labs) 80 ’ s – Large efficient code. (Stroustrup – 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 EfficientSecure and Safe Welcome to C Easy to avoid bugs Easy to Debug C++CJava

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

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

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

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

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

Basic 17 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; } ;

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

Statements - loops 19 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 do //statement or block while (condition)

Statically typed – each variable has a type. Declare by:. int x; int x,y; Optionally initialize (otherwise undefined!) int x=0; Variables 20 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…

Variables 21 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! }

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

Second Program 23 #include 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

Running…

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

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

Example – printing powers 27 #include 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; } 27

Functions Declaration 28 “Rule 1”: A function “knows” only functions which were declared above it. void funcA() {... } void funcB() { funcA(); } void funcC() { funcB(); funcA(); funcB(); } void funcA() {... } void funcB() { funcC(); } void funcC() { funcB(); } Error: funcC is not known yet. 28

Functions Declaration void funcC(int param); // or: void funcC(int); void funcA() { } void funcB() { funcC(7); } void funcC(int param) { } Amendment to “Rule 1” : Use forward declarations. 29

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

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

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

Building a program in C – Compiling 33 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 Compiler stdio.h tmpXQ.i (C code) hello.o (object file)

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

Link errors 35 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

36 The Preprocessor

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

Preprocessor 38 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.

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

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

Modules & Header files 41 Complex.c int area (int x1,int y1, int x2, int y2);... square.h #include "square.h" #include // implementation int area (int x1,int y1,int x2, int y2) {... } square.c #include "square.h" int main() { area (2,3, 5,6); } MyProg.c

Header files 42 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