Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Size: px
Start display at page:

Download "Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh"

Transcription

1 Remember! Practice will make you perfect!!! :D Learning C Language For BEGINNERS The 6 th week / May 24 th, 26 Su-Jin Oh sujinohkor@gmail.com 1

2 Index Basics Operator Precedence Table and ASCII Code Table Bit Operators Constant Values Type Casting Functions and Scope Rules Pointers Pointer-1: Pointers Pointer-2: Pointers and Arrays Pointer-3: Pointers and Functions 2

3 Bit Operators, Constant Values, and Type Casting BASICS 3

4 ASCII Code Table IMPORTANT 0 A a ASCII (decimal)

5 Operator Precedence Table Primary Unary Dyadic 5

6 Bit Operators Operator Meaning Cases ~ Bitwise Complement ~0 1 ~1 0 << Bitwise Shift Left >> Bitwise Shift Right & Bitwise AND ^ Bitwise Exclusive OR Bitwise (Inclusive) OR The left operands value is moved left by the number of bits specified by the right operand. The left operands value is moved right by the number of bits specified by the right operand. 0 & & & & ^ ^ ^ ^ &: If both bits are 1, then the result is 1. ^: If both bits are different, then the result is 1. : If the either bit is 1, then the result is 1. 6

7 Constant Values 1/2 Constant - [NOUN] A constant is a thing or value that always stays the same. Literals Integer Literals: Integer numbers. e.g. 0, 11, 0xFF, 20u, 40l, 60ul Floating-point Literals: Real numbers. e.g. 0.1, 3.14, E-5L Character Literals: These are enclosed in single quotes. e.g. a, \n... String Literals: These are enclosed in double quotes. e.g. Hello, Hallym University - 11: octal - 20u: unsigned int - 40l: long - 60ul: unsigned long Defining Constants Using #define Preprocessor Using const Keyword 7

8 Constant Values 2/2 Using #define Preprocessor #include <stdio.h> #define WID 5 #define HEI 10 int area = WID * HEI; printf("width(%d) * height(%d) = area(%d)\n", WID, HEI, area); Using const Keyword #include <stdio.h> const int WID = 5; const int HEI = 10; int area = WID * HEI; printf("width(%d) * height(%d) = area(%d)\n", WID, HEI, area); 8

9 Type Casting Is this really necessary? YES 08 Then, how can we correct the above code? 08 #include <stdio.h> int a = 1, b = 2; float f = a / b; printf("%d / %d = %f\n", a, b, f); #include <stdio.h> int a = 1, b = 2; float f = (float)a / b; printf("%d / %d = %f\n", a, b, f); You may think that the result is 0.5. But!!!!!! The result is 0!!! 9

10 Functions, Call Types, Scope Rules, and Recursion FUNCTIONS AND SCOPE RULES 10

11 Functions #include <stdio.h> int sum(int num1, int num2); int max(int, int); Calling Functions printf(" = %d\n", sum(10, 5) ); printf("10 and 5, the larger one is %d\n", max(10, 5) ); int sum(int num1, int num2) { return num1+num2; int max(int num1, int num2) { int res; if(num1 > num2) res = num1; else res = num2; return res; Function Declarations Both are allowed. Defining Functions return_type function_name(parameter list) { function body

12 Call Types 1/3 Call by Value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. e.g. max(num1, num2); Call by Reference This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. e.g. swap(&x, &y); 12

13 Call Types 2/3; Call by Value #include <stdio.h> void swap(int x, int y); int a = 100, b = 200; printf("before swap, a is %d, b is %d\n", a, b); swap(a, b); printf("after swap, a is %d, b is %d\n", a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; return; The result will be Before swap, a is 100, b is 200 After swap, a is 100, b is

14 Call Types 3/3; Call by Reference #include <stdio.h> void swap(int *x, int *y); int a = 100, b = 200; printf("before swap, a is %d, b is %d\n", a, b); swap(&a, &b); printf("after swap, a is %d, b is %d\n", a, b); void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; return; The result will be Before swap, a is 100, b is 200 After swap, a is 200, b is

15 Scope Rules 1/ #include <stdio.h> int a = 100; void globala(); int a = 10; printf("the local variable a is %d\n", a); globala(); void globala() { printf("the global variable a is %d\n", a); Unlike local variables, global variables are automatically initialized by the system. Data Type Default Value The result will be The local variable a is 10 The global variable a is 100 int char float double pointer 0 \0 (NULL) 0 0 NULL 15

