ECET 264 C Programming Language with Applications
|
|
- Clifford Sparks
- 5 years ago
- Views:
Transcription
1 ECET 264 C Programming Language with Applications Lecture 9 & 10 Control Structures and More Operators Paul I. Lin Professor of Electrical & Computer Engineering Technology February 9, 2009 Lecture 9 & 10 - P. Lin 1 Lecture 9 & 10 Control Structures and Operators Review Nested Control Structures Example 9-1: 9 Nested Control Structures Example 9-2: 9 Testing ASCII Characters Example 9-3: 9 Counting Various ASCII Characters More C Operators Bit-wise operators: AND, OR, NOT, EX-OR Truth Tables Example 9-4: 9 Bit-Operations Example 9-5: 9 Clear Memory Locations Using EX-OR Operations Conditional Operators Sizeof Operator Example 9-6: 9 Sizeof Example Example 9-7: 9 Char and Character Array Size Address and Indirection Operators Example 9-8: 9 Address Operator and Pointers Summary February 9, 2009 Lecture 9 & 10 - P. Lin 2 1
2 Example 9-19 Nested Control Structures Problem Statement Write a C program to produce a summary of the exam results for ten students using nested control structures. If more than 8 students pass, a message of raising tuition is displayed. Analysis Need to include <stdio.h< stdio.h> Read input one at a time (1 passed, 2 failed) Use WHILE control structure Needs two or three counters? Output To display each exam entered, the results, and a message of raising tuition when more than 8 students passed. February 9, 2009 Lecture 9 & 10 - P. Lin 3 Example 9-19 (continue) Nested Control Structures Formulating Algorithm: Analyze student s s exam results and decide if tuition should be raised Define and initialize variables: COUNTERS: passed, failed, student result Input the exam results, count passes and failures while the counter is not exceed 10 Input the next exam result If the student passed, add one to passed COUNTER else add one to failed COUNTER add one to the student COUNTER Print a summary of the exam, and include a message Raise tuition if more than 8 students is passed February 9, 2009 Lecture 9 & 10 - P. Lin 4 2
3 Example 9-19 (continue) Nested Control Structures /* Analysis of examination results */ #include <stdio.h< stdio.h> /* function main begins program execution */ int main() { /* initialize variables in definitions */ int passes = 0; /* number of passes */ int failures = 0; /* number of failures */ int student = 1; /* student counter */ int result; /* one exam result */ /* process 10 students using counter-controlled controlled loop */ while (student <= 10) {/* prompt user for input and obtain value fom user */ printf("enter result (1=pass,2=fail): " ); scanf( ( "%d", &result ); February 9, 2009 Lecture 9 & 10 - P. Lin 5 Example 9-19 (continue) Nested Control Structures /* if result 1, increment passes */ if ( result == 1 ) { passes = passes + 1; /* end if */ else { /* otherwise, increment failures */ failures = failures + 1; /* end else */ student = student + 1; /*increment student counter */ /* end while */ /* termination phase; display number of passes and failures */ printf( ( "Passed %d\n", passes ); printf( ( "Failed %d\n", failures ); February 9, 2009 Lecture 9 & 10 - P. Lin 6 3
4 Example 9-19 (continue) Nested Control Structures /* if more than eight students passed, print "raise tuition" */ if ( passes > 8 ) { printf( ( "Raise tuition\n" n" ); /* end if */ return 0; /* indicate program ended successfully */ /* end function main */ February 9, 2009 Lecture 9 & 10 - P. Lin 7 Example 9-19 (continue) Nested Control Structures February 9, 2009 Lecture 9 & 10 - P. Lin 8 4
5 Example 9-19 (continue) Nested Control Structures February 9, 2009 Lecture 9 & 10 - P. Lin 9 Example 9 2 Testing ASCII Characters /* if3.c - Char Class Testing - Testing various ASCII character classes. */ #include <stdio.h< stdio.h> #include <ctype.h< ctype.h> static char s[] = "1aBY4*$%"; /* Character array s[] contains the t following characters: l, a, B, Y, 4, *, $, %,,, and \0 which is the EOS (End of String Character) */ void main() { char c; int i = 0; while((c = s[i++])!= '\0')' { if(islower(c)) { printf("%c = ",c); puts("l_case"); if(isupper(c)) { printf("%c = ",c); puts("u_case"); February 9, 2009 Lecture 9 & 10 - P. Lin 10 5
6 Example 9 2 (continue) Testing ASCII Characters if(isgraph(c)) { printf("%c = ",c); puts("printable"); if(isdigit(c)) { printf("%c = ",c); puts("digit"); if(c % 2 == 0) puts("even number"); February 9, 2009 Lecture 9 & 10 - P. Lin 11 Example 9 2 (continue) Testing ASCII Characters February 9, 2009 Lecture 9 & 10 - P. Lin 12 6
7 Example 9 3 Counting Various ASCII Characters /* Counting blanks, digits, newlines, letters and others EOF: end of file character is defined in <stdio.h< stdio.h> > as -1 Ctrl Z... MS-DOS system Ctrl D... UNIX system */ /* To run this program to count the ifelse0.c source code under the MS-DOS prompt you type: C:\UNIT3 UNIT3\ifelse0 < ifelse0.c */ #include <stdio.h< stdio.h> void main() { char ch; int b, d, n, l, others; b = d = n = l = others = 0; February 9, 2009 Lecture 9 & 10 - P. Lin 13 Example 9 3 (continue) Counting Various ASCII Characters puts( Please continue to enter from keyboard; Enter Ctrl Z to exit the loop ); while((ch = getchar())!= EOF) { if (ch( == )) ++b; else if (ch( >= '0' && ch <= '9') ++d; else if(ch >= 'a' && ch <= 'z' ch >= 'A' && ch <= 'Z') ++l; else if(ch == '\n')' ++n; else ++others; printf("blanks = %d\t", b); printf("digits = %d\n", d); printf("newlines = %d\t", t", n); printf("letters = %d\n", l); printf("others = %d", others); February 9, 2009 Lecture 9 & 10 - P. Lin 14 7
8 Example 9 3 (continue) Counting Various ASCII Characters Output: Please continue to enter from keyboard; Enter Ctrl Z to exit the loop 1234 testing testing <Enter> key ^Z blanks = 2 digits = 4 newlines = 1 letters = 14 others = 0Press any key to continue February 9, 2009 Lecture 9 & 10 - P. Lin 15 Bit-wise operators: &,, ^, ~, >>, Bitwise data manipulations are machine dependent Conditional Operators exp1? exp2: exp3; For example (x > 0)? puts( X X is positive\n ): puts( X X is negative ); Sizeof operator Sizeof(short) ) is 2byte Sizeof(int) ) is 4 bytes More C Operators Address operator & address operator for reference to variable directly February 9, 2009 Lecture 9 & 10 - P. Lin 16 8
9 It works with character and unsigned integer types Bit-wise Operators Applications: Windows operators, computer circuit simulation, digital circuit operations, etc. Operator Meaning & Two input AND gate operation Two input OR gate operation ^ Two input Ex-OR gate operation ~ Complement (NOT) or inverter >> Shift variable s s content to the right << Shift variable s s content to the left February 9, 2009 Lecture 9 & 10 - P. Lin 17 Truth Tables Input Outputs A B A & B A B A ^B AND OR EX-OR February 9, 2009 Lecture 9 & 10 - P. Lin 18 9
10 Bit-wise Operators (continue) Bit-Wise AND Example: If we define bit, b,, and c as a unsigned character variables: unsigned char b = 0x2F, c = 0xF3, bit; bit = b & c; /* bit = 0x23 */ In terms of binary numbers: b = AND & c = bit = February 9, 2009 Lecture 9 & 10 - P. Lin 19 Bit-wise Operators (continue) Bit-wise OR example: unsigned char b = 0x2F, c = 0xF3, bit; bit = b c; /* bit = 0xFF */ In terms of binary numbers: b = (OR) c = bit = February 9, 2009 Lecture 9 & 10 - P. Lin 20 10
11 Bit-wise Operators (continue) Bit-wise EXOR example: unsigned char b = 0x2F, c = 0xF3, bit; bit = b ^ c; /* bit = 0xDC */ In terms of binary numbers: b = EXOR ^ c = bit = It is no surprise to see b = b ^ b = 0x00 February 9, 2009 Lecture 9 & 10 - P. Lin 21 Bit-wise Operators (continue) Operator Meaning Example &= bit-wised AND and assign b &= 0x01; = bit-wised OR and assign ^= bit-wised EX-OR and assign b = b & 0x01; b = 0x0E; b = b 0x0E; OR and assign b ^= a; b =b^ a; February 9, 2009 Lecture 9 & 10 - P. Lin 22 11
12 Example 9-4: 9 Bit-Operations /* bit_wised_ops.c */ #include <stdio.h< stdio.h> void main() { unsigned short A_in1 = 1, A_in2 = 0, A_out; unsigned short O_in1 = 1, O_in2 = 0, O_out; unsigned short exor_in1 = 1, exor_in2 = 0, exor_out; unsigned short register1 = 0x0001, register2 = 0x1000; // Not Gate (Inverter) printf("a_in2 = %x\n",% A_in2); printf("~a_in2 = %x\n",% ~A_in2); // No bit masking A_in2 = 1; printf("a_in2 = %x\n",% A_in2); A_in2 = ~A_in2; printf("~a_in2 = %x\n",% A_in2); // Masking off bit 15-bit0 A_in2 &= 0x0001; printf("after Maksing: : ~A_in2 = %x\n", A_in2); // AND Gate A_out = A_in1 & A_in2; // 2-2 input AND gate printf("%x & %x = A_out = %x\n", A_in1, A_in2, A_out); printf("\ntruth ntruth Table - 2 input AND Gate\n"); printf(" \n"); printf("0 & 0 = %x\n",% 0 & 0); printf("0 & 1 = %x\n",% ", 0 & 1); printf("1 & 0 = %x\n",% 1 & 0); printf("1 & 1 = %x\n",% 1 & 1); February 9, 2009 Lecture 9 & 10 - P. Lin 23 Example Bit-Operations (continue) // OR Gate O_out = O_in1 & O_in2; // 2-2 input OR gate printf("%x %x = O_out = %x\n", O_in1, O_in2, O_out); printf("\ntruth ntruth Table - 2 input OR Gate\n"); printf(" (" \n"); printf("0 0 = %x\n",% 0 0); printf("0 1 = %x\n",% 0 1); printf("1 0 = %x\n",% 1 0); printf("1 1 = %x\n",% ", 1 1); // EXOR printf("\ntruth ntruth Table - 2 input EX-OR Gate\n"); printf(" \n"); printf("0 ^ 0 = %x\n",% 0 ^ 0); printf("0 ^ 1 = %x\n",% 0 ^ 1); printf("1 ^ 0 = %x\n",% 1 ^ 0); printf("1 ^ 1 = %x\n",% 1 ^ 1); /* end of main */ February 9, 2009 Lecture 9 & 10 - P. Lin 24 12
13 Example Bit-Operations (continue) Output A_in2 = 0 ~A_in2 = ffffffff A_in2 = 1 ~A_in2 = fffe After Maksing: : ~A_in2 = 0 1 & 0 = A_out = 0 Truth Table - 2 input AND Gate & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = = O_out = 0 Truth Table - 2 input OR Gate = = = = 1 Truth Table - 2 input EX-OR Gate ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 Press any key to continue February 9, 2009 Lecture 9 & 10 - P. Lin 25 Example 9-59 Clear Memory Locations Problem Statement Write a C program to show all possible methods of clearing or resetting integer type numerical variables Analysis Possible integer type numerical variables including char, short, int,, and long Analysis (cont.) Memory clearing or resetting methods may include, for example: unsigned short n = 0xFFFF; //or Method 1, assign zero n = 0; Method 2, subtraction n = n - n; Method 3, using Exclusive OR n ^=n; February 9, 2009 Lecture 9 & 10 - P. Lin 26 13
14 Example Clear Memory Locations /* bit_exor_clearm.c */ #include <stdio.h< stdio.h> void main() { unsigned short n1 = 0xFFFF; // unsigned short n2 = 0x1000; // 4096 unsigned short n3 = 0x0100; // 256 printf("original unsigned short n1 = %d\n",% n1); printf("original unsigned short n2 = %d\n",% n2); printf("original unsigned short n3 = %d\n",% n3); // Method 1 n1 = 0; // Method 2 n2 = n2 - n2; // or n2 -= = n2; (continue) // Method 3 n3 ^= n3; // or n3 = n3 ^ n3; printf("cleared: : unsigned short n1 = %x\n",% n1); printf("cleared: : unsigned short n2 = %x\n",% n2); printf("cleared: : unsigned short n3 = %x\n",% n3); /* End of main */ OUTPUT Original: unsigned short n1 = Original: unsigned short n2 = 4096 Original: unsigned short n3 = 256 Cleared: unsigned short n1 = 0 Cleared: unsigned short n2 = 0 Cleared: unsigned short n3 = 0 Press any key to continue February 9, 2009 Lecture 9 & 10 - P. Lin 27 Conditional Operators High-level language: if (a > b) max=a; else max =b; Conditional Operator: exp1? Exp2: exp3; February 9, 2009 Lecture 9 & 10 - P. Lin 28 14
15 Conditional Operators (continue) Examples: int a = 10, b = -10; max = (a > b)? a: b; /* max = 10*/ min = (a < b)? a: b; /* min = -10 */ v = (5)? 1: 2; // 5 is non-zero, true; v = 1 v = (0)? 1: 2; // 0 is zero, false; v = 2 February 9, 2009 Lecture 9 & 10 - P. Lin 29 sizeof Operator sizeof operator is a unary operator to find the number of bytes for storing the value of a variable in the memory Examples Len = sizeof(short); /* Len= 2, or 2 bytes */ Len = sizeof (n);/ * Len = 4, if n is a 32-bit integer */ Applications: CPU size checking Dynamic memory allocation at run time Data record size calculation etc February 9, 2009 Lecture 9 & 10 - P. Lin 30 15
16 Example 9 6 sizeof Problem Statement You are asked to write a simple C program to check the sizes of C data types, including char, short, int,, long, float, and double. Analysis sizeof() and printf() functions are needed Correct data type names will be needed February 9, 2009 Lecture 9 & 10 - P. Lin 31 Example 9 6 (continue) sizeof The program that tell us the sizes of requested data types. /* sizeof_check.c */ #include <stdio.h> void main() { printf("sizeof(char) = %d Bytes\n", sizeof(char)); printf("sizeof(short) = %d Bytes\n", sizeof(short)); printf("sizeof(int) = %d Bytes\n", sizeof(int)); printf("sizeof(long) = %d Bytes\n", sizeof(long)); printf("sizeof(float) = %d Bytes\n", sizeof(float)); printf("sizeof(double) = %d Bytes\n", sizeof(double)); February 9, 2009 Lecture 9 & 10 - P. Lin 32 16
17 Example 9 6 (continue) sizeof Output sizeof(char) ) = 1 Bytes sizeof(short) ) = 2 Bytes sizeof(int) ) = 4 Bytes sizeof(long) ) = 4 Bytes sizeof(float) ) = 4 Bytes sizeof(double) ) = 8 Bytes February 9, 2009 Lecture 9 & 10 - P. Lin 33 Example 9 7 Char and Character Array Size Problem Statement: Write a program that shows and prints the size of a character and a character array. The sizeof operator should be used to evaluate the size of variables or objects and it return a number with the unit of bytes. Output Requirement The output of the program should be text- based, with heading info, and actual byte size for user. February 9, 2009 Lecture 9 & 10 - P. Lin 34 17
18 Example 9 7 (continue) Char and Character Array Size Analysis This program will be written using Microsoft Visual C and for running under DOS virtual machine Testing Data We will use the following data for testing: A char variable which contains a character T, and a character array which contains a string T which contains two characters, namely, T and a null character \0. Format Specifiers: %c for printing signal character %d -- for printing a decimal number %s -- for printing a string of characters February 9, 2009 Lecture 9 & 10 - P. Lin 35 Example 9 7 (continue) Char and Character Array Size /* charstr.c */ #include <stdio.h< stdio.h> void main() { char ch = 'T'; /*Define a single character variable ch*/ /*Evaluate the size of character type variable ch*/ /*The ch variable is a local variable within the main()*/ printf("sizeof(ch) ) = %d\n", sizeof(ch)); /* Evaluate the size of a string constant "T"*/ printf("%s\n", ", "T"); February 9, 2009 Lecture 9 & 10 - P. Lin 36 18
19 Example 9 7 (continue) Char and Character Array Size /*To display a double quote, a back slash is needed*/ /*Because a pair of double quote is used to enclose*/ /* the format string in the printf() () function */ printf("sizeof(\"t "T\")") = %d\n", sizeof("t")); ")); Output: sizeof(ch) ) = 1 T sizeof( T") = 2 February 9, 2009 Lecture 9 & 10 - P. Lin 37 Address and Indirection Operators & Obtain address of a variable * Access the contents of a memory (using pointer or indirection) Examples: int a = 10; int *ptr;; /* define a pointer variable called ptr of pointer type */ ptr = &a; /* assign the address of a variable to pointer ptr */ (*ptr ptr)++; /* Access memory directory using pointer variable, a = 11 after this line */ February 9, 2009 Lecture 9 & 10 - P. Lin 38 19
20 Example 9 8 Address Operator and Pointers /* pointer1.c - This program explains how to use address operator and pointers. */ #include <stdio.h< stdio.h> void main() { int a = 10; int *ptr;; /*A variable that holds the address of another r variable */ ptr = &a; printf("a = %d\n", a); printf("(* ("(*ptr)) = %d\n", (*ptr ptr)); (*ptr ptr)++; printf("(* ("(*ptr)++ = %d\n", (*ptr ptr)); printf("&a = %x\n", &a); printf("ptr = %x\n", ptr); February 9, 2009 Lecture 9 & 10 - P. Lin 39 Example 9 8 (continue) Address Operator and Pointers February 9, 2009 Lecture 9 & 10 - P. Lin 40 20
21 Summary Review Nested Control Structures More C Operators Bit-wise operators Conditional Operators sizeof operator Address operator C Examples Next More C Program Control February 9, 2009 Lecture 9 & 10 - P. Lin 41 Question? Answers lin@ipfw.edu February 9, 2009 Lecture 9 & 10 - P. Lin 42 21
ECET 264 C Programming Language with Applications
ECET 264 C Programming Language with Applications Lecture 6 Control Structures and More Operators Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture
More informationBasic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print
CSC 4304 - Systems Programming Fall 2010 Lecture - II Basics of C Programming Summary of Last Class Basics of UNIX: logging in, changing password text editing with vi, emacs and pico file and directory
More informationProgramming. Data Structure
Programming & Data Structure For Computer Science & Information Technology By www.thegateacademy.com Syllabus Syllabus for Programming and Data Structures Programming in C, Arrays, Stacks, Queues, Linked
More informationPointers (2) Applications
Pointers (2) Applications December 9, 2017 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license. 0.1 const qaulifier t1.c #include #include
More informationECET 264 C Programming Language with Applications. C Program Control
ECET 264 C Programming Language with Applications Lecture 7 C Program Control Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 7 - Paul I. Lin
More informationFundamental 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 informationIntroduction 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 informationTHE FUNDAMENTAL DATA TYPES
THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include
More informationCSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1
CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled
More informationCS107, Lecture 3 Bits and Bytes; Bitwise Operators
CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution
More informationPrinciples 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 informationToday. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed
CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)
More informationComputer 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 informationLESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long
LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be
More informationProgramming Language A
Programming Language A Takako Nemoto (JAIST) 26 November Takako Nemoto (JAIST) 26 November 1 / 12 Type char char is a datatype for characters. char express integers in certain region. For each ASCII character,
More informationINTRODUCTION TO C-PROGRAMING LANGUAGE
INTRODUCTION TO C-PROGRAMING LANGUAGE Compiled by G/Michael G. Mar 2015 Content Page 1. C - Basic Introduction----------------------------------------------------- 2 2. C - Program File------------------------------------------------------------
More informationDeclaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.
Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform
More informationFundamental 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 informationBeginning 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 informationFundamentals 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 informationChapter 3. Fundamental Data Types
Chapter 3. Fundamental Data Types Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Variable Declaration
More informationChapter 7. Basic Types
Chapter 7 Basic Types Dr. D. J. Jackson Lecture 7-1 Basic Types C s basic (built-in) types: Integer types, including long integers, short integers, and unsigned integers Floating types (float, double,
More informationUNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING
UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING MODULE CODE: MODULE TITLE: ET4131 Introduction to Computer Programming SEMESTER:
More informationMuntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types
Programming Fundamentals for Engineers 0702113 5. Basic Data Types Muntaser Abulafi Yacoub Sabatin Omar Qaraeen 1 2 C Data Types Variable definition C has a concept of 'data types' which are used to define
More informationDay05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14
Day05 A Young W. Lim 2017-10-07 Sat Young W. Lim Day05 A 2017-10-07 Sat 1 / 14 Outline 1 Based on 2 Structured Programming (2) Conditions and Loops Conditional Statements Loop Statements Type Cast Young
More informationISA 563 : Fundamentals of Systems Programming
ISA 563 : Fundamentals of Systems Programming Variables, Primitive Types, Operators, and Expressions September 4 th 2008 Outline Define Expressions Discuss how to represent data in a program variable name
More informationCS107, Lecture 3 Bits and Bytes; Bitwise Operators
CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution
More informationLecture 5: Multidimensional Arrays. Wednesday, 11 February 2009
Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional
More informationint main(void) { int a, b, c; /* declaration */
&KDSWHULQ$%& #include int main(void) { int a, b, c; /* declaration */ float x, y=3.3, z=-7.7; /* declaration with initialization */ printf("input two integers: "); /* function call */ scanf("%d%d",
More informationStructured Program Development in C
1 3 Structured Program Development in C 3.2 Algorithms 2 Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed
More informationA Short Course for REU Students Summer Instructor: Ben Ransford
C A Short Course for REU Students Summer 2008 Instructor: Ben Ransford http://www.cs.umass.edu/~ransford/ ransford@cs.umass.edu 1 Outline Today: basic syntax, compilation Next time: pointers, I/O, libraries
More informationA 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 informationC/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 informationC/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 informationUNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING
( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING INTRODUCTION TO C UNIT IV Overview of C Constants, Variables and Data Types
More informationIncoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.
Incoming Exam CS 0 Introduction to Pointers Debzani Deb Next Monday (th March), we will have Exam # Closed book Sit with an empty space in either side of you Calculators that have text-allowing is not
More informationCS 107 Lecture 3: Integer Representations
CS 107 Lecture 3: Integer Representations Monday, January 15, 2018 Computer Systems Winter 2018 Stanford University Computer Science Department Reading: Reader: Bits and Bytes, Textbook: Chapter 2.2 Lecturers:
More informationMain 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 informationLecture 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 informationBLM2031 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 informationLow-Level Programming
Programming for Electrical and Computer Engineers Low-Level Programming Dr. D. J. Jackson Lecture 15-1 Introduction Previous chapters have described C s high-level, machine-independent features. However,
More informationApplied Computer Programming
Applied Computer Programming Representation of Numbers. Bitwise Operators Course 07 Lect.eng. Adriana ALBU, PhD Politehnica University Timisoara Internal representation All data, of any type, processed
More informationCS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015
CS 141 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015 You may take this test with you after the test, but you must turn in your answer sheet. This test has the following sections:
More informationWork 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 informationStructured Programming. Jon Macey
Structured Programming Jon Macey Structured Programming Structured programming is an attempt to formalise the process of program development. There are several basis for a theorem of structured programming,
More informationLecture 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 informationCS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation
CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;
More informationUNIT 3 OPERATORS. [Marks- 12]
1 UNIT 3 OPERATORS [Marks- 12] SYLLABUS 2 INTRODUCTION C supports a rich set of operators such as +, -, *,,
More informationArithmetic Operators. Portability: Printing Numbers
Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - +
More informationDEPARTMENT 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 informationC: 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 informationComputers 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 informationOverview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O
Overview of C Basic Data Types Constants Variables Identifiers Keywords Basic I/O NOTE: There are six classes of tokens: identifiers, keywords, constants, string literals, operators, and other separators.
More informationApproximately a Test II CPSC 206
Approximately a Test II CPSC 206 Sometime in history based on Kelly and Pohl Last name, First Name Last 5 digits of ID Write your section number(s): All parts of this exam are required unless plainly and
More informationC Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then
1 7 C Pointers 7.7 sizeof Operator 2 sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then int myarray[ 10 ]; printf( "%d", sizeof(
More informationVariables 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 informationPointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan
Pointers Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization 7.3 Pointer
More informationITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University
ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs
More informationThese 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 informationEL2310 Scientific Programming
Lecture 11: Memory, Files and Bitoperations (yaseminb@kth.se) Overview Overview Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations Lecture 11: Memory, Files
More informationLecture 9 - C Functions
ECET 264 C Programming Language with Applications Lecture 9 C Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 9- Prof. Paul I. Lin
More informationChapter 3 Structured Program Development
1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction
More informationGuide 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 informationC 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 informationComputers Programming Course 6. Iulian Năstac
Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap
More informationLecture 02 C FUNDAMENTALS
Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void
More informationBit Manipulation in C
Bit Manipulation in C Bit Manipulation in C C provides six bitwise operators for bit manipulation. These operators act on integral operands (char, short, int and long) represented as a string of binary
More informationIntroduction to C Language
Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,
More informationReview Topics. Midterm Exam Review Slides
Review Topics Midterm Exam Review Slides Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University!! Computer Arithmetic!! Combinational
More informationHISTORY OF C LANGUAGE. Facts about C. Why Use C?
1 HISTORY OF C LANGUAGE C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating
More informationINTRODUCTION TO COMPUTER SCIENCE - LAB
LAB # O2: OPERATORS AND CONDITIONAL STATEMENT Assignment operator (=) The assignment operator assigns a value to a variable. X=5; Expression y = 2 + x; Increment and decrement (++, --) suffix X++ X-- prefix
More informationWeek 3 Lecture 2. Types Constants and Variables
Lecture 2 Types Constants and Variables Types Computers store bits: strings of 0s and 1s Types define how bits are interpreted They can be integers (whole numbers): 1, 2, 3 They can be characters 'a',
More informationP.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)
FACULTY: Ms. Saritha P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS) SUBJECT / CODE: Programming in C and Data Structures- 15PCD13 What is token?
More informationC 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 informationChapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 Chapter 7 Pointers Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 7 - Pointers 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization
More informationCS 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 informationFeatures 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 informationLecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar
Examples of Software Programming and Data Structure Lecture 2 Sudeshna Sarkar Read an integer and determine if it is a prime number. A Palindrome recognizer Read in airline route information as a matrix
More informationSample Examination. Family Name:... Other Names:... Signature:... Student Number:...
Family Name:... Other Names:... Signature:... Student Number:... THE UNIVERSITY OF NEW SOUTH WALES SCHOOL OF COMPUTER SCIENCE AND ENGINEERING Sample Examination COMP1917 Computing 1 EXAM DURATION: 2 HOURS
More information8. Characters and Arrays
COMP1917 15s2 8. Characters and Arrays 1 COMP1917: Computing 1 8. Characters and Arrays Reading: Moffat, Section 7.1-7.5 ASCII The ASCII table gives a correspondence between characters and numbers behind
More informationWorksheet 4 Basic Input functions and Mathematical Operators
Name: Student ID: Date: Worksheet 4 Basic Input functions and Mathematical Operators Objectives After completing this worksheet, you should be able to Use an input function in C Declare variables with
More informationFundamentals of Programming
Fundamentals of Programming Lecture 4 Input & Output Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline printf scanf putchar getchar getch getche Input and Output in
More informationOperators and Expressions:
Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence
More informationCS Programming In C
CS 24000 - Programming In C Week Eight: arithmetic and bit operations on chars ; C Programming Tools: GDB, C preprocessor, Makefile Zhiyuan Li Department of Computer Science Purdue University, USA This
More informationMidterm Exam Review Slides
Midterm Exam Review Slides Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Review Topics Number Representation Base Conversion
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi CE 43 - Fall 97 Lecture 4 Input and Output Department of Computer Engineering Outline printf
More informationIntroduction to Computing Systems Fall Lab # 3
EE 1301 UMN Introduction to Computing Systems Fall 2013 Lab # 3 Collaboration is encouraged. You may discuss the problems with other students, but you must write up your own solutions, including all your
More informationSchool of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015
Page 1 of 8 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2015 Midterm Examination # 1 Wednesday, February 11, 2015 Marking Exemplar Duration of examination: 75
More informationProgramming. Elementary Concepts
Programming Elementary Concepts Summary } C Language Basic Concepts } Comments, Preprocessor, Main } Key Definitions } Datatypes } Variables } Constants } Operators } Conditional expressions } Type conversions
More informationChapter 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 informationNumber Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example
Appendix F Number Systems Binary Numbers Decimal notation represents numbers as powers of 10, for example 1729 1 103 7 102 2 101 9 100 decimal = + + + There is no particular reason for the choice of 10,
More informationExpression and Operator
Expression and Operator Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a
More informationFile 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 informationAgenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion
CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,
More informationMore about BOOLEAN issues
More about BOOLEAN issues Every boolean test is an implicit comparison against zero (0). However, zero is not a simple concept. It represents: the integer zero for all integral types the floating point
More informationM4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE
M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,
More informationCIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions
Fall 2011 Name: Page Points Score 1 7 2 10 3 8 4 13 6 17 7 4 8 16 9 15 10 10 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. For each of the
More informationChapter 2 (Dynamic variable (i.e. pointer), Static variable)
Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= #
More information