Week 8 Lecture 3. Finishing up C

Similar documents
Write a C program using arrays and structure

Fundamentals of Programming

Week 3 Lecture 2. Types Constants and Variables

Fundamental of Programming (C)

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

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

Q 1. Attempt any TEN of the following:

Introduction to C/C++ Lecture 5 - String & its Manipulation

C Programming Multiple. Choice

Computer Organization & Systems Exam I Example Questions

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

ONE DIMENSIONAL ARRAYS

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

Computers Programming Course 5. Iulian Năstac

C-LANGUAGE CURRICULAM

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

CSC 2400: Computer Systems. Arrays and Strings in C

Procedural Programming & Fundamentals of Programming

CS 61c: Great Ideas in Computer Architecture


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

Lecture 02 C FUNDAMENTALS

SECTION II: LANGUAGE BASICS

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Questions Bank. 14) State any four advantages of using flow-chart

Principles of C and Memory Management

Model Viva Questions for Programming in C lab

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

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Computers Programming Course 10. Iulian Năstac

CSC 2400: Computer Systems. Arrays and Strings in C

Midterm Sample Questions. a) What evaluates to FALSE in C? What evaluates to TRUE?

Introduction to string

Lectures 5-6: Introduction to C

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

C does not support string data type. Strings in C are represented by arrays of characters.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Strings and Library Functions

ARRAYS(II Unit Part II)

String can be represented as a single-dimensional character type array. Declaration of strings

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Fundamentals of Programming

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

DEPARTMENT OF MATHS, MJ COLLEGE

Chapter 8 Character Arrays and Strings

Lectures 5-6: Introduction to C

CS 222: Pointers and Manual Memory Management

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

by Pearson Education, Inc. All Rights Reserved.

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

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

COP 3223 Final Review

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

COP 3223 Final Review

High Performance Programming Programming in C part 1

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

Object Oriented Pragramming (22316)

Introduction to C Language (M3-R )

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Information Science 1

CS107, Lecture 4 C Strings

Programming Fundamentals (CS-302 )

Chapter 21: Introduction to C Programming Language

Fundamental of Programming (C)

Informatics Ingeniería en Electrónica y Automática Industrial

211: Computer Architecture Summer 2016

Lecture 3. More About C

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

C Characters and Strings

CS107 Spring 2019, Lecture 4 C Strings

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

Intermediate Programming, Spring 2017*

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Programming refresher and intro to C programming

C Programming Language Review and Dissection III

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Computers Programming Course 11. Iulian Năstac

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

Quick Reference Guide

Do not start the test until instructed to do so!

Quick Reference Guide

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Computers Programming Course 6. Iulian Năstac

SUMMER 13 EXAMINATION Model Answer

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

Lecture 3: C Programm

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

C programming language continued: Recursion, strings and pointer

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

Functions: call by reference vs. call by value

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

Array Initialization

Programming in C and C++

Transcription:

Lecture 3 Finishing up C

Conditional Operator 2

Conditional Operator Syntax Value <condition>? <expression1> : <expression2> If condition is true, expression1, otherwise expresion2 Example 0 > x? x : -x Value: absolute value of x 3

Comma Operator 4

Comma operator Syntax <expr1>, <expr2>,, <exprn> Value Exprn Precedence Lowest: <expri> is evaluated before going to next expr. Example for(i = 1, j = 2; i < 10; i++, j+=2) 5

Unions 6

Unions Unions save memory space by different types in a single variable Allows you to define a single super type for a set of sub types. The variable can only store on type at a time. 7

Unions Syntax: union <tag> {dec1,, decn} <vars> Syntax Each var may have the type of the declaration 8

Union Example 9

Storage Classes 10

Storage Class In addition to scope, lifetime and value, variables may have a storage class Auto: on stack Register: special memory location Static: on heap rather than on stack Extern: defined in separate compilation module 11

