C LANGUAGE A Short Course

Size: px
Start display at page:

Download "C LANGUAGE A Short Course"

Transcription

1 C LANGUAGE A Short Course Alvaro F. M. Azevedo January 2002 C Language - Alvaro Azevedo 1

2 ANSI C Standard (ANSI, ISO) Compiled - efficient Low level / high level Other languages are based on the syntax of the C language Example: C++, Java, C#, etc. January 2002 C Language - Alvaro Azevedo 2

3 MY FIRST FULL PROGRAM #include <stdio.h> int main() printf("hello world!\n"); return 0; Recommended source file extension:.c Example: my_first_test.c January 2002 C Language - Alvaro Azevedo 3

4 DECLARATIONS /* This is a comment */ int k; /* k is a signed integer */ float x; /* x is a single precision real number */ double y; /* y is a double precision real number */ k = -3; x = 8.19; y = x / 7 + k; printf("k = %4d, x = %6.2f, y = %12.7lf\n", k, x, y); January 2002 C Language - Alvaro Azevedo 4

5 if if (k > 3) printf("k is GREATER THAN 3\n"); if (k >= 3) printf("k is GREATER THAN OR EQUAL to 3\n"); if (k == 3) printf("k is EQUAL to 3\n"); if (k!= 3) printf("k is NOT EQUAL to 3\n"); if (i < 0 && j < 0) printf("i AND j are negative\n"); if (i < 0 j < 0) printf("i OR j is negative\n"); if (! (i < 0 && j < 0) ) printf("not(i < 0 and j < 0)\n"); January 2002 C Language - Alvaro Azevedo 5

6 if int j = 6; int ktest = 4; if (ktest) /* 0 => FALSE; Not equal to 0 => TRUE */ j++; /* j = j + 1 */ printf("j = %d\n", j); /* Output: j = 7 */ else j--; /* j = j - 1 */ printf("j = %d\n", j); /* if */ January 2002 C Language - Alvaro Azevedo 6

7 if if (ktest > 0) j += 100; /* j = j */ printf("j = %d\n", j); else if (ktest < 0) j -= 100; /* j = j */ printf("j = %d\n", j); else printf("j unchanged\n"); /* if */ January 2002 C Language - Alvaro Azevedo 7

8 OTHER COMPOUND OPERATORS j *= i + 2; /* j = j * (i + 2) */ jnewcoeff /= a - b; /* jnewcoeff = jnewcoeff / (a - b) */ OTHER FORMAT SPECIFIERS j = 37; printf("%5.5d\n", j); /* */ a = ; printf("%12.4le\n", a); /* e-003 */ Difference must be > 8 January 2002 C Language - Alvaro Azevedo 8

9 INPUT int myage; double todaystemperature; printf("how old are you? "); scanf("%d", &myage); /* Note the ampersand (&) */ printf("tell me the temperature outside... "); scanf("%lf", &todaystemperature); printf("i am %d years old and ", myage); printf("the temperature\noutside is %5.1lf degrees.\n", todaystemperature); January 2002 C Language - Alvaro Azevedo 9

10 ARRAYS int v[3]; /* Array of 3 integers */ v[0] = 8000; v[1] = 8001; v[2] = 8002; double a[5][3]; /* 5x3 matrix of doubles */ a[0][0] = 9.00; a[0][1] = 9.01; a[0][2] = 9.02; a[1][0] = 9.10; a[1][1] = 9.11; a[1][2] = 9.12; a[2][0] = 9.20; a[2][1] = 9.21; a[2][2] = ; a[3][0] = 9.30; a[3][1] = 9.31; a[3][2] = 9.32; a[4][0] = 9.40; a[4][1] = 9.41; a[4][2] = 9.42; January 2002 C Language - Alvaro Azevedo 10

11 for LOOP for (i = 1; i <= 6; i++) printf("%4d %4d\n", i, 100 * i); /* for (i) */ for (i = -5, j = 1; j <= 12 && i!= 2 * j; i -= 2, j += 3) printf("%4d %4d %4d\n", i, j, 2 * j); /* for (i,j) */ January 2002 C Language - Alvaro Azevedo 11

