C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

Size: px
Start display at page:

Download "C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature"

Transcription

1 C-programming Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature I Bilting, Skansholm: Vägen till C, in Swedish, I Kernighan, Ritchie: The C Programming Language, Second edition, I Oualline: Practical C programming On the net you can nd several tutorials, eg ( )

2 What is C? I An imperative programming language I Statements (commands) that are executed in sequence I Data (variables) that are manipulated I Functions with possible side eects I Iterations (loops) I Low level but platform independent (hmm... ) I memory addresses, address arithmetic I bit manipulation I... I Small, primitive I Similarities with Java, Pascal and Fortran ( )

3 History I Originally developed by Dennis Ritchie 1972 for implementation of operating systems and other system software I Unix was mostly written in C I ANSI-standard from 1989 (ANSI-C) I Revised standard from 1999 (C99) I Inspired languages like C++ and Java ( )

4 Some features I C uses static typing: all variables has a type that cannot be changed I C uses weak typing: some type conversions are done automatically and uncontrolled and inconsistent conversions are allowed I In C you may works directly with memory addresses I Explicit manual memory handling I C is compiled to machine code while Python and Java are interpreted I There is a minimum of runtime checks while running your program. (uncontrolled pointers, limits of an array, undened variables... ) ( )

5 When, for what and why is C used I To be close to the hardware (parts of OS, drivers,... ) I Embedded system I Time- and/or memory critical applications I When there is no other choice, a naked system without OS I Programming of parallel systems (Typical with C, C++ or Fortran). (OpenMP is only available in these languages) ( )

6 Example A program that prints Hello, world /* hello.c A first, classic example of a C-program */ #include <stdio.h> int main() { printf("hello, world\n"); return 0; ( )

7 Example: iteration, variables /* squares.c Program that writes a table over squares of the numbers 1, 2, */ #include <stdio.h> int main() { int i = 1; while ( i<=10) { printf( "%d \t %d\n", i, i*i ); i = i + 1; return 0; ( )

8 Compiling bellatrix$ ls squares.c bellatrix$ cat squares.c /* squares.c Program that writes a table over squares of the numbers 1, 2, */ #include <stdio.h> int main() { int i = 1; while ( i<=10 ) { printf( "%d \t %d \n", i, i*i); i = i + 1; return 0; bellatrix$ gcc squares.c bellatrix$ ls a.out* squares.c ( )

9 Execution bellatrix$ a.out bellatrix$ ( )

10 Example: several functions /* factorial.c */ Program that tabulates the faculty function #include <stdio.h> int factorial(int n) { int result = 1; while ( n > 0 ) { result = result*n; n = n - 1; return result; int main() { int i = 0; while ( i<=15 ) { printf( "%2d %12d \n", i, factorial(i)); i = i + 1; return 0; ( )

11 Example: recursion, conditional statements /* factorialrec.c Program that tabulates the faculty function Recursive version. */ #include <stdio.h> int factorial(int n) { if ( n<=0 ) return 1; else return n*factorial(n-1); int main() { int i = 0; while ( i<=15 ) { printf( "%2d %12d \n", i, factorial(i)); i = i + 1; return 0; ( )

12 Example: Read and write characters /* cat1.c Copy standard input to standard output */ #include <stdio.h> int main() { int c; c = getchar(); while ( c!= EOF ) { putchar(c); c = getchar(); return 0; ( )

13 Example: Read and write characters version 2 /* cat2.c Copy standard input to standard output, version 2 */ #include <stdio.h> int main() { int c; while ( (c = getchar())!= EOF ) putchar(c); return 0; ( )

14 Example: A program that counts characters and lines #include <stdio.h> int main() { int c; int nchars = 0, nlines = 0; while ( (c = getchar())!= EOF ) { ++nchars; if ( c == '\n' ) { ++nlines; printf( "Characters: %d \nlines: %d \n", nchars, nlines ); return 0; ( )

