# The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:

Save this PDF as:

Size: px
Start display at page:

Download "The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:"

## Transcription

1 1 2 Simple and compound s The if- Simple s: E.g.: expression; Various jumps : break, goto, continue, return. k = a * p + 3; printf("k = %d\n", k); 1 + 2; ; The if- comes in two forms: or E.g.: if (expression) if (expression) else Compound s: declarations s if (n > 0) n--; else printf("!\n"); n = 100; 3 4 The if- (cont). The switch- if (c == a ) else if (c == b ) else if (c == c ) else What happens below? Assume i = 0 and consider both the case decrement = 0 and decrement = 1. if (decrement) if (i > 0) i--; else i++; The switch- is similar to case in Pascal: switch (expression) case label 1 : s case label 2 : s default: s Note: expression of some integer type. Each label must be a constant. Fall through! Explicit breaks needed!

2 5 6 The switch- (cont.) The while and do s Example: Syntax: switch (c) case a : i++; break; case : case \t : case \n : blank = 1; break; default: j++; break; while (expression) do while (expression); Example: int fac(int n) int f, i; f = i = 1; while (++i <= n) f *= i; 7 8 The for- Function definition using prototypes Very common. Syntax: for (exp 1 ; exp 2 ; exp 3 ) Equivalent to: exp 1 ; while (exp 2 ) exp 3 ; Examples. for (;;) printf("hello!\n"); s = 0; for (i = 1; i <= 10; i++) s += i; for (s=0, i=1; i<=10; s+=(i++)); The general form of a function definition: type name(formal args) local vars code where formal args is a list of the form: type 1 arg 1,, type n arg n An example: double max(double x, double y) if (x > y) return x; return y;

3 9 10 Function definition (cont.) Two special prototypes: No parameters: type name(void) /*body*/ Variable number of parameters: type name(args, ) /*body*/ As an example, the printf-prototype: int printf(const char *format, ) Functions without a result ( procedures ): void foo(formal args) /*body*/ Only return s of the form return; are allowed in the body. Declaring functions using prototypes A function declaration looks like: type name(formal args); where formal args is a list of the form: type 1 arg 1,, type n arg n, or type 1,, type n What is the difference? A function declaration makes the function known to the compiler. A function may be declared more than once. A function definition gives the code for the function. A function must be defined exactly once, later in the same file or externally Generally: Definitions vs. declarations A declaration specifies what an identifier stands for. A definition is a construction that, in addition to declaring an identifier, also reserves space for it and gives its value. Old style (K&R) function definitions and declarations Old style function definition: type name(arg. names) arg. decls Typical example: As a rule, always at least declare before use. Include files (e.g. stdio.h etc.) contain declarations only. This is vital. int foo(x, y) double x; double y; Old style function declaration: type name(); E.g. int foo(); No info about number of args or their type!