12 for LOOP /* Print a 5x3 matrix */ printf("matrix a:\n"); for (i = 0; i < 5; i++) for (j = 0; j < 3; j++) printf(" %10.4lf", a[i][j]); /* for (j) */ printf("\n"); /* for (i) */ January 2002 C Language - Alvaro Azevedo 12

13 CHARACTERS char gender; /* Male/Female => 'M'/'F' */ printf("male (M) or Female (F)? "); gender = getchar(); if (gender!= 'M' && gender!= 'F') printf("error!\n"); exit(1); /* #include <stdlib.h> is required */ /* if */ printf("the gender is: %c\n", gender); January 2002 C Language - Alvaro Azevedo 13

14 STRINGS /* A string is an array of char */ char t[4]; strcpy(t, "ABC"); /* #include <string.h> is required */ printf("t[0] = %3d\n", t[0]); /* 65 => ASCII code of A */ printf("t[1] = %3d\n", t[1]); /* 66 => ASCII code of B */ printf("t[2] = %3d\n", t[2]); /* 67 => ASCII code of C */ printf("t[3] = %3d\n", t[3]); /* 0 => All strings are terminated by a NULL character. */ /* printf("t[4] = %3d\n", t[4]); => ERROR */ printf("the string is: %s\n", t); /* The string is: ABC */ January 2002 C Language - Alvaro Azevedo 14

15 POINTERS double z; /* z is a double */ double* p; /* p is a pointer to a double */ z = 5.87; /* z is initialized with the value 5.87 */ p = &z; /* p is initialized with the address of z */ printf("z = %6.2lf\n", z); /* z = 5.87 */ printf("p = %d\n", p); /* p = */ printf(">>> *p = %6.2lf\n", *p); /* >>> *p = 5.87 */ Notes: & is the address-of operator * is the indirection or dereferencing operator January 2002 C Language - Alvaro Azevedo 15

16 FILES (input) FILE* f; /* #include <stdio.h> is required */ f = fopen("my_file.txt", "r"); /* "r" means "read" */ if (f == NULL) fprintf(stderr, "File not found\n"); exit(1); /* Program terminates */ /* if */ fscanf(f, "%lf", &z); /* z must be declared as a double */ printf("z = %lg\n", z); fclose(f); /* The file is closed */ January 2002 C Language - Alvaro Azevedo 16

17 FILES (output) FILE* f; /* #include <stdio.h> is required */ f = fopen("my_file.txt", "w"); /* "w" means "write" */ if (f == NULL) fprintf(stderr, "Unable to open the output file\n"); exit(1); /* Program terminates */ /* if */ fprintf(f, "z = %6.2lf\n", z); /* z is a double */ fclose(f); /* The file is closed */ January 2002 C Language - Alvaro Azevedo 17

18 break AND continue int i, j; for (i = 20; i <= 40; i++) j = i / 6; /* Integer division */ if (j == 5) break; /* for loop is terminated */ printf("%2d %2d\n", i, j); /* for (i) */ for (i = 20; i <= 40; i++) j = i % 6; /* Remainder of the integer division */ if (j == 0) continue; /* "goto" the beginning */ printf("%3d %3d\n", i, j); /* for (i) */ January 2002 C Language - Alvaro Azevedo 18

19 break WITH NESTED FOR LOOPS for (i = 1; i <= 8; i++) for (j = 1; j <= 6; j++) k = i + 2 * j; if (k == 9) break; /* for (j) is terminated */ printf("%3d %3d... %3d\n", i, j, k); /* for (j) */ printf("================\n"); /* for (i) */ January 2002 C Language - Alvaro Azevedo 19

20 OTHER STATEMENTS for (;;) /* Infinite loop (forever) */... if (condition) break;... /* for (;;) */ while (condition)... /* while */ do... while (condition); January 2002 C Language - Alvaro Azevedo 20