Storage Class In addition to scope, lifetime and value, variables may have a storage class Auto: on stack Register: special memory location Static: on heap rather than on stack Extern: defined in separate compilation module 12

Stack vs Heap Functions get new variables each time called The new variables are on the stack A stack is last in, first out The default storage class for local variable is auto Global variable have only on instance The variable is on the heap The heap is shared by the entire program The default storage class for globals is static 13

Automatic Variables Designated by auto keyword, though rarely used. Default class for variables (including parameters) in functions, including main(). Default value for variables local to blocks (e.g. for (int i = 0;...)) Not initialize. Contain garbage values. 14

Static Variables Designated by static keyword Default class for global variables Only one version of the variable Auto variables are create new for each new function Variables in functions may be declared static This is the only common use of the keyword 15

External variables Designated by extern keyword External variables are defined in other object files (i.e.,.o files) During linking the external variable are associated with the memory location defined in the other.o file. Used to expose variables in external modules. 16

Register Variables Designated by register keyword A register is a memory location on the Arithmetic-Logic Unit that does the calculations Registers need to be loaded before arithmetic can be done. Declaring a variable register removes that step. Don't user register variables The compiler is better at deciding where variables should go than you do. 17

Bit operators 18

Bit Operators C lets you operate on the bit representation of an int or a char. To have reasonable results, the type much be interpretable as a string of bits A char is 1 byte or 4 bits -128.. 127 0.. 256 An int is 4 bytes or 32 bits -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 19

Bitwise Operator. Manav Rachna College of Engg Returns 1 if LHS and RHS bits are different, 0 if same 20

Operating on Bits Bitwise operators The shift operator: x << n Manav Rachna College of Engg Shifts the bits in x n positions to the left, shifting in zeros on the right. If x = 1111 1111 1111 00002 x << 1 equals 1111 1111 1110 0000 2 x >> n Shifts the bits in x n positions right. shifts in the sign if it is a signed integer (arithmetic shift) shifts in 0 if it is an unsigned integer x >> 1 is 0111 1111 1111 10002. 21

Operating on Bits Bitwise logical operations Work on all integer types & Bitwise AND x&y Bitwise Inclusive OR Manav Rachna College of Engg x y ^ Bitwise Exclusive OR x^y output 1 only if input bits are different ~ The complement operator ~ y Complements all of the bits of X. 22

Bitwise Operator Example 23

Left shift, right shift We define char x = 15 Left shift moves the bits to the left, filling with 0 15 is 0000 1111 in binary e.g. x << 1 -> 0001 1110 or 16 + 8 + 4 + 2 = 30 Right shift moves the bits to the left e.g. x >> 1 0000 0111 or 4 + 2 + 1 = 7 24

And, Or, Exclusive or, Not &&, and! operate on true value &,, ^, and ~ work on bits && true false true false! true true false true true true true false false false false false true false false true & 1 0 1 0 ^ 1 0 ~ 1 1 0 1 1 1 1 0 1 1 0 0 0 0 0 1 0 0 1 0 0 1 25

&,, ^, ~ Remember x is 1510, which is 0000 11112 x & 1 -> 0000 0001 or 1 x 1 0000 1111 or 8 + 4 + 2 + 1 = 15 x ^ 1 0000 1110 or 8 + 4 + 2 + 1 = 14 ~x 1111 0000 or -16 26

1111 0000 is -16? Computers use twos compliment arithmetic Twos complement inverts the digits then adds 1 e.g. 15 is 0000 1111; -15 is 1111 0001 Advantages Can add positive and negative numbers Only one 0 27

Twos Compliment Computed by ~x + 1 Examples: 7 + -7 = 0 0111 +1001 ----0000 5 + -4 = 0 0101 +1100 ----0001 5 + -7 = -2 0101 +1001 ----1110 Bits Value 0111 7 0110 6 0101 5 0100 4 0011 3 0010 2 0001 1 0000 0 1111-1 1110-2 1101-3 1100-4 1011-5 1010-6 1001-7 1000-8 28