16 Scope Rules 2/ #include <stdio.h> void locala(); void statica(); locala(); statica(); locala(); statica(); The result will be The local variable a is 10 The static variable a is 100 The local variable a is 10 The static variable a is 1 void locala() { int a = 10; printf("the local variable a is %d\n", a++); void statica() { static int a = 100; printf("the static variable a is %d\n", a++); 16

17 Recursion #include <stdio.h> int Factorial(int n); int Fibonacci(int n); int i; printf("5! is %d\n", Factorial(5)); printf("fib(5) is [ "); for(i=0; i<=5; i++) printf("%d ", Fibonacci(i)); printf("]\n"); int Factorial(int n) { if(n == 0) return 1; else return n * Factorial(n-1); int Fibonacci(int n) { The result will be 5! is 120 Fib(5) is [ ] if(n < 2) return n; else return Fibonacci(n-1) + Fibonacci(n-2); 17

18 POINTER-1 POINTERS 18

19 Memory Space When we make some variables, then 19 char c = a ; int i = 7; double d = 3.14; 0x1000 c = a 0x10 i = 7 0x10 0x10 0x10 0x10 d=3.14 0x10 0x10 0x1008 0x1009 0x100A 0x100B 0x100C 0x100D 0x100E adding...

20 What are Pointers? When we make a pointer, then int i = 100; int *pi = &i; Below is invalid! int *p = 100; 0x1000 i = 100 0x10 0x10 0x10 0x x2000 0x20 0x20 0x20 0x20 0x100C adding pi = 0x A pointer variable is that stores the memory address of another variable. The size of a pointer variable is fixed regardless of its type. (x86: 4-byte / x64: 8-byte) The type of a pointer indicates the size of the referenced memory space. (If there is no type in pointers, then computers cannot know how many memory spaces they should access.) 20

21 The Operator Sizeof() char c = 'a', *pc = &c; int i = 10, *pi = &i; double d = 3.14, *pd = &d; printf("sizeof(pc):%d\n", sizeof(pc)); printf("sizeof(*pc):%d\n", sizeof(*pc)); printf("sizeof(pi):%d\n", sizeof(pi)); printf("sizeof(*pi):%d\n", sizeof(*pi)); printf("sizeof(pd):%d\n", sizeof(pd)); printf("sizeof(*pd):%d\n", sizeof(*pd)); Can you explain this code? 21

22 * and & Operators Case-1 Case-2 int i = 100; int *pi = &i; int i = 100; int *pi = &i; This instruction makes a pointer variable. The pointer pi points out the variable i s address. The value of the memory which is indicated by the variable pi. The variable pi s address printf( pi:%x / *pi:%d / &pi:%x\n, pi, *pi, &pi); printf( i:%d / &i:%x\n, i, &i); The variable i s address 0x1000 i 0x2000 pi 100 0x

23 What If We Compile Below Codes? Case-1 int i = 100, *pi = &i; printf( *pi:%d\n, *pi); The ++ operator has precedence over the * operator. (*pi)++; printf( *pi:%d\n, *pi); Case-2 23 int n = 100, *p1 = &n, *p2 = p1; printf("%d\n", (*p1)++); printf("%d\n", (*p2)++); printf("%d\n", n); The result will be p1 0x1000 n p x1000 0x1000

24 POINTER-2 POINTERS AND ARRAYS 24