21 switch switch (nchoice) case 1:... /* Executed when nchoice == 1 */ break; case 5: case 8:... /* Executed when nchoice == 5 nchoice == 8 */ break; default: fprintf(stderr, "Valid choices are: 1, 5 or 8\n"); exit(1); /* switch (nchoice) */ January 2002 C Language - Alvaro Azevedo 21

22 #define AND const #define PI /* Simple text replacement */ const double PI = ; /* PI is a const variable */ sscanf AND sprintf char t[20]; double x = -2.87; double y; sprintf(t, "%6.2lf", x); /* Output goes to the string t */ strcat(t, "654"); /* String concatenation */ sscanf(t, "%lf", &y); /* Input from the string t */ printf("y = %12.8lf\n", y); /* y = */ January 2002 C Language - Alvaro Azevedo 22

23 malloc AND free #include <stdlib.h> /* malloc, free */... int i, n; double* vect; printf("n? "); scanf("%d", &n); /* Allocate memory: */ vect = (double*)malloc(n * sizeof(double)); for (i = 0; i < n; i++) vect[i] = (double)i / 3; /* i is converted into a double */... free(vect); /* Free memory */ January 2002 C Language - Alvaro Azevedo 23

24 FUNCTIONS void decorate(char* t) printf("\n"); printf("/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\\n\n"); printf(">>> %s\n\n", t); printf("\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\n\n"); int main() char msg[] = "Very nice!"; decorate(msg); /* The function decorate is called */ return 0; January 2002 C Language - Alvaro Azevedo 24

25 FUNCTIONS Memory address char calc(int k, double x, double* presult ) k -= 3; /* Modifying a copy of the original k */ if (k == 0) return 1; *presult = x / k; printf("function calc: k = %d\n", k); return 0; int main() int k = 8; double x = 5.55; double result; char c; c = calc(k, x, &result ); /* c == 0 indicates success */ if (c == 0) printf("result = %lg\n", result); printf("main: k = %d\n", k); /* k = 8 (unchanged) */ return 0; January 2002 C Language - Alvaro Azevedo 25

26 argc AND argv int main(int argc, char* argv[]) /* argc is the n. of command line arguments */ if (argc!= 3) /* The program name is also counted */ fprintf(stderr, "When this program is executed,\n"); fprintf(stderr, "two arguments must follow the\n"); fprintf(stderr, "program name.\n\n"); fprintf(stderr, "Usage: show_args arg1 arg2\n"); exit(1); /* if */ printf("argv[0] = %s\n", argv[0]); printf("argv[1] = %s\n", argv[1]); printf("argv[2] = %s\n", argv[2]); Note: save this file as "show_args.c" return 0; January 2002 C Language - Alvaro Azevedo 26

27 struct struct Point double x; double y; int size; char color; ; /* Semicolon (;) required */ int main() struct Point A, B; A.x = 70.1; A.y = 70.2; A.size = 5; A.color = 'R'; B = A; printf("b = ( %lg, %lg ) -> %d/%c\n", B.x, B.y, B.size, B.color); return 0; January 2002 C Language - Alvaro Azevedo 27

28 struct AS A FUNCTION ARGUMENT void PrintPoint(char* description, struct Point K) /* Copies of the data members of */ /* the struct are printed. */ printf("\n%s: ( %lg, %lg ) -> %d/%c\n", description, K.x, K.y, K.size, K.color); int main() struct Point A, B; A.x = 70.1; A.y = 70.2; A.size = 5; A.color = 'R'; B = A; PrintPoint("Point A", A); PrintPoint("Point B", B); return 0; January 2002 C Language - Alvaro Azevedo 28

29 struct MODIFIED BY A FUNCTION void ReadPoint(char* description, struct Point* pk) printf("\n%s (x,y,size,color)\n", description); printf("x? "); scanf("%lf", &pk->x); printf("y? "); scanf("%lf", &pk->y); printf("size? "); scanf("%d", &pk->size); printf("color [1-5]? "); scanf("%d", &pk->color); pk->color += 64; /* [1-5] -> [A-E] */ int main() struct Point A, B; ReadPoint("Point A?", &A); ReadPoint("Point B?", &B); PrintPoint(">>> Point A", A); PrintPoint(">>> Point B", B); return 0; January 2002 C Language - Alvaro Azevedo 29