C String Library 29

The C String Library String functions are provided in an ANSI standard string library.. Manav Rachna College of Engg Access this through the include file: #include <string.h> Includes functions such as: Computing length of string Copying strings Concatenating strings This library is guaranteed to be there in any ANSI standard implementation of C. 30

Strlen() This function counts and returns the number of characters in a string. The length does not include a null character. Syntax n=strlen(string); Where n is integer variable. Which receives the value of length of the string. Example length=strlen( Hollywood ); The function will assign number of characters 9 in the string to a integer variable length. Manav Rachna College of Engg. 31

/* Use of the function strlen( ) */ main( ) { char arr[ ] = "Bamboozled" ; int len1, len2 ; len1 = strlen ( arr ) ; len2 = strlen ( "Humpty Dumpty" ) ; printf ( "\nstring = %s length = %d", arr, len1 ) ; Manav Rachna College of Engg printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ; }. 32

Comparing Strings Manav Rachna College of Engg C strings can be compared for equality or inequality If they are equal - they are ASCII identical If they are unequal the comparison function will return an int that is interpreted as: < 0 : str1 is less than str2 0 : str1 is equal to str2 > 0 : str1 is greater than str2. 33

Strcmp() Manav Rachna College of Engg int strcmp (char *str1, char *str2) ; Does an ASCII comparison one char at a time until a difference is found between two chars Return value is as stated before If both strings reach a '\0' at the same time, they are considered equal.. 34

Strcmp() Strcmp(string1,string2) string1 & string2 may be string variables or string constants. Some computers return a negative if the string1 is alphabetically less than the second and a positive number if the string is greater than the second. strcmp( their, there ) will return a -9 which is the numeric difference between ASCII i and ASCII r. strcmp( The, the ) will return -32 which is the numeric difference between ASCII T & ASCII t.. Manav Rachna College of Engg Example: strcmp( Newyork, Newyork ) will return zero because 2 strings are equal. 35

Strcat() Included in string.h and comes in the form: Manav Rachna College of Engg strcat ( str1, str2); Appends a copy of str2 to the end of str1 A pointer equal to str1 is returned Ensure that str1 has sufficient space for the concatenated string! Array index out of range will be the most popular bug in your C programming career.. 36

Example. Manav Rachna College of Engg #include <string.h> main() { char str1[27] = "abc"; char str2[100]; printf("%d\n",strlen(str1)); strcpy(str2,str1); puts(str2); puts("\n"); strcat(str2,str1); puts(str2); } 37

Strcpy() Strcpy () copies a string including the null character terminator from source string to destination. strcpy(destination string2, source string1); Manav Rachna College of Engg #include <string.h> char string1[] = "Hello, world!"; char string2[20]; strcpy(string2, string1);. 38

Usage of strcpy(). Manav Rachna College of Engg #include <string.h> main() { /* Example 1 */ char string1[ ] = "A string declared as an array.\n"; /* Example 2 */ char *string2 = "A string declared as a pointer.\n"; /* Example 3 */ char string3[30]; strcpy(string3, "A string constant copied in.\n"); printf (string1); printf (string2); printf (string3); } 39

Example- To check whether a char is vowel or not Manav Rachna College of Engg main() { char a[10];int i; printf("enter string"); gets(a); for(i=0;a[i]!='\0';i++) { if(a[i]=='a' a[i]=='e' a[i]=='i' a[i]=='o' a[i]==a[i]=='u' a[i]=='a' a[i]=='e' a[i]== 'I' a[i]=='o' a[i]=='u') { printf("%c",a[i]); } } }. 40

Example -To check whether string is palindrome or not for(i=0,j=l-1;i<l/2;i++,j--) { if(a[i]!=a[j]) { printf("not palindrome"); break; } } if(i==l/2) { printf("palindrome"); } Manav Rachna College of Engg main() { char a[10]; int l,i,j; clrscr(); printf("enter string"); gets(a); l=strlen(a);. } 41