25 Arrays int arr[5] = { 10, 20, 30, 40, 50 ; An array s name means the 1 st element s address of the array x1000 0x10 0x1008 0x100C 0x arr[0] arr[1] arr[2] arr[3] arr[4] int arr[5] = {10,20,30,40,50; printf("arr:%x\n", arr); printf("&arr:%x\n", &arr); printf("&arr[0]:%x\n", &arr[0]); printf("&arr[1]:%x\n", &arr[1]); printf("*arr:%d\n", *arr); printf("*(arr+1):%d\n", *(arr+1)); The result will be

26 A Pointer to an Array Case-1 Case int arr[5] = {10,20,30,40,50; int *p = arr; printf("p[0]:%d, p[1]:%d\n", p[0], p[1]); int arr[5] = {10,20,30,40,50; int *p = arr; printf("%d\n", *p); printf("%d\n", *(++p)); printf("%d\n", *(++p)); printf("%d\n", *(p+1)); printf("%d\n", *(p+2)); The result will be p[0]:10, p[1]:20 The result will be

27 arr[i] is the Same as *(arr+i) int arr[5] = {10, 20, 30, 40, 50; int *p = arr; The result will be arr[0]:10, *(arr+1):20 p[0]:10, *(p+1):20 printf("arr[0]:%d, *(arr+1):%d\n", arr[0], *(arr+1)); printf("p[0]:%d, *(p+1):%d\n", p[0], *(p+1)); Using an Array Using a Pointer arr[0] arr[1] arr[2] arr[3] arr[4] *(arr+0) *(arr+1) *(arr+2) *(arr+3) *(arr+4) p[0] p[1] p[2] p[3] p[4] *(p+0) *(p+1) *(p+2) *(p+3) *(p+4) 27

28 Practices Practice-1 Practice int arr[5] = {10,20,30,40,50; int *p = arr; The result will be arr[0]:10, *(arr+1):20 p[0]:10, *(p+1):20 printf("p[0]:%d, p[1]:%d\n", p[0], p[1]); int arr[5] = {10, 20, 30, 40, 50; int *p = arr, i, temp; for(i = 0; i <4-i; i++) { temp = p[i]; p[i] = p[4-i]; p[4-i] = temp; The result will be for(i = 0; i < 5; i++) p[0]:50 p[1]:40 p[2]:30 p[3]:20 p[4]:10 printf("p[%d]:%d\t", i, p[i]); printf("\n"); 28

29 Pointers and Strings Both are similar. However, actually they are different!!! 1. char str1[6] = hello ; 2. char *str2 = HELLO ; 1. A String Variable h e l l o \0 str1[0] str1[1] str1[2] str1[3] str1[4] str1[5] str1[1] = a ; str2[1] = A ; // ERROR!!! Why the 2 nd statement makes an error? 2. A Pointer Variable 0x2000 str2 Try it!!! Both are the same! 0x2000 HELLO\0 printf("str1:%s\n", str1); printf("str2:%s\n", str2); printf( str2:%x\n, str2); printf("&str2:%x\n", &str2); printf("&(*str2):%x\n", &(*str2)); 29

30 A String Constant char *str = HELLO ; 0x2000 str 0x2000 HELLO\0 What if we initialize two pointer variables as the same string? 0x2000 str1 0x2000 str2 0x2000 HELLO\0 These are the same! printf("str1:%x\n", str1); printf("str2:%x\n", str2); printf("*str1:%c\n", *str1); printf("*str2:%c\n", *str2); printf("*(str1+1):%c\n", *(str1+1)); printf("*(str2+1):%c\n", *(str2+1)); printf("&str1:%x\n", &str1); printf("&str2:%x\n", &str2); printf("&(*str1):%x\n", &(*str1)); printf("&(*str2):%x\n", &(*str2)); 30

31 An Array of Pointers Case-1 Case int a = 10, b = 20; int *arr[2] = {&a, &b; printf("*arr[0]:%d\n", *arr[0]); printf("*arr[1]:%d\n", *arr[1]); char *p[2] = { "Hallym University, Korea", "Su-Jin Oh" ; printf("p[0]:%s\n", p[0]); printf("*(p+1):%s\n", *(p+1)); printf("*p[0]:%c\n", *p[0]); printf("*(p[0]+1):%c\n", *(p[0]+1)); The result will be *arr[0]:10 *arr[1]:20 The result will be p[0]:hallym University, Korea *(p+1):su-jin Oh *p[0]:h *(p[0]+1):a 31

32 POINTER-3 POINTERS AND FUNCTIONS 32

33 Passing Pointers/Arrays to Functions #include <stdio.h> void mul(int *arr, int len); void div(int arr[], int len); int arr[] = {10, 20, 30; mul(arr, sizeof(arr)/sizeof(arr[0])); printf("after mul: %d %d %d\n", arr[0], arr[1], arr[2]); div(arr, sizeof(arr)/sizeof(arr[0])); printf("after div: %d %d %d\n", arr[0], arr[1], arr[2]); void mul(int *arr, int len) { int i; for(i = 0; i < len; i++) arr[i] *= 10; void div(int arr[], int len) { int i; for(i = 0; i < len; i++) arr[i] /= 10; If you want to fix the length of this array, then you can write like int arr[10]. The operator sizeof() - When you use it to an array, then you can get the whole size of the array. - When you use it to a pointer variable, then you will get the size of a pointer variable. (x86: 4-byte / x64: 8-byte) When you declare a function s arguments, both int arr[] and int *arr are the same.

34 Return an Array from a Function #include <stdio.h> #define NUM 10 int *setvalues(void); int i, *p = setvalues(); printf("*(p + i): "); for(i = 0; i < NUM; i++) printf("%d ", *(p+i)); printf("\n"); To return the address of a function's local variable is not allowed in C. int *setvalues() { Thus, you need to define the key word static. int i; static int values[num]; printf("values[i]: "); for(i = 0; i < NUM; i++) { values[i] = i*10; printf("%d ", values[i]); printf("\n"); return values; If you delete this word, then the compiler may report a warning. warning: function returns address of local variable

35 Const Case-1 Case-2 Case-3 35 int a = 10; const int *p = &a; //*p = 30; // Error!!! a = 30; printf("*p:%d\n", *p); int a = 10, b = 30; int *const p = &a; //p = &b; // Error!!! *p = 30; printf("*p:%d\n", *p); int a = 10, b = 30; const int *const p = &a; //p = &b; // Error!!! //*p = 30; // Error!!! printf("*p:%d\n", *p); The variable a cannot be changed through the pointer variable p. The pointer variable p cannot point out another variable. The pointer variable p cannot point out another variable, further the variable a cannot be changed through the pointer variable p.

36 Thanks for attending :D THE END 36

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

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

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

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

More information

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information

QUIZ on Ch.8. What is kit?

QUIZ on Ch.8. What is kit? QUIZ on Ch.8 What is kit? QUIZ on Ch.8 What is kit? A: A vector of nested structures! QUIZ on Ch.8 C crash-course Compilation and Execution of a C Program Run-time Interpreted languages (like MATLAB, Python,

More information

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.7 Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.8 Given the following array, write code

More information

Computer Organization & Systems Exam I Example Questions

Computer Organization & Systems Exam I Example Questions Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C

More information

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

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc. C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker) C for

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

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

Basic and Practice in Programming Lab7

Basic and Practice in Programming Lab7 Basic and Practice in Programming Lab7 Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10;

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

Work relative to other classes

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

More information

Computers Programming Course 5. Iulian Năstac

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

More information

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265 Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =

More information

SAE1A Programming in C. Unit : I - V

SAE1A Programming in C. Unit : I - V SAE1A Programming in C Unit : I - V Unit I - Overview Character set Identifier Keywords Data Types Variables Constants Operators SAE1A - Programming in C 2 Character set of C Character set is a set of

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

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

Fundamentals of Programming Session 19

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

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Func%ons. CS449 Fall 2017

Func%ons. CS449 Fall 2017 Func%ons CS449 Fall 2017 1 Func%ons in C A group of statements performing a task Statements inside func%on is executed by calling it using the () operator Uses: Readability (through code modulariza%on)

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

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

Q 1. Attempt any TEN of the following:

Q 1. Attempt any TEN of the following: Subject Code: 17212 Model Answer Page No: 1 / 26 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The

More information

Tutorial 10 Pointers in C. Shuyue Hu

Tutorial 10 Pointers in C. Shuyue Hu Tutorial 10 Pointers in C Shuyue Hu Content Basic concept of pointers Pointer arithmetic Array of pointers Pointer to pointer Passing pointers to functions in C Return pointer from functions in C 2 Content

More information

CS 0449 Sample Midterm

CS 0449 Sample Midterm Name: CS 0449 Sample Midterm Multiple Choice 1.) Given char *a = Hello ; char *b = World;, which of the following would result in an error? A) strlen(a) B) strcpy(a, b) C) strcmp(a, b) D) strstr(a, b)

More information

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C. 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are

More information

C Programming Language (Chapter 2 of K&R) Variables and Constants

C Programming Language (Chapter 2 of K&R) Variables and Constants C Programming Language (Chapter 2 of K&R) Types, Operators and Expressions Variables and Constants Basic objects manipulated by programs Declare before use: type var1, var2, int x, y, _X, x11, buffer;

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

Fundamentals of Programming Session 20

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

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

More information

2.2 Pointers. Department of CSE

2.2 Pointers. Department of CSE 2.2 Pointers 1 Department of CSE Objectives To understand the need and application of pointers To learn how to declare a pointer and how it is represented in memory To learn the relation between arrays

More information

Programming. Elementary Concepts

Programming. Elementary Concepts Programming Elementary Concepts Summary } C Language Basic Concepts } Comments, Preprocessor, Main } Key Definitions } Datatypes } Variables } Constants } Operators } Conditional expressions } Type conversions