30 struct CONTAINING AN ARRAY struct Vector int size; double* vect; ; int main() struct Vector A, B; A.size = 3; A.vect = (double*)malloc(3 * sizeof(double)); A.vect[0] = 7.00; A.vect[1] = 7.01; A.vect[2] = 7.02; B = A; printf("a => %d, %8.2lf\n", A.size, A.vect[1]); printf("b => %d, %8.2lf\n", B.size, B.vect[1]); A.vect[1] = ; /* Why was this ignored? */ B.vect[1] = ; printf("a => %d, %8.2lf\n", A.size, A.vect[1]); printf("b => %d, %8.2lf\n", B.size, B.vect[1]);... January 2002 C Language - Alvaro Azevedo 30

31 NEXT CHAPTER: C++... January 2002 C Language - Alvaro Azevedo 31

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock) C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

More information

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify

More information

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the

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 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Program Structure /* Print "hello, world" to stdout. Return 0. */ { printf("hello, world\n"); -----------------------------------------------------------------------------------

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28 From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs,

More information

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function. (i) (5 pts.) SOFTWARE Ph.D. Qualifying Exam Spring 2018 Consider the following C program which consists of two function definitions including the main function. #include int g(int z) { int y

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables) Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)

More information

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point. Chapter 1. An Overview 1.1 Programming and Preparation Operating systems : A collection of special programs Management of machine resources Text editor and C compiler C codes in text file. Source code

More information

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( ) Systems Group Department of Computer Science ETH Zürich Tutorial 1: Introduction to C Computer Architecture and Systems Programming (252-0061-00) Herbstsemester 2012 Goal Quick introduction to C Enough

More information

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark] Note to Students: This file contains sample solutions to the term test together with the marking scheme and comments for each question. Please read the solutions and the marking schemes and comments carefully.

More information

A Short Course for REU Students Summer Instructor: Ben Ransford

A Short Course for REU Students Summer Instructor: Ben Ransford C A Short Course for REU Students Summer 2008 Instructor: Ben Ransford http://www.cs.umass.edu/~ransford/ ransford@cs.umass.edu 1 Outline Today: basic syntax, compilation Next time: pointers, I/O, libraries

More information

A Short Course for REU Students Summer Instructor: Ben Ransford

A Short Course for REU Students Summer Instructor: Ben Ransford C A Short Course for REU Students Summer 2008 Instructor: Ben Ransford http://www.cs.umass.edu/~ransford/ ransford@cs.umass.edu 1 Outline Last time: basic syntax, compilation This time: pointers, libraries,

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga Arrays and Strings Antonio Carzaniga Faculty of Informatics Università della Svizzera italiana February 23, 2015 Outline General memory model Definition and use of pointers Invalid pointers and common

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Functions Similar to (static) methods in Java without the class: int f(int a, int

More information

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison

More information

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and

More information

Final Intro to C Review

Final Intro to C Review Final Exam Content: Final Intro to C Review - Pass by reference Functions - General Syntax - Structures - Recursion(maybe?) - Programming by nature is cumulative so any past material is up for grabs as

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 7 January Takako Nemoto (JAIST) 7 January 1 / 13 Usage of pointers #include int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko;

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 11: Memory, Files and Bitoperations (yaseminb@kth.se) Overview Overview Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations Lecture 11: Memory, Files

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

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function. (i) (6 pts.) SOFTWARE Ph.D. Qualifying Exam Spring 2017 Consider the following C program, which includes three function definitions, including the main function. #include #include

More information

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 2) Be extra careful with pointers! Arrays and Pointers (part 2) EECS 2031 22 October 2017 1 Be extra careful with pointers! Common errors: l Overruns and underruns Occurs when you reference a memory beyond what you allocated. l Uninitialized

