Introduction to C. Systems Programming Concepts

Similar documents
C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Content. In this chapter, you will learn:

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

INTRODUCTION TO COMPUTER SCIENCE - LAB

Lecture 3. More About C

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

C Pointers Pearson Education, Inc. All rights reserved.

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

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

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

First of all, it is a variable, just like other variables you studied

Basic Types and Formatted I/O

More about BOOLEAN issues

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Basic Assignment and Arithmetic Operators

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Chapter 3 Structured Program Development

THE FUNDAMENTAL DATA TYPES

Fundamental of Programming (C)

Structured Program Development

ECET 264 C Programming Language with Applications

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

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

ECET 264 C Programming Language with Applications

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

EL2310 Scientific Programming

Programming for Engineers Iteration

CS 61C: Great Ideas in Computer Architecture Introduction to C

Structured Program Development in C

Programming. Data Structure

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

INTRODUCTION TO C-PROGRAMING LANGUAGE

The C language. Introductory course #1

Work relative to other classes

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Variation of Pointers

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Computers Programming Course 6. Iulian Năstac

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

ISA 563 : Fundamentals of Systems Programming

Principles of C and Memory Management

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura


This exam is to be taken by yourself with closed books, closed notes, no calculators.

Fundamental of Programming (C)

C library = Header files + Reserved words + main method

C-LANGUAGE CURRICULAM

3. Types of Algorithmic and Program Instructions

The New C Standard (Excerpted material)

OBJECT ORIENTED PROGRAMMING

Fundamental of C programming. - Ompal Singh

Declaration and Memory

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

WARM UP LESSONS BARE BASICS

Technical Questions. Q 1) What are the key features in C programming language?

CSE 5A Final Fall 2006

Arithmetic Operators. Portability: Printing Numbers

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

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Differentiate Between Keywords and Identifiers

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

Fundamentals of Programming

Programming, numerics and optimization

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

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

C: How to Program. Week /Mar/05

Course Outline Introduction to C-Programming

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Dept. of CSE, IIT KGP

Introduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved

UNIT- 3 Introduction to C++

Chapter 3. Fundamental Data Types

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

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

std::cout << "Size of long = " << sizeof(long) << " bytes\n\n"; std::cout << "Size of char = " << sizeof(char) << " bytes\n";

Chapter 2 - Introduction to C Programming

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

PRINCIPLES OF OPERATING SYSTEMS

The Design of C: A Rational Reconstruction: Part 2

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Continued from previous lecture

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

Operators in C. Staff Incharge: S.Sasirekha

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

Pointers and Arrays 1

Pointers and Strings Prentice Hall, Inc. All rights reserved.

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Transcription:

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 Another C Program #define scanf ( ) Systems Programming Introduction to C 2

Introduction to C Another C Program (continued) for loop Promotion Other C topics Increment and Decrement Operators Casting Operator Precedence Value of Assignment Operator Systems Programming Introduction to C 3

Variables Variable names correspond to memory locations in memory. Every variable has a type, a name and a value. int i; i = 4; 32212242 i 4 (the address of i ) &i Systems Programming Introduction to C 4

