Computer Programming: 8th Week Pointers

Size: px
Start display at page:

Download "Computer Programming: 8th Week Pointers"

Transcription

1 Computer Programming: 8th Week Pointers Hazırlayan Asst. Prof. Dr. Tansu Filik

2 Computer Programming Previously on Bil-200 Recursive functions, two way communications with functions (reference)

3 Meanings of * Multiplication: Between two numbers, it is the multiplication operator. a * b Pointer: Corresponds to the pointer type variable. char *p; Pointer: Corresponds to the value of the variable pointed to by the pointer. *p = 5;

4 Computer Programming Pointers int *p1 /*Pointer to an integer variable*/ double *p2 /*Pointer to a variable of data type double*/ char *p3 /*Pointer to a character variable*/ float *p4 /*pointer to a float variable*/ Example-1 int main() { /*Pointer of integer type*/ int *p; int var = 10; /*Assigning the variable address to pointer*/ p= &var; printf("value of var is: %d", var); printf("address of var is: %d", p); return 0; Declerations #include <stdio.h> int main() { int var =10; int *p; p= &var; printf ( "\n Address of var is: %d", &var); printf ( "\n Address of var is: %d", p); printf ( "\n Address of pointer p is: %d", &p); printf( "\n Value of pointer p is: %d", p); printf ( "\n Value of var is: %d", var); printf ( "\n Value of var is: %d", *p); printf ( "\n Value of var is: %d", *( &var)); return 0; Address of var is: 00XBBA77 Address of var is: 00XBBA77 Address of pointer p is: Value of pointer p is: 00XBBA77 Value of var is: 10 Value of var is: 10 Value of var is: 10

5 Example: Date Write a function to enter a date in the "dd/mm/yyyy" form. If the entered date s invalid, the function must warn by writing invalid and requesting a proper date. void input_date(int *gun, int *ay, int *yil);

6 Example: Date void input_dat(int *gun, int *ay, int *yil) { int gecerli; char ayrac1, ayrac2; do { gecerli = 1; printf("tarih girin(gg aa yyyy): "); scanf("%d %d %d", gun, ay, yil); if ( *gun < 0 *gun > 31 *ay< 1 *ay > 12 *yil < 0 *yil > 9999) { gecerli = 0; printf("tarih yanlis\n"); while (!gecerli);

7 Example: Date void input_date(int *gun, int *ay, int *yil) { gun is a pointer variable. It contains the the address of the variable.... scanf("%d %d %d", gun, ay, yil);... Why didn t we use &? The & sign is used for accessing the address of the variable. The gun variable is already an address.

8 Example: Polynomial roots Write a function which takes three float type variables a, b, c from the main program, and returns the real roots of the second order polynomail identified by these coefficients. The function will have three input and two output parameters. input parameters void kok_bul(float a, float b, float c, float &x1, float &x2); output parameters

9 Example: Polynomial roots #include <stdio.h> void kok_bul(float a,float b,float c, float *x1, float *x2); void main() { float x, y, z, k1, k2; printf("a b c girin: "); scanf("%f %f %f", &x, &y, &z); kok_bul(x, y, z, &k1, &k2); prototype values addresses printf("kokler: %f %f\n", k1, k2);

10 Example: Polynomial roots void kok_bul(float a, float b, float c, float *x1, float *x2) { float delta; delta = b * b 4 * a * c; if(delta < 0) { *x1 = 0; *x2 = 0; return; *x1 = (-b + sqrt(delta)) / ( 2 * a ); *x2 = (-b - sqrt(delta)) / ( 2 * a );

11 Introduction to Programming Languages #include <stdio.h> #include <math.h> void kok_bul(double a, double b, double c, double *x1, double *x2); void main(void) { double x, y, z, k1, k2; printf("a - b - c girin: "); scanf_s("%lf %lf %lf", &x, &y, &z); kok_bul(x, y, z, &k1, &k2); printf("kokler: %f %f\n", k1, k2); system("pause"); void kok_bul(double a, double b, double c, double *x1, double *x2) { double delta; delta = b * b - 4 * a * c; if (delta < 0) { *x1 = 0; *x2 = 0; return; *x1 = (-b + sqrt(delta)) / (2 * a); *x2 = (-b - sqrt(delta)) / (2 * a);

12 Class exercise: division Write a function bolum()with the following parameter list of integers: sayi: to be divided bolen: number to divide with sonuc: division result kalan: remainder The function must implement the integer division. It should divide sayi to bolen, and return the division in sonuc, remainder in kalan.

13 Character arrays We have been using them for a long time, but...

14 Character arrays character array printf("sayi=%d!\n", i, j); We have not seen character array type (string) variables yet, but we have been using them for some time. They are defined like: char isim[100];

15 Strings Like other arrays, initial assignments can be made: char isim[20] = mehmet"; In memory null character (terminates array) isim m e h m e t \0????????????? space allocated for the array

16 Strings In the declaration and initial assignment line, the compiler terminates the string with the null character. There is a standard string library in C. The defined functions work with the fact that end of string is a null character. The characters to its right are ignored. Since a null character is necessary to terminate a string, you must allocate a space at least 1 more than the maximum number of letters. For example, by saying "char isim[20];" we can hold names of length up to 19 (+ null character).

17 String declarations String can be initialized in several ways. char renk[ ] = "kırmızı" ; OR char *renk = "kırmızı" ; OR char renk[8] = {'k','ı','r','m','ı','z','ı','\0' ; In the first two methods, the necessary memory space is calculated according to the initial constant string (+1) In the third declaration, we must allocate sufficient space carefully.

18 void getstring(char *str, int max_len) { int i=0; do { str[i]=getche(); /* conio.h */ i++; while((str[i-1]!= \n )&&(i<max_len-1)); str[i-1]= \0 ; void main(void) { char my_string[100]; getstring( &( my_string [0]),100); printf( %s\n,my_string);

19 Exercise Write a function which converts an entered string into an integer assuming the entered string is proper. a float, assuming the entered string is proper. Write the two functions in such a way that they ignore improper entries. As examples, the strings at the left would be recognized as numbers at the right: 123se24 => 12324, ser.43 => Note: Functions such as i=atoi(str), or x=atof(str) can normally do this, but they are not allowed. They do not tolerate improper entries. Note: sscanf(str, %d,&i) or sscanf(str, %f,&x) are not allowed, either.

20 Note: sscanf and sprintf are two very powerful functions used for extracting variables from a string or to form a string with many variables. : i=10; j=20; x=3.14; char *str2= 101, olabilir ; sprintf(str, Degisken i=%d, j=%d, x=%f,i,j,x); printf( %s\n,str); sscanf(str2, %d,%d %f,&i,&j,%x);

21 String manipulation functions string.h must be included as a header. String copy: Used for copying contents of the second string str2 into the first string str1: char *strcpy(char *str1,const char *str2); String concatenation: str2 is appended to the last part of str1 char *strcat(char *str1,const char *str2);

22 String functions Length of a string str (except the null character) is evaluated. int strlen(const char *str1); To lexicographically compare to strings, use strcmp(). If str1 and str2 have the same content, strcmp() returns 0. strcmp(const char *str1,const char *str2);

23 #include <stdio.h> #include <string.h> void main() { char ad[100]; Example strcmp() printf("adınızı Yazınız: "); gets(ad); /* read one line */ if(strcmp(ad, Mehmet") == 0) { printf( Selam Mehmet");

24 Example: strlen #include <stdio.h> #include <string.h> void main() { char ad[100]; printf("adinizi Yazınız: "); gets(ad); /* read one line */ printf("ad Uzunluğu = %d\n",strlen(ad));

25 Exercise (difficult) Open a text file. Ask the user to enter two strings. Replace all first strings with the second string inside the file (find / replace) Note: Assuming the file is small, you can read the whole text file into a single string. But, you do not have to do that. The usage is like: Ali topu at, koş Ali koş. Ali -> Veli Veli topu at, koş Veli koş. (or) Ali -> O O topu at, koş O koş.

26 Midterm Exam-2 Questions ( )

27 Midterm Exam-2 Questions ( )

28 Midterm Exam-2 Questions ( )

29 static and automatic variables, and scope

30 Rem: Static variables We have seen that local variables are lost when exitting a function. Static variables are re-formed every time when entering the same function. They are different from automatic variables. These variables are formed at the beginning of the program, and their values are set. Obviously, they are valid only inside its declared function.

31 void fonk_yaz(void) { int a = 0; printf("deger = %d\n", a); a++; void main() { int i; for( i = 0 ; i < 3 ; i++ ) { fonk_yaz(); Automatic variables A new a is formed for each enterence to the function. This is the default way.

32 Example: without using static void fonk_yaz(void) { int a = 0; printf("deger = %d\n", a); a++; void main() { int i; A new a variable is formed at each enterence to the function. The variable a is destroyed whenever we quit the function. for( i = 0 ; i < 3 ; i++ ) { fonk_yaz(); So, on the screen, we would see three 0 (zero) values.

33 void fonk_yaz(void) { static int a = 0; Example re-formed every time when entering the same function printf("deger = %d\n", a); a++; void main() { int i; On the screen, you see deger = 0 deger = 1 deger = 2 for( i = 0 ; i < 3 ; i++ ) { fonk_yaz();

34 void fonk_yaz(void) { static int a = 0; Example The variable a is formed at the beginning of the program, and its value is kept. printf("deger = %d\n", a); a++; void main() { int i; The variable is not destroyed when quitting the program. for( i = 0 ; i < 3 ; i++ ) { fonk_yaz();

35 Enumerated types variables that are formed by the keyword: enum

36 Enumerated types Gives you a chance to form your own type of variables. Its usage improves intelligibility of the program. For example, we may create a type to contain week days: enum Gunler { Pazartesi, Sali, Carsamba, Persembe, Cuma, Cumartesi, Pazar ;

37 Enumerated types We may make variables of the created type enum Gunler gun; switch(gun) { case Pazartesi: printf("pazartesi"); break; case Sali: printf("sali"); break;...

38 Enumerated Types Actually, each element in an enum type is a number list starting from 0. enum Gunler { Pazartesi, /* 0 */ Sali, /* 1 */ Carsamba, /* 2 */ Persembe, /* 3 */ Cuma, /* 4 */ Cumartesi, /* 5 */ Pazar /* 6 */ ;

39 Enumerated Types Since enum elements are numerical, they can be used in comparisons of if expressions. enum Gunler gun1, gun2; gun1 = (enum Gunler) 2; /* carsamba */ gun2 = "Pazartesi"; if(gun2 < gun1) { printf("gun2 daha onceki gun\n");

40 Scope How long do the variables stay valid?

41 Scope of Variables The variable scope is defined as the program segment in which the variable can be used. The variable name is an alias for the address of the variable in the memory. For example, if the variable is defined right after "void main {", then it is valid only in main(). It cannot be used anywhere else.

42 Scope of Variables If a variable is defined inside a block ( { and ), then it is valid until the end of block. If the variable is defined before all the functions, they are called global variables, and they are valid throughout the whole file.

43 Scope of Variables No two variables may have the same name if they are defined in the same scope. In different scopes, you can use variables with the same name. Since they correspond to different memory places, there will be no ambiguity. Yet, try to avoid using the same variable names under any circumstances.

44 Example - 1 x variable is not defined yet. Compile error!!! #include <stdio.h> void main ( ) { if ( x == 3 ) { float x = 4.5; x was only in the block, and now not defined any more... printf ( "blok içinde x=%f", x); printf( "bloktan sonra x=%d\n", x);

45 Example - 2 x variable is defined. OK!! #include <stdio.h> void main ( ) { int x = 3; Here, the nearestly defined one is valid. if ( x == 3 ) { float x = 4.5; printf ( "blok içinde x=%f", x); printf( bloktan sonra x=%d\n", x);

46 Scope of Variables In the previous example, there is an x variable defined right after main(). But in if block, there is another variable called x. Inside the block, that new variable will be valid, and the outer x is not valid. Since the two variables correspond to two different memory addresses, they will not be confused.

47 Would they? Run the program and see the following output: blok içinde x=4.5 bloktan sonra x=3

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Computer Programming Previously on Bil 200 Introduction to pointers String (character array) Static and automatic variables static and automatic variables, and scope Rem: Static

More information

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Computer Programming Previously on Bil 200 Midterm Exam - 1 Midterm Exam - 1 126 students Curve: 49,78 Computer Programming Arrays Arrays List of variables: [ ] Computer Programming

More information

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil 200 Low-Level I/O getchar, putchar,

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

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Previously on Bil 200 Low-Level I/O getchar, putchar, getc, putc Control Structures if-else, switch-case, loops (while, dowhile, for loops) for loop (generic) A disturbingly

More information

Computer Programming 3 th Week Variables, constant, and expressions

Computer Programming 3 th Week Variables, constant, and expressions 3 th Week Variables, constant, and expressions Hazırlayan Asst. Prof. Dr. Tansu Filik Previously on Bil 200 Unitary operations (applied to single variable, +.-,++,--) Type casting Operation/Operator precedence

More information

Fundamentals of Programming Session 12

Fundamentals of Programming Session 12 Fundamentals of Programming Session 12 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Outline Review pointers New: Strings New: Variable Scope (global vs. local variables)

More information

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University SYSC 2006 C Winter 2012 String Processing in C D.L. Bailey, Systems and Computer Engineering, Carleton University References Hanly & Koffman, Chapter 9 Some examples adapted from code in The C Programming

More information

Pointers, Arrays, and Strings. CS449 Spring 2016

Pointers, Arrays, and Strings. CS449 Spring 2016 Pointers, Arrays, and Strings CS449 Spring 2016 Pointers Pointers are important. Pointers are fun! Pointers Every variable in your program has a memory location. This location can be accessed using & operator.

More information

Computer Programming: Skills & Concepts (CP) Strings

Computer Programming: Skills & Concepts (CP) Strings CP 14 slide 1 Tuesday 31 October 2017 Computer Programming: Skills & Concepts (CP) Strings Ajitha Rajan Tuesday 31 October 2017 Last lecture Input handling char CP 14 slide 2 Tuesday 31 October 2017 Today

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Intermediate Programming, Spring 2017*

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

More information

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference

More information

CSC 2400: Computer Systems. Arrays and Strings in C

CSC 2400: Computer Systems. Arrays and Strings in C CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays: C vs.

More information

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly

More information

CSC 2400: Computer Systems. Arrays and Strings in C

CSC 2400: Computer Systems. Arrays and Strings in C CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays in C

More information

Computer Programming 6th Week Functions (Function definition, function calls),

Computer Programming 6th Week Functions (Function definition, function calls), Computer Programming 6th Week Functions (Function definition, function calls), Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil-200 loops (do-while, for), Arrays, array operations,

More information

BSM540 Basics of C Language

BSM540 Basics of C Language BSM540 Basics of C Language Chapter 4: Character strings & formatted I/O Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture To explain the input/output functions printf()

More information

School of Computer Science Algorithms & Programming. Fall Midterm Examination # 2 Wednesday, November 14, 2007.

School of Computer Science Algorithms & Programming. Fall Midterm Examination # 2 Wednesday, November 14, 2007. School of Computer Science 60-141-01 Algorithms & Programming Fall 2007 Midterm Examination # 2 Wednesday, November 14, 2007 Marking Scheme Duration of examination: 75 minutes 1. Answer all questions on

More information

Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers

Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers Hazırlayan Asst. Prof. Dr. Tansu Filik Introduction to Programming Languages Previously on Bil-200 Functions: Function

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

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

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Bil 200 Week -2 Previously on Bil 200 Programming Languages (Machine Code, Assembly code, high-level languages) Compiler (translator) Software development (components of algorithm)

More information

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7 Strings and Clases BIL104E: Introduction to Scientific and Engineering Computing Lecture 7 Manipulating Strings Scope and Storage Classes in C Strings Declaring a string The length of a string Copying

More information

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSC209H Lecture 4. Dan Zingaro. January 28, 2015 CSC209H Lecture 4 Dan Zingaro January 28, 2015 Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a

More information

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction. Chapter 9 Scanning Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Stephen Scott (Adapted from Christopher M. Bourke) Scanning 9.1 String 9.2 Functions: Assignment

More information

Characters and Strings

Characters and Strings Characters and Strings 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Character constants A character in single quotes,

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Scanning Stephen Scott (Adapted from Christopher M. Bourke) 1 / 51 Fall 2009 cbourke@cse.unl.edu Chapter 9 Scanning

More information

BLM2031 Structured Programming. Zeyneb KURT

BLM2031 Structured Programming. Zeyneb KURT BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Stephen Scott (Adapted from Christopher M. Bourke) 1 / 51 Fall 2009 Chapter 9 9.1 String 9.2 Functions: Assignment

More information

8. Characters, Strings and Files

8. Characters, Strings and Files REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 4 CMPE 013/L Pointers and Functions Gabriel Hugh Elkaim Spring 2013 Pointers and Functions Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

M1-R4: Programing and Problem Solving using C (JAN 2019)

M1-R4: Programing and Problem Solving using C (JAN 2019) M1-R4: Programing and Problem Solving using C (JAN 2019) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter

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

Full file at C How to Program, 6/e Multiple Choice Test Bank

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

ECE551 Midterm Version 1

ECE551 Midterm Version 1 Name: ECE551 Midterm Version 1 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual

More information

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:

More information

C introduction: part 1

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

More information

Create a Program in C (Last Class)

Create a Program in C (Last Class) Create a Program in C (Last Class) Input: three floating point numbers Output: the average of those three numbers Use: scanf to get the input printf to show the result a function to calculate the average

More information

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre Programming in C Session 7 Seema Sirpal Delhi University Computer Centre Relationship between Pointers & Arrays In some cases, a pointer can be used as a convenient way to access or manipulate the data

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 9 Principles of C and Memory Management Dr Lei Shi Last Lecture Today Pointer to Array Pointer Arithmetic Pointer with Functions struct Storage classes typedef union String struct struct

More information

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan. Functions Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 5.1 Introduction 5.3 Math Library Functions 5.4 Functions 5.5

More information

Chapter 1 & 2 Introduction to C Language

Chapter 1 & 2 Introduction to C Language 1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

More information

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci

More information

Functions. Systems Programming Concepts

Functions. Systems Programming Concepts Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value

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

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

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

Chapter 8 C Characters and Strings

Chapter 8 C Characters and Strings Chapter 8 C Characters and Strings Objectives of This Chapter To use the functions of the character handling library (). To use the string conversion functions of the general utilities library

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4 BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions

More information

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Previously on Bil 200 Operators Logical and arithmetic operators Control Structures if-else mechanism Low-level I/O They are fast! Might be useful for console applications.

More information

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17 Grade Distribution Exam 1 Exam 2 Score # of Students Score # of Students 16 4 14 6 12 4 10 2 8 1 Total: 17 Exams 1 & 2 14 2 12 4 10 5 8 5 4 1 Total: 17 Score # of Students 28 2 26 5 24 1 22 4 20 3 18 2

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

ALGORITHM 2-1 Solution for Exercise 4

ALGORITHM 2-1 Solution for Exercise 4 Chapter 2 Recursion Exercises 1. a. 3 * 4 = 12 b. (2 * (2 * fun1(0) + 7) + 7) = (2 * (2 * (3 * 0) + 7) + 7) = 21 c. (2 * (2 * fun1(2) + 7) + 7) = (2 * (2 * (3 * 2) + 7) + 7) = 45 2. a. 3 b. (fun2(2, 6)

More information

C-LANGUAGE CURRICULAM

C-LANGUAGE CURRICULAM C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals

More information

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 16 January Takako Nemoto (JAIST) 16 January 1 / 15 Strings and pointers #include //11-1.c char str[] = "ABC"; char *ptr = "123"; printf("str = \"%s\"\n",

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

CSE123. Program Design and Modular Programming Functions 1-1

CSE123. Program Design and Modular Programming Functions 1-1 CSE123 Program Design and Modular Programming Functions 1-1 5.1 Introduction A function in C is a small sub-program performs a particular task, supports the concept of modular programming design techniques.

More information

Procedural Programming

Procedural Programming Exercise 5 (SS 2016) 28.06.2016 What will I learn in the 5. exercise Strings (and a little bit about pointer) String functions in strings.h Files Exercise(s) 1 Home exercise 4 (3 points) Write a program

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

Procedural Programming & Fundamentals of Programming

Procedural Programming & Fundamentals of Programming Procedural Programming & Fundamentals of Programming Exercise 3 (SS 2018) 29.05.2018 What will I learn in the 4. exercise Pointer (and a little bit about memory allocation) Structure Strings and String

More information

Chapter 2: Overview of C. Problem Solving & Program Design in C

Chapter 2: Overview of C. Problem Solving & Program Design in C Chapter 2: Overview of C Problem Solving & Program Design in C Addison Wesley is an imprint of Why Learn C? Compact, fast, and powerful High-level Language Standard for program development (wide acceptance)

More information

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook Chapter 5 Section 5.4 The Common String Library Functions CS 50 Hathairat Rattanasook Library Functions We already discussed the library function fgets() Library functions are available: to find the length

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

ECE551 Midterm Version 2

ECE551 Midterm Version 2 Name: ECE551 Midterm Version 2 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

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

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions CMPE-013/L Gabriel Hugh Elkaim Winter 2014 Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by value) int x = 2, y = 0; Value of variable passed to function

More information

C Programming Language Review and Dissection III

C Programming Language Review and Dissection III C Programming Language Review and Dissection III Lecture 5 Embedded Systems 5-1 Today Pointers Strings Formatted Text Output Reading Assignment: Patt & Patel Pointers and Arrays Chapter 16 in 2 nd edition

More information

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ; INPUT AND OUTPUT IN C Function: printf() library: sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ; format_string it is

More information

CS1100 Introduction to Programming

CS1100 Introduction to Programming CS1100 Introduction to Programming Sorting Strings and Pointers Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Lexicographic (Dictionary) Ordering Badri

More information

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science Functions in C C Programming and Software Tools N.C. State Department of Computer Science Functions in C Functions are also called subroutines or procedures One part of a program calls (or invokes the

More information

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach C Fundamentals & Formatted Input/Output adopted from KNK C Programming : A Modern Approach C Fundamentals 2 Program: Printing a Pun The file name doesn t matter, but the.c extension is often required.

More information

JTSK Programming in C II C-Lab II. Lecture 3 & 4

JTSK Programming in C II C-Lab II. Lecture 3 & 4 JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically

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

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming & Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Seven: Strings and Files Name: First Name: Tutor: Matriculation-Number: Group-Number:

More information

Function Call Stack and Activation Records

Function Call Stack and Activation Records 71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students

More information

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1 Dynamic Memory Dynamic Memory Allocation Strings September 18, 2017 Hassan Khosravi / Geoffrey Tien 1 Pointer arithmetic If we know the address of the first element of an array, we can compute the addresses

More information

ECE 551D Spring 2018 Midterm Exam

ECE 551D Spring 2018 Midterm Exam Name: SOLUTIONS ECE 551D Spring 2018 Midterm Exam NetID: There are 6 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam

More information

ECE 551D Spring 2018 Midterm Exam

ECE 551D Spring 2018 Midterm Exam Name: ECE 551D Spring 2018 Midterm Exam NetID: There are 6 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual

More information

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis

More information

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

More information

Variation of Pointers

Variation of Pointers Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before

More information

Programming and Data Structures

Programming and Data Structures Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur #include int main()

More information

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo SYSTEM AND LIBRARY CALLS UNIX Programming 2015 Fall by Euiseong Seo Now, It s Programming Time! System Call and Library Call Who does process the following functions? strcmp() gettimeofday() printf() getc()

More information

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world alun@debian:~$ gcc hello.c alun@debian:~$ a.out Hello, world alun@debian:~$ gcc -o hello hello.c alun@debian:~$ hello Hello, world alun@debian:~$ 1 A Quick guide to C for Networks and Operating Systems

More information

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, Strings Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

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

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER: Page 1 of 8 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2015 Midterm Examination # 2 Wednesday, March 18, 2015 ANSWERS Duration of examination: 75 minutes STUDENT

More information

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS CHARACTER STRINGS 1. Overview The learning objective of this lab session is to: Understand the internal representation of character strings Acquire skills in manipulating character strings with standard

More information

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017 CS 137 Part 6 ASCII, Characters, Strings and Unicode November 3rd, 2017 Characters Syntax char c; We ve already seen this briefly earlier in the term. In C, this is an 8-bit integer. The integer can be

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

Lecture 3. Variables. Variables

Lecture 3. Variables. Variables Lecture 3 Variables Variables Data processed by programs are input from keyboard by user, are read from the storage medium or are obtained by evaluating expressions. For this purpose it is necessary to

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

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