More information

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 2) Be extra careful with pointers! Arrays and Pointers (part 2) CSE 2031 Fall 2011 23 October 2011 1 Be extra careful with pointers! Common errors: Overruns and underruns Occurs when you reference a memory beyond what you allocated. Uninitialized

More information

Signals, Instruments, and Systems W2. C Programming (continued)

Signals, Instruments, and Systems W2. C Programming (continued) Signals, Instruments, and Systems W2 C Programming (continued) 1 Resources 2 Remember man? You can use man to show information about functions in the standard libraries of C: e.g. man printf or man atan2

More information

C programming for beginners

C programming for beginners C programming for beginners Lesson 2 December 10, 2008 (Medical Physics Group, UNED) C basics Lesson 2 1 / 11 Main task What are the values of c that hold bounded? x n+1 = x n2 + c (x ; c C) (Medical Physics

More information

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 { Memory A bit is a binary digit, either 0 or 1. A byte is eight bits, and can thus represent 256 unique values, such as 00000000 and 10010110. Computer scientists often think in terms of hexadecimal, rather

More information

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

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor

More information

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

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

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee. Midterm Exam Nov 8th, 2012 COMS W3157 Advanced Programming Columbia University Fall 2012 Instructor: Jae Woo Lee About this exam: - There are 4 problems totaling 100 points: problem 1: 30 points problem

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

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1 Roy Chan CSC2100B Data Structures Tutorial 1 January 12, 2009 1 / 38 1 Information Your TA team Course Information Assignment 2 Online Judge Writing Your Assignment Program Submitting Your Program Online

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

CSC 270 Survey of Programming Languages. Input and Output

CSC 270 Survey of Programming Languages. Input and Output CSC 270 Survey of Programming Languages C Lecture 8 Input and Output Input and Output C supports 2 different I/O libraries: buffered (higher level functions supported by ANSI standards) unbuffered (lower-level

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

Kurt Schmidt. October 30, 2018

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

More information

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. .. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. A Simple Program. simple.c: Basics of C /* CPE 101 Fall 2008 */ /* Alex Dekhtyar */ /* A simple program */ /* This is a comment!

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

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

16.216: ECE Application Programming Fall 2011

16.216: ECE Application Programming Fall 2011 16.216: ECE Application Programming Fall 2011 Exam 2 Solution 1. (24 points, 6 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ. C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,

More information

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009 Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional

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

High Performance Programming Programming in C part 1

High Performance Programming Programming in C part 1 High Performance Programming Programming in C part 1 Anastasia Kruchinina Uppsala University, Sweden April 18, 2017 HPP 1 / 53 C is designed on a way to provide a full control of the computer. C is the

More information

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME CLOSED BOOK Exam #1 100 Points NAME 1. The following program has (at least) 10 syntax errors. Circle each error. Write the corrected program in the blank space below. 2 points for each error you find.

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

BİL200 TUTORIAL-EXERCISES Objective:

BİL200 TUTORIAL-EXERCISES Objective: Objective: The purpose of this tutorial is learning the usage of -preprocessors -header files -printf(), scanf(), gets() functions -logic operators and conditional cases A preprocessor is a program that

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

CS 261 Data Structures. Introduction to C Programming

CS 261 Data Structures. Introduction to C Programming CS 261 Data Structures Introduction to C Programming Why C? C is a lower level, imperative language C makes it easier to focus on important concepts for this class, including memory allocation execution

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

CSC 270 Survey of Programming Languages. What is a Pointer?

CSC 270 Survey of Programming Languages. What is a Pointer? CSC 270 Survey of Programming Languages C Lecture 6 Pointers and Dynamic Arrays What is a Pointer? A pointer is the address in memory of a variable. We call it a pointer because we envision the address

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

Syntax Analysis Part VIII

Syntax Analysis Part VIII Syntax Analysis Part VIII Exercises: Bison Text adapted from : Marinella Sciortino, Università di Palermo Exercise I Write an interpreter for hand calculator with the following operators +, - (binary)

More information

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

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

More information

Programming in C UVic SEng 265

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

More information

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following: CS360 Midterm 1 - February 21, 2017 - James S. Plank Put all answers on the answer sheet. In all of these questions, please assume the following: Pointers and longs are 4 bytes. The machine is little endian

More information

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action; Chapter 13 Control Structures Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Control Structures! Conditional n making a decision

More information

Lab 1: Introduction to C Programming

Lab 1: Introduction to C Programming CS342 Computer Security Handout # 2 Prof. Lyn Turbak September 13, 2010 Wellesley College Lab 1: Introduction to C Programming Reading: Hacking, 0x210 0x240 Overview Later in the course, we will study

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

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

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

More information

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

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Structure: - header files - global / local variables - main() - macro Basic Units: - basic data types - arithmetic / logical / bit operators

More information

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the

More information

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable. CMPS 12M Introduction to Data Structures Lab Lab Assignment 3 The purpose of this lab assignment is to introduce the C programming language, including standard input-output functions, command line arguments,

More information

Computer Programming Unit 3

Computer Programming Unit 3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very

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

Structured programming

Structured programming Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 12: Memory, Files and Bitoperations (pronobis@kth.se) Overview Overview Lecture 12: Memory, Files and Bit operations Wrap Up Main function; reading and writing Bitwise Operations Wrap Up Lecture

More information

CSE 333 Lecture 7 - final C details

CSE 333 Lecture 7 - final C details CSE 333 Lecture 7 - final C details Steve Gribble Department of Computer Science & Engineering University of Washington Today s topics: - a few final C details header guards and other preprocessor tricks

More information

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Spring 2018 COP 3223 Introduction to Programming with C - Study Union - Spring 2018 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

More information

Arrays, Strings, and Pointers

Arrays, Strings, and Pointers Arrays, Strings, and Pointers Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague Lecture 04 BE5B99CPL C Programming Language Jan Faigl, 2017

More information

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut mith College CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 2 Hours D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard

More information

BSM540 Basics of C Language

BSM540 Basics of C Language BSM540 Basics of C Language Chapter 9: Functions I Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture Introduce the switch and goto statements Introduce the arrays in C

More information

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow Variables Integer Representation Variables are used to store a value. The value a variable holds may change over its lifetime. At any point in time a variable stores one value (except quantum computers!)

More information

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 1. Do a page check: you should have 8 pages including this cover sheet. 2. You have 50 minutes

More information

Compiling and Running a C Program in Unix

Compiling and Running a C Program in Unix CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name

More information

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations Reminder of midterm 1 Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations A New Example to see effect of E++ (better than the one in previous lecture) Purpose of showing

More information

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

Contents. A Review of C language. Visual C Visual C++ 6.0 A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source

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

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

UNIT III (PART-II) & UNIT IV(PART-I)

UNIT III (PART-II) & UNIT IV(PART-I) UNIT III (PART-II) & UNIT IV(PART-I) Function: it is defined as self contained block of code to perform a task. Functions can be categorized to system-defined functions and user-defined functions. System

More information

Procedures, Parameters, Values and Variables. Steven R. Bagley

Procedures, Parameters, Values and Variables. Steven R. Bagley Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

More information

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014) CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014) Goals Demonstrate proficiency in the use of the switch construct and in processing parameter data passed to a program via the command

More information

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm Name: Umail Address: @ umail.ucsb.edu Circle Lab section: 9am 10am 11am noon (Link to Printer Friendly-PDF

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

Engineering program development 6. Edited by Péter Vass

Engineering program development 6. Edited by Péter Vass Engineering program development 6 Edited by Péter Vass Variables When we define a variable with its identifier (name) and type in the source code, it will result the reservation of some memory space for

More information

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics {C Programming Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics Variables A variable is a container (storage area) to hold data. Eg. Variable name int potionstrength = 95; Variable

More information

int main() { return 0; }

int main() { return 0; } int main() { return 0; % gcc x.c %./a.out % gcc -o x x.c %./x int main() { return 1; 0 % gcc -Wall -o x x.c %./x int main() { printf("hi\n"); return 0; \n #include int main() { printf("hi\n");

More information