4 13 14 A function call: Calling functions name(actual args.) Calling a function without arguments: name() Note: the parentheses are essential. Argument passing is always call-by-value: No reference arguments. The same effect is obtained by passing pointers by value. Examples: int foo(float, int); i = foo(1.2, 17); printf("%d\n", foo(7.77, 42); (void) printf("%d\n", 42); Storage classes Basic idea: Storage classes determine how and when storage is allocated, e.g.: Suggest that the compiler try to put a variable in a CPU register: register int i; Tell the compiler that something is defined elsewhere: external double sin(double); Unfortunately, a little complex in practice: The exact meaning of a storage class is context dependent. Somewhat complicated default assumptions. Other uses, such as controlling visibility of top level symbols For local variables. The storage class auto Default for local variables (and formal arguments). Usually not stated explicitly. Storage is automatically allocated and deallocated (on system stack) when a block is entered/left. Any initialization performed each time. A declaration of an auto object is also a definition. An auto-object must never be accessed outside of the block in which it is defined. Example: int foo(float x, int i) int n = 10, m = i; The storage class extern Separate compilation makes it necessary to tell the compiler: which symbols that are defined in another file, i.e. defined externally, which top-level symbols should be visible outside the file, i.e. visible externally. The storage class extern serves both these purposes.

5 17 18 The storage class extern (cont.) Extern function or variable declaration: Assumed by default for top-level declarations. States the type of the object. Indicates that the object is either - defined later in the same file, or - defined in another file (externally). Storage allocated for top-level variable declaration (i.e the declaration counts as a definition, implicit initialization to 0) if: - extern is not stated explicitly, - no definition is encountered later in the file. Otherwise, no storage is allocated. The storage class extern (cont.) Extern function or variable definition: Only for top-level (global) symbols. Global storage allocated, object lives throughout the entire execution. Initialization performed only once. (Actually, initialized global data comprises a section of the executable file.) Also indicates to the compiler that the symbol should be externally visible. Assumed by default for top-level definitions; usually not stated explicitly The storage class extern (cont.) The easy option: Use extern explicitly only for importing things from other modules. Be aware of that global symbols are visible outside the file, unless you say otherwise. File foo.c Example on extern usage extern int a; int foo(int i) return a * i; File main.c #include <stdio.h> extern int foo(int); int a = 10; main() printf("%d\n", foo(7)); a = 20; printf("%d\n", foo(7));

6 21 22 The storage class static For functions and variables: Like external in that a static object is initialized once and lives during the entire execution. Unlike external in that static objects are not visible externally. Also, can be applied to local variable definitions (and declarations, which then count as definitions). Example: static int fie() static int count = 0; return (++count); Constants Variables and parameters may be declared as constants: But: const int max = 7; const char end = \0 ; Not the same thing as const in Pascal. Really variables that cannot be assigned to. Use #define if you need compile-time constants (for sizes of arrays etc.). To see the difference: int foo(int a, int b) const int m = max(a,b); /* body of function */ Strings in C No real strings in C. Instead: Pointer to a sequence of char. Explicit termination by the null character, \0. printf("hello!\n"); is represented as printf( ); H e l l o! \n \0 Declaration: T a[n]; Basic arrays The variable a is an array of N elements of the type T. Subscripting: a[0], a[1],, a[n-1] No range checks! Arrays may be initialized when declared: float f[3] = 0.0, 1.0, 2.0; int a[100] = 0; char s[] = a, b, c, \0 ; char s[] = "abc"; Default initialization to 0 of static and extern arrays.

7 25 26 #include <stdio.h> #define N 20 Bubblesort main() int a[n], i, j, temp; for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N-1; i++) for (j = N-1; j > i; j--) if (a[j-1] > a[j]) temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; for (i = 0; i < N; i++) printf("%d ", a[i]); Declaration: T *ap; Basic pointers ap can hold a pointer to an object of type T. Taking the address of a variable of type T by using & creates a pointer to an object of type T: int a, b, *ap = &a, *bp = &b; char *s = "abc"; a: b: ap: bp: s: a b c \ Basic pointers (cont.) Dereferencing pointers: a) *ap = 1; b = *ap + 1; b1) ap = bp b2) *ap = *bp; a: b: ap: a b1 b2 1 2 a: b: ap: 1 2 a: b: ap: 2 2 Call-by-value vs. call-by-reference Call-by-value (C or C++): int y = 1; void foo(int x) x++ foo(y); y is still 1 after the call (contents of y copied to x which is just a local variable). Call-by-reference (C++): int y = 1; bp: bp: bp: void foo(int &x) x++ foo(y); y is 2 after the call since the formal argument x refers to the actual argument y during the call.

8 29 30 Call-by-reference in C Arguments are always passed by value in C. No concept of reference parameters. Instead, pointers are passed explicitly. #include <stdio.h> void foo(int *ap, int *bp) scanf("%d", ap); *bp = *ap * 2; Exercise 1 Write a loop to reverse an array int a[n], i.e. swap a[0] and a[n-1], a[1] and a[n-2], etc. Write a function that may be used to swap the contents of two integer variables. Then change your code for reversing the array so that it uses your swap function. main() int a, b, c; foo(&a, &b); scanf("%d", &c); printf("a*b=%d,c=%d\n",a*b,c); Pointer arithmetic Bubblesort revisited In C, + and - also work on pointers: An integer may be added to/subtracted from a pointer. One pointer may be subtracted from another. The basic assumption is that the pointers refer to elements in some array: T *a: T *b: T x[n] a-2 a+3 Pointer arithmetic is often used to access arrays by pointer stepping. #include <stdio.h> #define N 20 main() int a[n], *ip, *jp, temp; for (ip = &a[0]; ip < &a[n]; ip++) scanf("%d", ip); for (ip = &a[0]; ip < &a[n-1]; ip++) for (jp = &a[n-1]; jp > ip; jp--) if (*(jp-1) > *jp) temp = *(jp-1); *(jp-1) = *jp; *jp = temp; for (ip = &a[0]; ip < &a[n]; ip++) printf("%d ", *ip);

9 33 34 The relationship between arrays and pointers The relationship between arrays and pointers (cont.) Arrays and pointers are not identical! char s1[] = "hello"; char *s2 = "world"; s1: h e l l o \0 In C, an equivalence between arrays and pointers in the following sense holds: An array is, with three exceptions, converted to a pointer to its first element when used. s2: w o r l d \0 The exceptions occur when the array is: an argument to sizeof, Assume a typical 32-bit computer sizeof(s1) =? sizeof(s2) =? Is the following legal C? Why? s1 = s2; s2 = s1; an argument to &, a literal string used for initialization of a char array. In all other cases we have a = &a[0]. Thus, the for-loops earlier could have been written: for (ip = a; ip < &a[n]; ip++) Given The relationship between arrays and pointers (cont.) char s1[] = "hello"; char *s2 = "world"; the equivalence explains why: s1 = s2 is as invalid as 1 = a, s2 = s1 is a legal assignment, sizeof(s1) = 6. Moreover, by definition we have: s1[i] *(s1 + i) s2[i] *(s2 + i) The equivalence (together with pointer arithmetic) explains why this works. Arrays as arguments to functions It is always a pointer to the first element that is passed! void foo(t *a) main() T a[n]; foo(a); Alternative, equivalent ways of declaring foo: void foo(t a[]) void foo(t a[n])

10 37 38 Arrays as arguments to functions (cont.) What gets printed when foo is called? void foo(int a[10]) int b[10]; printf("%d, %d\n", sizeof(a), sizeof(b)); Reading a string: char s[20]; scanf("%19s", s); s on its own is the address of the array: no & in this case! There must be room for the terminating \0. Exercise 2 Assume a typical 32-bit architecture, and: char s[5]="abcd", (*cap)[5]; Answer the following: sizeof(s) =? sizeof(cap) =? Is cap = s OK? Is cap = &s OK? sizeof(*cap) =? sizeof(&s) =? sizeof(*s) =? Is the following legal C? If so, what gets printed? printf("%c\n", 5["abcdef"]); 39 Solution exercise 1 Loop to reverse an array int a[n]: int i, temp; for (i = 0; i < N/2; i++) temp = a[i]; a[i] = a[(n-1)-i]; a[(n-1)-i] = temp; Function to swap two integer variables. void swap(int *a, int *b) int temp; temp = *a; *a = *b; *b = temp; Rewriting the loop body: swap(&a[i], &a[(n-1)-i]);

### LESSON 7. ptr = &i; ptr = 0; ptr = NULL; /* equivalent to ptr == 0; */ ptr = (int *) 1999; /* an absolute address in memory */

LESSON 7 POINTERS AND CALL BY REFERENCE When an expression is passed as an argument to a function, a copy of the value of the expression is made, and it is this copy that is passed to the function. Suppose

### Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples

Identifiers and Expressions CSE 130: Introduction to C Programming Spring 2005 Punctuation in C Statements are terminated with a ; Groups of statements are enclosed by curly braces: { and } Commas separate

### an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage.

Lecture 05 C Arrays & pointers In this lecture Introduction to 1D arrays Array representation, access and updates Passing arrays to functions Array as a const pointer Dynamic arrays and resizing Introduction

### Programming for MSc Part I

Herbert Martin Dietze University of Buckingham herbert@the-little-red-haired-girl.org July 24, 2001 Abstract The course introduces the C programming language and fundamental software development techniques.

### Arrays and Pointers (part 1)

Arrays and Pointers (part 1) EECS 2031 25 September 2016 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 1 Arrays: Example

### INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

### Going from Python to C

Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

### C Primer. Fall Introduction C vs. Java... 1

CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

### Dept. of CSE, IIT KGP

Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

### C AND C++ PROGRAMMING

C AND C++ PROGRAMMING Bharathidasan University A Courseware prepared by University Informatics Centre Part I - Programming in C Getting Started This courseware is intended to be an introduction to C programming

### The C Programming Language course syllabus associate level

TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

### Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Problem 1 Recall the definition of root in project 1. (The declaration of struct entrynode appears below.) struct entrynode * root; Give the type of each of the following expressions. The answer may be

### Common C Errors. Compiled by: Leela Kamalesh Yadlapalli

Common C Errors Compiled by: Leela Kamalesh Yadlapalli This document shows some of the common errors and warnings that you may encounter during this class. Always remember to use the Wall option if you

### Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program

Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

### Arrays. Arrays, Argument Passing, Promotion, Demotion

Arrays Arrays, Argument Passing, Promotion, Demotion Review Introduction to C C History Compiling C Identifiers Variables Declaration, Definition, Initialization Variable Types Logical Operators Control

### COMP 356 Programming Language Structures Notes for Chapter 5 of Concepts of Programming Languages Names, Types and Scopes

Some definitions: COMP 356 Programming Language Structures Notes for Chapter 5 of Concepts of Programming Languages Names, Types and Scopes a name is a string of characters (a word) that represents a program

### 1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

### C Programming Dr. Hasan Demirel

C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

### C A short introduction

About these lectures C A short introduction Stefan Johansson Department of Computing Science Umeå University Objectives Give a short introduction to C and the C programming environment in Linux/Unix Go

### Sources: On the Web: Slides will be available on:

C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

### Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C

Outline ompiling, interpreting, and running Administrative trivia Goals of the class Introduction to 1 2 The Programming Language Java vs. Systems programming language Originally used to write Unix and

### Final Exam Review. CS 1428 Fall Jill Seaman. Final Exam

Final Exam Review CS 1428 Fall 2011 Jill Seaman 1 Final Exam Friday, December 9, 11:00am to 1:30pm Derr 241 (here) Closed book, closed notes, clean desk Comprehensive (covers entire course) 25% of your

### MIT Aurangabad FE Computer Engineering

MIT Aurangabad FE Computer Engineering Unit 1: Introduction to C 1. The symbol # is called a. Header file c. include b. Preprocessor d. semicolon 2. The size of integer number is limited to a. -32768 to

### FORMAT MEANING VARIABLE TYPE

Printf and Scanf Both formatted I/O Both sent to standard I/O location Printf Converts values to character form according to the format string Scanf Converts characters according to the format string,

### CPSC 213. C vs. Java. Introduction to Computer Systems. Reading. Java Hello World... C Hello World... C, Pointers, and Dynamic Allocation.

Reading CPSC 213 Textbook New to C, Understanding Pointers, The malloc and free Functions, Why Dynamic Memory Allocation 2ed: "New to C" sidebar of 34, 310, 991-992 1ed: "New to C" sidebar of 34, 311,1091-1092

### Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types In this lecture Computer Languages Assembly Language The compiler Operating system Data and program instructions Bits, Bytes and Data Types ASCII table Data Types

### Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments

Reading C Programming Language Types, operators, expressions Control flow, functions Basic IO K&R Chapter 2 Types, Operators, and Expressions K&R Chapter 3 Control Flow K&R Chapter 7 Basic I/O NEWS Assignment

### C for Java Programmers

C for Java Programmers CS 414 / CS 415 Niranjan Nagarajan Department of Computer Science Cornell University niranjan@cs.cornell.edu Original Slides: Alin Dobra Why use C instead of Java Intermediate-level

### Introduction to C Programming S Y STEMS

Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

### Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

### A Rudimentary Intro to C programming

A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

### UNIT-1. C Programming & Data Structures. Introduction to Computers: Computing Environment: Types of Computing Environments:

Introduction to Computers: C Programming & Data Structures UNIT-1 A computer system consists of hardware and software. Computer hardware is the collection of physical elements that comprise a computer

### INTI COLLEGE MALAYSIA

CSC112 (F) / Page 1 of 5 INTI COLLEGE MALAYSIA CERTIFICATE IN COMPUTING AND INFORMATION TECHNOLOGY PROGRAMME CSC 112 : FUNDAMENTALS OF PROGRAMMING FINAL EXAMINATION : DECEMBER 2002 SESSION This paper consists

### Pointers and dynamic memory management in C

Pointers and dynamic memory management in C Jakob Rieck Proseminar C - 2014 Contents 1 Introduction 2 2 Pointers 4 2.1 Declaration & Initialization................... 5 2.2 Using pointers..........................

### z = x + y * z / 4 % 2-1

1.Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1? A. rem = 3.14 % 2.1; B. rem = modf(3.14, 2.1); C. rem = fmod(3.14, 2.1); D. Remainder cannot be obtain

### Quiz 3 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 3 S o l u t i o n s. Q1: C++ functions other than main are executed:

Quiz 3 Solutions Q1: C++ functions other than main are executed: a. Before main executes. b. After main completes execution. c. When they are explicitly called by another function. d. Never. ANS c. When

### ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers 0.. +127. char variables mostly used to store characters

### Arrays, strings, and functions

Arrays, strings, and functions Goals of this Lecture Helps you learn about: Arrays and strings Functions Recursive functions Some pointer concept, but we will defer the details to next lecture Subset of

### Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

### Programming Language: Syntax. Introduction to C Language Overview, variables, Operators, Statements

Programming Language: Syntax Introduction to C Language Overview, variables, Operators, Statements Based on slides McGraw-Hill Additional material 2004/2005 Lewis/Martin Modified by Diana Palsetia Syntax

### CS 11 C track: lecture 3

CS 11 C track: lecture 3 n This week: n Arrays n one-dimensional n multidimensional n Command-line arguments n Assertions Arrays n What is an "array"? n A way to collect together data of a single type

### C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

### CMSC 106 Lecture Set #2. C language syntax. Syntax vs Semantics Examples

CMSC 106 Lecture Set #2 C Language Introduction C language syntax Syntax rules of the grammar vocabulary recognized by the language ANSI standard American National Standards Institute Semantics the meaning

### This exam is to be taken by yourself with closed books, closed notes, no calculators.

Student ID CSE 5A Name Final Signature Fall 2003 Page 1 (12) cs5a This exam is to be taken by yourself with closed books, closed notes, no calculators. Page 2 (33) Page 3 (33) Page 4 (27) Page 5 (40) Page

### SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E.

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK Subject: Computer Programming and Utilization (2110003) [F.Y.B.E.: ALL BRANCHES] Unit 1

### 7 Introduction to C++

7 Introduction to C++ 7.1 Introduction C++ is an extension to C Programming language. It was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. It is a deviation from traditional

### 6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

### BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering Cohort: BCNS/07/FT- BIS/07/FT- BIS/06/FT -BM/07/FT BSE/07/FT/PT-

### The main features of First Generation are:

MODEL SOLUTION AS -4016 B.Te ch (First Semester) course A INTRODUCTION TO COMPUTER PROGRAMMING ANS 1 : i. Electrically Erasable Programmable Read Only Memory ii. Four Generation iii. c iv. 8 v. High Level

### Your first C program. Which one is best?

Your first C program #include void main(void) { printf( Hello, world!\n ); } #include void main(void) { printf( Hello, ); printf( world! ); printf( \n ); } Which one is best? #include

### 05 Case Study: C Programming Language

CS 2SC3 and SE 2S03 Fall 2009 05 Case Study: C Programming Language William M. Farmer Department of Computing and Software McMaster University 18 November 2009 The C Programming Language Developed by Dennis

### The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())!

Character and String Processing CSE 130: Introduction to C Programming Spring 2005 The char Data Type A char value can be thought of as either a character or a small integer printf( %d, a ); /* prints

### Strings in C++ and Java. Questions:

Strings in C++ and Java Questions: 1 1. What kind of access control is achieved by the access control modifier protected? 2 2. There is a slight difference between how protected works in C++ and how it

### C programming. Intro to syntax & basic operations

C programming Intro to syntax & basic operations Example 1: simple calculation with I/O Program, line by line Line 1: preprocessor directive; used to incorporate code from existing library not actually

### 6.087 Lecture 3 January 13, 2010

6.087 Lecture 3 January 13, 2010 Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables 1 Review:

### Chapter 8. Arithmetic in C++

Christian Jacob Chapter 8 Arithmetic in C++ 8.1 The C++ Vocabulary 8.2 Variables and Types 8.2.1 Data Objects 8.2.2 Variables 8.2.3 Declaration of Variables 8.3 Elementary C++ Data Types 8.3.1 Integers

### Introduction to C Programming

Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

### Learning objectives. So what is Problem Solving? CSI1102 Introduction to Software Design. A reminder: Assumed background knowledge

CSI1102 Introduction to Software Design Chapter 1: Introduction Learning objectives Understand what problem solving entails Understand why problem solving skills are so important Describe the various levels

### What does the following code print? (Assume the program compiles without error). Trick question! Note the semicolon at the end of the second line.

All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

### arrays C Programming Language - Arrays

arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

### Java Review (Essentials of Java for Hadoop)

Java Review (Essentials of Java for Hadoop) Have You Joined Our LinkedIn Group? What is Java? Java JRE - Java is not just a programming language but it is a complete platform for object oriented programming.

### strsep exercises Introduction C strings Arrays of char

strsep exercises Introduction The standard library function strsep enables a C programmer to parse or decompose a string into substrings, each terminated by a specified character. The goals of this document

### Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

### Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

### The Basics of C Programming. Marshall Brain

The Basics of C Programming Marshall Brain Last updated: October 30, 2013 Contents 1 C programming 1 What is C?................................. 2 The simplest C program, I........................ 2 Spacing

### Creating Functions in C/C++, Part 2

Call-by-Reference Parameters What if we want to write a function that actually changes the value of its parameters? For example, suppose we want a function that can be called like this: swap(x,y) that

### Branching. We ll continue to look at the basics of writing C++ programs including. Overview

Branching Overview We ll continue to look at the basics of writing C++ programs including Boolean Expressions (need to know this before we can using branching and looping!) Branching J.S. Bradbury CSCI

### 1. What is the output of this program?

1. What is the output of this program? char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); (a) How are you? (b) are you? (c) are (d) No output 2. Which of the following

### CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

### ,!7IA3C1-dcahfj!:t;K;k;K;k. Addison-Wesley s JavaScript Reference Card. Kathleen M. Goelz and Carol J. Schwartz, Rutgers University VARIABLES

Addison-Wesley s JavaScript Reference Card Kathleen M. Goelz and Carol J. Schwartz, Rutgers University Javascript: A scripting language designed to be integrated into HTML code to produce enhanced, dynamic,

### Pseudocode. Pseudocode. Guide for Pseudocode. Computers in Engineering Pseudocode and C Language Review. Example Pseudocode.

Computers in Engineering Pseudocode and C Language Review Pseudocode Pseudocode is an artificial and informal language that helps you develop algorithms Pseudocode is similar to everyday English; it is

### M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: IMPORTANT INSTRUCTIONS: 1. Question Paper in English and Hindi and Candidate can choose any one language. 2. In case of discrepancies in

### YOU MUST TURN IN EVERY PAGE OF THIS EXAM including the ref card page

CSE2421 MIDTERM Spring 2013 NAME SCORE / 100 FYI: There are no compiler errors in the code presented on this exam. If you think there is, please bring it to my attention. NOTE: If there is not an answer

### Common Errors in C. David Chisnall. February 15, 2011

Common Errors in C David Chisnall February 15, 2011 The C Preprocessor Runs before parsing Allows some metaprogramming Preprocessor Macros Are Not Functions The preprocessor performs token substitution

### Senem Kumova Metin & Ilker Korkmaz 1

Senem Kumova Metin & Ilker Korkmaz 1 A loop is a block of code that can be performed repeatedly. A loop is controlled by a condition that is checked each time through the loop. C supports two categories

### Example Solution to Exam in EDA150 C Programming

Example Solution to Exam in EDA150 C Programming Janurary 12, 2011, 14-19 Inga hjälpmedel! Examinator: Jonas Skeppstedt, tel 0767 888 124 30 out of 60p are needed to pass the exam. General Remarks A function

### C++ Programming Language

C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract

### Introduction to Java

Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high

### X86-64 Architecture Guide

X86-64 Architecture Guide For the code-generation project, we shall expose you to a simplified version of the x86-64 platform. Example Consider the following Decaf program: class Program { int foo(int

### BHARATHIAR UNIVERSITY: COIMBATORE CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++ LANGUAGE

Certificate in Advanced Programming - C++ Language Page 1 of 7 BHARATHIAR UNIVERSITY: COIMBATORE 641046 CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++

### Introduction to Programming Block Tutorial C/C++

Michael Bader Master s Program Computational Science and Engineering C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers

### A First Book of C++ Chapter 2 Data Types, Declarations, and Displays

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

### Indicates the end of data when the number of data is not known in advance Must be distinct from the data but be of the same type

Program Control Essentials of repetition Loop control variable Counts the number of repetitions in the counter-controlled loop Controls the execution of loop Sentinel value Indicates the end of data when

### Python to C/C++ Fall 2011

Python to C/C++ Fall 2011 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. C: Both require #include directives to access libraries

### 12 INPUT AND OUTPUT OF DATA

12 INPUT AND OUTPUT OF DATA 12.1 INTRODUCTION In C language input and output of data is done by a collection of library functions like getchar, putchar, scanf, printf, gets and puts. These functions permit

### SYSTEMS PROGRAMMING C++ INTRODUCTION

Faculty of Computer Science / Institute of Systems Architecture / Operating Systems SYSTEMS PROGRAMMING C++ INTRODUCTION Alexander Warg WHY C++? C++ is the language that allows to express ideas from the

### C: How to Program. Week /June/11

C: How to Program Week 16 2007/June/11 1 Chapter 10 - C Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing

### Semantic Analysis: Types and Type Checking

Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors

### Arrays. In C++, all indexes start at zero. The first element of the array a is referred to as a[0], the second element as a[1], the third as a[?]..

Arrays Definition: An array is a collection of variables of the same type that are referenced by a common name. -In C++, all arrays consist of contiguous memory locations. -To refer to an element or particular

### Thinking in C. Darin Brezeale. March 25, 2010

Thinking in C Darin Brezeale March 25, 2010 NOTE: This is definitely a work in progress. 1 Introduction One of the most difficult parts of learning to program is knowing how to deconstruct a problem in

### Convert Temperature CMPE-013/L. Constants and Literals. The Code #include <stdio.h> Gabriel Hugh Elkaim Winter 2014

CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Winter 2014 Convert Temperature Want to write a program to convert the temperature from Fahrenheit to Celsius for a range of temperatures. Demonstrates

### Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

### Data Structures. Arrays. Array Example. Array Initialization. 57:017, Computers in Engineering Review of Arrays, Sorting, Searching

57:07, Computers in Engineering Review of Arrays, Sorting, Searching Data Structures Our focus thus far has mainly been on control structures An array is a data structure consisting of data items of the

### The programming language C. sws1 1

The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan

### C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,

### Figure 1.12 Entering, Translating, and Running a High-Level Language Program Pearson Education, Inc. All rights reserved.

Figure 1.12 Entering, Translating, and Running a High-Level Language Program 1 2007 Pearson Education, Inc. All rights reserved. 2 Figure 2.7 General Form of a C Program 2007 Pearson Education, Inc. All