15 Conclusions I A C-program consists of one or more function that are stored in one (or more) le I Comments are delimited with /* and */. In C99 // can be used for oneline comments, (as in C++ and Java) I #include <stdio.h> to use library functions for I/O (printf, getchar, putchar, EOF) I A C-function has a return type, a name, a parameter list and a function body I The function body can hold declarations of variables and statements I Semicolon (;) is used to terminate declarations and statements. I Vaiables can also be declared outside a function. They then become global in some sense. ( )

16 Conclusions continued I Each le must be compiled before the program can be executed I The execution starts in the function named main I Variables must be declared with type and name I Variables are assigned values (of the proper type) with assignments I Statements in a function are executed in sequence I Statements for selections: if (and switch) I Statements for iterations: while (and for and do-while). ( )

17 Exercises 1. Write a program that reads characters from standard input, and counts the number of sentences if the text. A sentence is terminated with a period, an exclamation mark or a question mark. 2. Write a C-function that computes the harmonic sum 1 + 1=2 + 1=3 + 1=4::: + 1=n What parameters and what return type should the function have? 3. Write a C-program that tabulates the above sum for n = 1; 2; : : : ; Write a C-program that computes how many terms are required to get a sum that is greater than =2 + 1=3 + 1=4::: + 1=n ( )