printf int main() { printf( %d %c\n, i, ch); Two components of printf statement: Formatting template {within quotes Argument list variables separated by commas. Systems Programming Introduction to C 5

printf int main() { printf( %d %f %c\n, i, fvar, ch); Formatting template: Argument list matches up with % Some of the argument types: %d integers %f floating-point numbers %c characters Systems Programming Introduction to C 6

printf int main() { printf( %4d %5f %6.2f\n, i, fvar, f2var); Width of variable printing: %4d decimal integers at least 4 digits wide %5f floating point at least 5 digits wide %6.2f floating point at least 6 digits wide with at least 2 after the decimal point Systems Programming Introduction to C 7

A Simple C Program /* Example of a simple C Program */ #include <stdio.h> int main() { i int i; c C char c, s; Preprocessor directive 2303 i = 2303; s S c = 'C'; s = 'S'; printf("\nhello"); printf(" %c%c %d Students!!\n", c, s, i); return 0; Comments in C Type declarations specify memory sizes Systems Programming Introduction to C 8

Compiling and Running simple %ls simple.c %gcc simple.c %ls a.out simple.c %./a.out Alternate Version %ls simple.c %gcc o simple simple.c %ls simple simple.c %./simple Hello CS 2303 Students!! % Systems Programming Introduction to C 9

sizeof Operator 1 /* Fig. 7.17: fig07_17.c 2 Demonstrating the sizeof operator */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 char c; 8 short s; 9 int i; 10 long l; 11 float f; 12 double d; 13 long double ld; 14 int array[ 20 ]; /* create array of 20 int elements */ 15 int *ptr = array; /* create pointer to array */ 16 Figure 7.17 (part 1) Systems Programming Introduction to C 10

sizeof Operator 17 printf( " sizeof c = %d\tsizeof(char) = %d" 18 "\n sizeof s = %d\tsizeof(short) = %d" 19 "\n sizeof i = %d\tsizeof(int) = %d" 20 "\n sizeof l = %d\tsizeof(long) = %d" 21 "\n sizeof f = %d\tsizeof(float) = %d" 22 "\n sizeof d = %d\tsizeof(double) = %d" 23 "\n sizeof ld = %d\tsizeof(long double) = %d" 24 "\n sizeof array = %d" 25 "\n sizeof ptr = %d\n", 26 sizeof c, sizeof( char ), sizeof s, sizeof( short ), sizeof i, 27 sizeof( int ), sizeof l, sizeof( long ), sizeof f, 28 sizeof( float ), sizeof d, sizeof( double ), sizeof ld, 29 sizeof( long double ), sizeof array, sizeof ptr ); 30 31 return 0; /* indicates successful termination */ 32 33 /* end main */ sizeof c = 1 sizeof(char) = 1 sizeof s = 2 sizeof(short) = 2 sizeof i = 4 sizeof(int) = 4 sizeof l = 4 sizeof(long) = 4 sizeof f = 4 sizeof(float) = 4 sizeof d = 8 sizeof(double) = 8 sizeof ld = 8 sizeof(long double) = 8 sizeof array = 80 sizeof ptr = 4 Figure 7.17 (part 2) from typelen.c char 1 short 2 int 4 long 4 long long 8 float 4 double 8 long double 12 Systems Programming Introduction to C 11

Conditional Testing for True /* check to see what conditional does with negative integers */ int main () { int i = 0; // zero is the only value for false in C if (i) printf("%d = true\n", i); else printf("%d = false\n", i); i = 4; if (i) printf("positive integer %d = true\n", i); else printf("positive integer %d = false\n", i); i = -4; if (i) printf("negative integer %d = true\n", i); else printf("negative integer %d = false\n", i); return 0; $./a.out 0 = false Positive integer 4 = true Negative integer -4 = true Systems Programming Introduction to C 12

Another C Program #define SIZE 5 int main () { int i, start, finish; float celsius; preprocessor directive scanf needs the address scanf("%d", &start); finish = start + SIZE; for (i=start; i<finish; i++) { celsius = (5.0/9.0)* (i - 32.0); printf("%3d %6.1f\n", i, celsius); return 0; use of define Systems Programming Introduction to C 13

Another C Program #define SIZE 5 int main () { int i, start, finish; float celsius; initial value continue to loop if True scanf("%d", &start); finish = start + SIZE; for (i=start; i<finish; i++) { celsius = (5.0/9.0)* (i - 32.0); printf("%3d %6.1f\n", i, celsius); return 0; after each iteration Systems Programming Introduction to C 14

Another C Program #define SIZE 5 int main () { int i, start, finish; float celsius; example of promotion scanf("%d", &start); finish = start + SIZE; for (i=start; i<finish; i++) { celsius = (5.0/9.0)* (i - 32.0); printf("%3d %6.1f\n", i, celsius); return 0; $./a.out 30 30-1.1 31-0.6 32 0.0 33 0.6 34 1.1 Systems Programming Introduction to C 15

Other C Topics Increment and decrement operators Casting operator ( type ) Operator precedence Danger mistake in the value of the assignment operator Variable scope Switch Conditional operator?: Will cover later!! Systems Programming Introduction to C 16

Increment and Decrement Operators Operator Sample expression Explanation ++ ++a Increment a by 1, then use the new value of a in the expression in which a resides. ++ a++ Use the current value of a in the expression in which a resides, then increment a by 1. -- --b Decrement b by 1, then use the new value of b in the expression in which b resides. -- b-- Use the current value of b in the expression in which b resides, then decrement b by 1. Fig. 3.12 Increment and decrement operators Systems Programming Introduction to C 17

Casting Cast is a unary operator. Cast is often useful when an iteration index is used in mixed type arithmetic. Later, it will be important to make sure arguments passed are properly matched between called and calling routines. Example: int total, count; float average; average = (float) total / count; When in doubt, be conservative and use cast to be sure! Systems Programming Introduction to C 18

Fig 4.16 Operator Precedence Operators Associativity Type ++ (postfix) -- (postfix) right to left postfix + -! ++ (prefix) -- (prefix) (type) right to left unary * / % left to right multiplicative arithmetic + - left to right additive < <= > >= left to right relational boolean cast ==!= left to right equality && left to right logical AND logical left to right logical OR?: right to left conditional = += -= *= /= %= right to left assignment, left to right comma Systems Programming Introduction to C 19

Value of Assignment The value of assignment is the same as the contents deposited into the variable type on the left. Note: There are several potential dangers here especially when the programmer creates new types!! Bad Examples (for now): if ( i = 0 ) if ( i = 4 ) What is the problem?? if ( i == 0) if ( i == 4) Systems Programming Introduction to C 20

Review of Introduction to C This presentation covers many important C topics quickly including: Declaration of variable types memory allocation by type The address of a variable & printf ( ), scanf ( ) C arithmetic (operators, precedence, casting, promotion, assignment value) C booleans (true and false) if Preprocessor directives #define, #include for You are now ready to due lab 1 and once we cover functions everyone should be able to due Program 1. Systems Programming Introduction to C 21