More information

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C Mechatronics and Microcontrollers Szilárd Aradi PhD Refresh of C About the C programming language The C programming language is developed by Dennis M Ritchie in the beginning of the 70s One of the most

More information

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ... Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such

More information

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g. 1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

More information

Fundamentals of Programming Session 14

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

More information

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

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

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

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

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

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

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]

More information

Engineering Computing I

Engineering Computing I Engineering Computing I Types, Operators, and Expressions Types Operators Expressions 2 1 2.1 Variable Names Names are made up of letters and digits The first character must be a letter The underscore

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

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

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

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

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

More information

Memory, Data, & Addressing II CSE 351 Spring

Memory, Data, & Addressing II CSE 351 Spring Memory, Data, & Addressing II CSE 351 Spring 2018 http://xkcd.com/138/ Review Questions 1) If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) a) 64

More information

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler) 1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would

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

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

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

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

More information

EC 413 Computer Organization

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

More information

Chapter 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

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

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

More information

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

VARIABLES AND CONSTANTS

VARIABLES AND CONSTANTS UNIT 3 Structure VARIABLES AND CONSTANTS Variables and Constants 3.0 Introduction 3.1 Objectives 3.2 Character Set 3.3 Identifiers and Keywords 3.3.1 Rules for Forming Identifiers 3.3.2 Keywords 3.4 Data