18 The if-statement Two variants: and if ( u ) s where if ( u ) s 1 else s 2 I u is an expression of any type I each of s, s 1, s 2 is a statement The expression u is interpreted as false if its value is 0, 0.0, or NULL (for pointers) otherwise as true. If more that one statement is required for any of s, s 1, s 2, you build a compund statement using { and ( )

19 The for statement An iteration statement with the syntax: for ( u 1 ; u 2 ; u 3 ) s You get the same functionality using the while-statements: u 1 ; while ( u 2 ) { s; u 1 ; Common idiom to iterate something a number of times for ( i = 1; i<10; i++ ) { s; ( )

20 Operators (incomplete) binary aritmetical : + - * / unary aritmetical : assignment : = += -= *= /= relations : == < > <= >=!= logical : &&! ( )

21 Data types I void I scalar types I arithmetic types I integer types: char, short int, long int, long long int (optional, unsigned) I oating point types: float, double, long double I pointers I arrays (not really a type, rather a pointer) I compound types struct Note: No type for logical values (true, false)! ( )

22 Integer types Common sizes and numerical ranges: long int to short int to unsigned long int 4 0 to unsigned short int 2 0 to signed char to 127 unsigned char 1 0 to 255 The type int is usually equivalent to long or short int (implementation dependent) The type char is usually equivalent to unsigned char ( )

23 Constants Integer constants decimal form 3, 8, -255 octal form 003, 010, Floating point constants Notation with decimal point and/or exponent -1.5,.26, 100., e10, 0.5e2, 1e-10 ( )

24 The type char The type char is an integer type and character constants are a way to represent both characters and small integer numbers. Thus you can do arithmetic on characters: char toupper(char c) { /* If c is in lower case, it is converted to upper case */ if ( c>='a' && c <='z' ) return c + 'A' - 'a'; else return c; ( )

25 The library ctype.h This library contains representation independent functions to classify characters: #include <ctype.h> int isalpha(int c); int isdigit(int c); int isalnum(int c); int isspace(int c); int isupper(int c); int islower(int c); int isprint(int c); int iscntrl(int c); int tolower(int c); int toupper(int c); ( )

26 Example: Print an ascii-table /* A program that prints a table of character codes */ int main() { char c; for ( c = ' '; c<127; c++ ) printf( "%d \t %c \n", c, c ); return 0; (In the ascii code, space is the rst printable and (126) is the last printable) ( )

27 Exercises 1. Write a program that reads characters from standard input and that counts the number of words. A word is dened as a sequence of characters. 2. Modify the program so that it also prints the length of the longest word. 3. Write a function int isequal(char c1, char c2) that returns 1 if the characters c1 and c2 are equal, otherwise 0. If the characters are letters, they should be treated as equal regardless of case (upper/lower). 4. Write a program that reads a line from standard input and that prints the line translated to rövarspråket. In the translation, a consonant like x is replace with xox while vowels are left unchanged. For example: The text Don't panic becomes Dodonon'tot popanonicoc. 5. Write a function that reads a line of arbitrary length from standard input and that prints the line backwards, i. e. with the last character rst. The function need not use arrays or lists. ( )

28 All variables have to be declared Exempel: int i, j, k; float x, y; char c; short int p, q; unsigned short int r; unsigned char ch; int start = 0, stop = 10; char c=getchar(); The declarations must be placed at the top in a block. A block is dened by a { - pair (The C99-standard allows a more free placement of declarations) ( )

29 Type conversions I Operands with dierent types are automatically converterted from narrow types to wider types, eg short to long. I Assignments from wide types to narrower types can give warnings but are not illegal. I Explicit type conversions with casts : (type) expression Exempel: y = power( (float) i, n ); ( )

30 Formatted output printf( format string, value, value,... ) Some format specications: %d int %ld long int %u unsigned int %lu unsigned long int %o int octal form %x int hexadecimal form %c char %f double decimalform %e double exponential form %g double decimal- or exponential form %s character string ( )

31 Formatted output continued The specications can have attributes to specify eld width, number of decimals, justication etc. /* formatexample.c - Demonstrates the use of format codes */ #include <stdio.h> int main() { float x; for (x=0; x<=10.; x++) { printf(" %2.0f %8.4f %12.4e\n", x, sin(x), exp(x)); /* Output: e e e e e e e e e e e+04 ( )

32 Input of numbers The function scanf for formatted input Example: #include <stdio.h> int main() { float x; int n; printf("give x and n: "); scanf("%f %d", &x, &n ); printf( "%f raised to %d is %f", x, n, power(x,n) ); return 0; ( )

33 Some facts about functions I All functions are declared at the same level, i. e. a function cannot contain functions. I A function has zero or more parametrars of arbitrary type I At call time, the parameters must match in order, number and type. Automatical type conversion may occur if some cases, e. g. int -> double. I Parameter transmission uses call by value I A function can be of the type void if it has no return value. ( )

34 I Functions can return any scalar type or structure(struct) but not arrrays. I Functions returns a value of the type int if no explicit type is specied. I Local variables dies at return (if not static) I To use a function, if has to be known. To be known it must be introduced either through a denition or a declaration: typ name( parameter list ); typ name( void ); ( )

35 Exercises 1. The series is called Fibonaccis numbers. 0; 1; 1; 2; 3; 5; 8; 13 : : : 1.1 Write a program that reads a number n, calculates and prints the n rst Fibonaccinumbers. 1.2 Write a program that reads a number m and calcutes how many Fibonacci numbers that are less than or equal to m. 1X 2. The exponential function e x can be approximated with the ininite series i=0 x i 2 i! = 1 + x 1 + x 2 + x 6 + x 24 + Write a function double exp( double x) that calculates and returns (an approximation) to e x using the above formula. 3. Write a recursive function void printb(int x, int b) that prints x in the base b. For simplicity, assume that b <= ( )

36 Example: prime number control Problem: Write a program that reads a sequence of positive integers, and for each of these, tests whether it is a prime number or not. int isprime(int n) { int answer = 1; int i; for (i = 2; i<n; i++) { if ( n%i == 0 ) { answer = 0; return answer; I A new operator: % ( )

37 Better: int isprime(int n) { /* Precondition: n is an integer >= 0 * Returns: 1 if n is a prime number else 0 */ int answer = 1; int i; for (i = 2; i <= sqrt(n) && answer; i++) { if ( n%i == 0 ) { answer = 0; return answer; I A new operator: && I The function sqrt I 0 is interpreted as false, everything else as true ( )

38 The program: /* checkprimes.c Reads a sequence of integer numbers and tests if these are a prime number or not. Terminates when the number 0 is read. */ #include <stdio.h> #include <math.h> int isprime(int n) {... // As above int main() { int number=1; // Number to check while (number!=0) { printf("number to test: "); scanf("%d", &number); if (number!=0) { if (isprime(number)) { printf("%d is a prime number\n", number); else { printf("%d is not a prime number\n", number); return 0; ( )

39 Execution: kursa$ gcc -o checkprimes checkprimes.c Undefined first referenced symbol in file sqrt /var/tmp//cczwizdu.o ld: fatal: Symbol referencing errors. No output written to checkprimes collect2: ld returned 1 exit status kursa$ gcc -o checkprimes -lm checkprimes.c kursa$ checkprimes Number to test: 2 2 is a prime number Number to test: 4 4 is not a prime number Number to test: is not a prime number Number to test: is a prime number Number to test: is not a prime number Number to test: -4-4 is a prime number Number to test: 0 kursa$ ( )

SWEN-250 Personal SE. Introduction to C

SWEN-250 Personal SE. Introduction to C SWEN-250 Personal SE Introduction to C A Bit of History Developed in the early to mid 70s Dennis Ritchie as a systems programming language. Adopted by Ken Thompson to write Unix on a the PDP-11. At the

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

THE FUNDAMENTAL DATA TYPES

THE FUNDAMENTAL DATA TYPES THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

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

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

More information

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case Overview of Today s Lecture Lecture 2: Character Input/Output in C Prof. David August COS 217 http://www.cs.princeton.edu/courses/archive/fall07/cos217/ Goals of the lecture o Important C constructs Program

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

Programming refresher and intro to C programming

Programming refresher and intro to C programming Applied mechatronics Programming refresher and intro to C programming Sven Gestegård Robertz sven.robertz@cs.lth.se Department of Computer Science, Lund University 2018 Outline 1 C programming intro 2

More information

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts Strings in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch,

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

Computers Programming Course 5. Iulian Năstac

Computers Programming Course 5. Iulian Năstac Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction

More information

Chapter 8: Character & String. In this chapter, you ll learn about;

Chapter 8: Character & String. In this chapter, you ll learn about; Chapter 8: Character & String Principles of Programming In this chapter, you ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character

More information

Strings. Steven R. Bagley

Strings. Steven R. Bagley Strings Steven R. Bagley Recap Programs are a series of statements Defined in functions Functions, loops and conditionals can alter program flow Data stored in variables or arrays Or pointed at by pointers

More information

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size] (November 10, 2009 2.1 ) Arrays An array is a collection of several elements of the same type. An array variable is declared as type array name[size] I The elements are numbered as 0, 1, 2... size-1 I

More information

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme] Programming in C and Data Structures [15PCD13/23] 1 PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme] Course objectives: The objectives of this course is to make students

More information

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23. Subject code - CCP01 Chapt Chapter 1 INTRODUCTION TO C 1. A group of software developed for certain purpose are referred as ---- a. Program b. Variable c. Software d. Data 2. Software is classified into

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

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 Control Structures for C CS Basics 10) C Control Structures Emmanuel Benoist Fall Term 2016-17 Data Input and Output Single character In and Output Writing output data Control Statements Branching Looping

More information

Introduction to string

Introduction to string 1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ Types, Variables, Expressions and Statements Neel Krishnaswami and Alan Mycroft Course Structure Basics of C: Types, variables, expressions and statements Functions, compilation

More information

Programming in C Quick Start! Biostatistics 615 Lecture 4

Programming in C Quick Start! Biostatistics 615 Lecture 4 Programming in C Quick Start! Biostatistics 615 Lecture 4 Last Lecture Analysis of Algorithms Empirical Analysis Mathematical Analysis Big-Oh notation Today Basics of programming in C Syntax of C programs

More information

Data Type Fall 2014 Jinkyu Jeong

Data Type Fall 2014 Jinkyu Jeong Data Type Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Syntax Rules Recap. keywords break double if sizeof void case else int static... Identifiers not#me scanf 123th printf _id so_am_i gedd007 Constants

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

More information

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019 CSCI 2132: Software Development Introduction to C Norbert Zeh Faculty of Computer Science Dalhousie University Winter 2019 The C Programming Language Originally invented for writing OS and other system

More information

Continued from previous lecture

Continued from previous lecture The Design of C: A Rational Reconstruction: Part 2 Jennifer Rexford Continued from previous lecture 2 Agenda Data Types Statements What kinds of operators should C have? Should handle typical operations

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #8 Feb 27 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline More c Preprocessor Bitwise operations Character handling Math/random Review for midterm Reading: k&r ch

More information

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

Introduction to C Language

Introduction to C Language Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,

More information

Structure of this course. C and C++ Past Exam Questions. Text books

Structure of this course. C and C++ Past Exam Questions. Text books Structure of this course C and C++ 1. Types Variables Expressions & Statements Alastair R. Beresford University of Cambridge Lent Term 2008 Programming in C: types, variables, expressions & statements

More information

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga C-Programming CSC209: Software Tools and Systems Programming Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Adapted from Dan Zingaro s 2015 slides. Week 2.0 1 / 19 What

More information

The Design of C: A Rational Reconstruction: Part 2

The Design of C: A Rational Reconstruction: Part 2 The Design of C: A Rational Reconstruction: Part 2 1 Continued from previous lecture 2 Agenda Data Types Operators Statements I/O Facilities 3 Operators Issue: What kinds of operators should C have? Thought

More information

Lecture 02 C FUNDAMENTALS

Lecture 02 C FUNDAMENTALS Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void

More information

Programming Language Basics

Programming Language Basics Programming Language Basics Lecture Outline & Notes Overview 1. History & Background 2. Basic Program structure a. How an operating system runs a program i. Machine code ii. OS- specific commands to setup

More information

The C language. Introductory course #1

The C language. Introductory course #1 The C language Introductory course #1 History of C Born at AT&T Bell Laboratory of USA in 1972. Written by Dennis Ritchie C language was created for designing the UNIX operating system Quickly adopted

More information

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any  from ee209 yet Follow the link from our class homepage EE209: C Examples 1 Reminder Sign up for ee209 mailing list If you haven t received any email from ee209 yet Follow the link from our class homepage Precept 7:00-8:15pm, every Wednesday 창의학습관 (Creative

More information

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR.  VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS S.LAWRENCE CHRISTOPHER, M.C.A., B.Ed., LECTURER IN COMPUTER SCIENCE PONDICHERRY

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C Princeton University Computer Science 217: Introduction to Programming Systems A Taste of C C 1 Goals of this Lecture Help you learn about: The basics of C Deterministic finite-state automata (DFA) Expectations

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Gives

More information

Algorithms, Data Structures, and Problem Solving

Algorithms, Data Structures, and Problem Solving Algorithms, Data Structures, and Problem Solving Masoumeh Taromirad Hamlstad University DT4002, Fall 2016 Course Objectives A course on algorithms, data structures, and problem solving Learn about algorithm

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

C - Basics, Bitwise Operator. Zhaoguo Wang

C - Basics, Bitwise Operator. Zhaoguo Wang C - Basics, Bitwise Operator Zhaoguo Wang Java is the best language!!! NO! C is the best!!!! Languages C Java Python 1972 1995 2000 (2.0) Procedure Object oriented Procedure & object oriented Compiled

More information

EC 413 Computer Organization

EC 413 Computer Organization EC 413 Computer Organization C/C++ Language Review Prof. Michel A. Kinsy Programming Languages There are many programming languages available: Pascal, C, C++, Java, Ada, Perl and Python All of these languages

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University C: A High-Level Language! Gives

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda. Princeton University Computer Science 217: Introduction to Programming Systems Goals of this Lecture A Taste of C C Help you learn about: The basics of C Deterministic finite-state automata (DFA) Expectations

More information

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32 CMPT 115 C tutorial for students who took 111 in Java Mark G. Eramian Ian McQuillan University of Saskatchewan Mark G. Eramian, Ian McQuillan CMPT 115 1/32 Part I Starting out Mark G. Eramian, Ian McQuillan

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

3/13/2012. ESc101: Introduction to Computers and Programming Languages

3/13/2012. ESc101: Introduction to Computers and Programming Languages ESc101: Introduction to Computers and Programming Languages Instructor: Krithika Venkataramani Semester 2, 2011-2012 The content of these slides is taken from previous course lectures of Prof. R. K. Ghosh,

More information

C Programming

C Programming 204216 -- C Programming Chapter 3 Processing and Interactive Input Adapted/Assembled for 204216 by Areerat Trongratsameethong A First Book of ANSI C, Fourth Edition Objectives Assignment Mathematical Library

More information

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code CS102: Standard I/O Our next topic is standard input and standard output in C. The adjective "standard" when applied to "input" or "output" could be interpreted to mean "default". Typically, standard output

More information

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23 Structure of this course Programming in C: types, variables, expressions & statements functions, compilation, pre-processor pointers, structures extended examples, tick hints n tips Programming in C++:

More information

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language Part 2 (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Input/Output Structures and Arrays 2 Basic I/O character-based putchar (c) output getchar

More information

CS Programming In C

CS Programming In C CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

C Programming Multiple. Choice

C Programming Multiple. Choice C Programming Multiple Choice Questions 1.) Developer of C language is. a.) Dennis Richie c.) Bill Gates b.) Ken Thompson d.) Peter Norton 2.) C language developed in. a.) 1970 c.) 1976 b.) 1972 d.) 1980

More information

Outline. 1 About the course

Outline. 1 About the course Outline EDAF50 C++ Programming 1. Introduction 1 About the course Sven Gestegård Robertz Computer Science, LTH 2018 2 Presentation of C++ History Introduction Data types and variables 1. Introduction 2/1

More information

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Goals of C  The Goals of C (cont.)  Goals of this Lecture The Design of C: A Rational Reconstruction Goals of this Lecture The Design of C: A Rational Reconstruction Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C Why? Learning

More information

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

More information

Goals of this Lecture

Goals of this Lecture A Taste of C C 1 Goals of this Lecture Help you learn about: The basics of C Deterministic finite state automata (DFA) Expectations for programming assignments Why? Help you get started with Assignment

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! Gives

More information

Introduction to C. Systems Programming Concepts

Introduction to C. Systems Programming Concepts Introduction to C Systems Programming Concepts Introduction to C A simple C Program Variable Declarations printf ( ) Compiling and Running a C Program Sizeof Program #include What is True in C? if example

More information

Presented By : Gaurav Juneja

Presented By : Gaurav Juneja Presented By : Gaurav Juneja Introduction C is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs of UNIX are written

More information

Tail recursion. Decision. Assignment. Iteration

Tail recursion. Decision. Assignment. Iteration Computer Programming Tail recursion. Decision. Assignment. Iteration Marius Minea marius@cs.upt.ro 7 October 2014 Two ways of writing recursion unsigned max(unsigned a, unsigned b) { return a > b? a :

More information

CSCI 2132 Software Development. Lecture 8: Introduction to C

CSCI 2132 Software Development. Lecture 8: Introduction to C CSCI 2132 Software Development Lecture 8: Introduction to C Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 21-Sep-2018 (8) CSCI 2132 1 Previous Lecture Filename substitution

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 7: Introduction to C (pronobis@kth.se) Overview Overview Lecture 7: Introduction to C Wrap Up Basic Datatypes and printf Branching and Loops in C Constant values Wrap Up Lecture 7: Introduction

More information

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space. The char Type The C type char stores small integers. It is 8 bits (almost always). char guaranteed able to represent integers 0.. +127. char mostly used to store ASCII character codes. Don t use char for

More information

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Language Design COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Language Design Issues Syntax: how programs look Names and reserved words Instruction

More information

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein

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

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations

More information

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below: Q1. Explain the structure of a C program Structure of the C program is shown below: Preprocessor Directives Global Declarations Int main (void) Local Declarations Statements Other functions as required

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ 1. Types Variables Expressions & Statements Dr. Anil Madhavapeddy University of Cambridge (based on previous years thanks to Alan Mycroft, Alastair Beresford and Andrew Moore)

More information

File Handling in C. EECS 2031 Fall October 27, 2014

File Handling in C. EECS 2031 Fall October 27, 2014 File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically

More information

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary Chris J Michael cmicha1@lsu.edu 28 August 2008 C Language Summary Heavily Influenced by the GNU C Reference Manual: http://www.gnu.org/software/gnu-c-manual/ Introduction -C98, or the original ANSI C standard

More information

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

!#$% &'($) *+!$ 0!' 0+'&$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-!.&/+*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./ 0!'" 0+'&"$ &0-2$ 10 +3&2),&/3+, #include int main() int i, sum, value; sum = 0; printf("enter ten numbers:\n"); for( i = 0; i < 10; i++ ) scanf("%d", &value); sum = sum + value; printf("their

More information

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition

More information

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C Princeton University Computer Science 217: Introduction to Programming Systems The Design of C C is quirky, flawed, and an enormous success. While accidents of history surely helped, it evidently satisfied

More information

CS240: Programming in C. Lecture 2: Overview

CS240: Programming in C. Lecture 2: Overview CS240: Programming in C Lecture 2: Overview 1 Programming Model How does C view the world? Stack Memory code Globals 2 Programming Model Execution mediated via a stack function calls and returns local

More information

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017 C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually

More information

Data Types. Data Types. Integer Types. Signed Integers

Data Types. Data Types. Integer Types. Signed Integers Data Types Data Types Dr. TGI Fernando 1 2 The fundamental building blocks of any programming language. What is a data type? A data type is a set of values and a set of operations define on these values.

More information

program structure declarations and definitions expressions and statements more standard I/O

program structure declarations and definitions expressions and statements more standard I/O imperative week 2 and definitions expressions and more standard I/O Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 21 : typical #include ... // library,

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

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,

More information

Unit 4. Input/Output Functions

Unit 4. Input/Output Functions Unit 4 Input/Output Functions Introduction to Input/Output Input refers to accepting data while output refers to presenting data. Normally the data is accepted from keyboard and is outputted onto the screen.

More information

Computer Programming

Computer Programming Computer Programming Introduction Marius Minea marius@cs.upt.ro http://cs.upt.ro/ marius/curs/cp/ 26 September 2017 Course goals Learn programming fundamentals no prior knowledge needed for those who know,

More information

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011 Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011 Autumn 2015 Lecture 3, Simple C programming M. Eriksson (with contributions from A. Maki and

More information

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering. C Tutorial: Part 1 Dr. Charalampos C. Tsimenidis Newcastle University School of Electrical and Electronic Engineering September 2013 Why C? Small (32 keywords) Stable Existing code base Fast Low-level

More information

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, Yashwant Malaiya Colorado State University C: A High-Level Language

More information

The Design of C: A Rational Reconstruction (cont.)

The Design of C: A Rational Reconstruction (cont.) The Design of C: A Rational Reconstruction (cont.) 1 Goals of this Lecture Recall from last lecture Help you learn about: The decisions that were available to the designers of C The decisions that were

More information

Introduction to C An overview of the programming language C, syntax, data types and input/output

Introduction to C An overview of the programming language C, syntax, data types and input/output Introduction to C An overview of the programming language C, syntax, data types and input/output Teil I. a first C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1 PROGRAMMING LANGUAGE C is

More information