More information

CS 241 Data Organization Binary

CS 241 Data Organization Binary CS 241 Data Organization Binary Brooke Chenoweth University of New Mexico Fall 2017 Combinations and Permutations In English we use the word combination loosely, without thinking if the order of things

More information

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

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

More information

Introduction to C. Systems Programming Concepts

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

More information

Preview from Notesale.co.uk Page 6 of 52

Preview from Notesale.co.uk Page 6 of 52 Binary System: The information, which it is stored or manipulated by the computer memory it will be done in binary mode. RAM: This is also called as real memory, physical memory or simply memory. In order

More information

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6 CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers

More information

EK131 E5 Introduction to Engineering

EK131 E5 Introduction to Engineering EK131 E5 Introduction to Engineering Lecture 5: Conditional, Functions, Recursions Prof. Michel A. Kinsy Conditional execution Conditional constructs provide the ability to control whether a statement

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

SAMPLE MIDTERM SOLUTION

SAMPLE MIDTERM SOLUTION CMPT 102 SAMPLE MIDTERM SOLUTION PART 1 (40 points): SHORT ANSWER QUESTIONS 1. (8 points) Which of the following identifiers are invalid? Why? int my_book while loop FOR butter iff sentinel Calc-mean MeanOf45

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2 CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with

More information

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes MIDTERM TEST EESC 2031 Software Tools June 13, 2017 Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes This is a closed-book test. No books and notes are allowed. Extra space for

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

Number Systems, Scalar Types, and Input and Output

Number Systems, Scalar Types, and Input and Output Number Systems, Scalar Types, and Input and Output Outline: Binary, Octal, Hexadecimal, and Decimal Numbers Character Set Comments Declaration Data Types and Constants Integral Data Types Floating-Point

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

3. Types of Algorithmic and Program Instructions

3. Types of Algorithmic and Program Instructions 3. Types of Algorithmic and Program Instructions Objectives 1. Introduce programming language concepts of variables, constants and their data types 2. Introduce types of algorithmic and program instructions

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING LAB 1 REVIEW THE STRUCTURE OF A C/C++ PROGRAM. TESTING PROGRAMMING SKILLS. COMPARISON BETWEEN PROCEDURAL PROGRAMMING AND OBJECT ORIENTED PROGRAMMING Course basics The Object

More information

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

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

More information

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

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours? Lecture 16 Functions II they re back and they re not happy Daily Puzzle If it is raining at midnight - will we have sunny weather in 72 hours? function prototypes For the sake of logical clarity, the main()

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Beginning C Programming for Engineers

Beginning C Programming for Engineers Beginning Programming for Engineers R. Lindsay Todd Lecture 6: Bit Operations R. Lindsay Todd () Beginning Programming for Engineers Beg 6 1 / 32 Outline Outline 1 Place Value Octal Hexadecimal Binary

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

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

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

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

More information

Quick Reference Guide

Quick Reference Guide SOFTWARE AND HARDWARE SOLUTIONS FOR THE EMBEDDED WORLD mikroelektronika Development tools - Books - Compilers Quick Reference Quick Reference Guide with EXAMPLES for Basic language This reference guide

More information

Lecture 3. More About C

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

More information

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

Basic Types and Formatted I/O

Basic Types and Formatted I/O Basic Types and Formatted I/O C Variables Names (1) Variable Names Names may contain letters, digits and underscores The first character must be a letter or an underscore. the underscore can be used but

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

Chapter 12 Variables and Operators

Chapter 12 Variables and Operators Chapter 12 Variables and Operators Highlights (1) r. height width operator area = 3.14 * r *r + width * height literal/constant variable expression (assignment) statement 12-2 Highlights (2) r. height

More information

EL6483: Brief Overview of C Programming Language

EL6483: Brief Overview of C Programming Language EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions

More information