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

Similar documents
Expressions and Precedence. Last updated 12/10/18

Review of the C Programming Language for Principles of Operating Systems

UNIT- 3 Introduction to C++

Variables and literals

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

Work relative to other classes

C Language Summary (Continued)

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

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

Review of the C Programming Language

ISA 563 : Fundamentals of Systems Programming

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

Chapter 3: Operators, Expressions and Type Conversion

The Arithmetic Operators

Part I Part 1 Expressions

OBJECT ORIENTED PROGRAMMING

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

A flow chart is a graphical or symbolic representation of a process.

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:

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

Lecture 3. More About C

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

Computers Programming Course 6. Iulian Năstac

Information Science 1

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Introduction to C Language

LECTURE 3 C++ Basics Part 2

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

LEXICAL 2 CONVENTIONS

CSC 1107: Structured Programming

Fundamentals of Programming

Fundamental of Programming (C)

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

Programming in C and C++

Programming. Elementary Concepts

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Variables and Operators 2/20/01 Lecture #

SECTION II: LANGUAGE BASICS

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

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Reserved Words and Identifiers

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

Language Reference Manual simplicity

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C - Basics, Bitwise Operator. Zhaoguo Wang

C Syntax Out: 15 September, 1995

TED Language Reference Manual

A Fast Review of C Essentials Part I

ANSI C Programming Simple Programs

Differentiate Between Keywords and Identifiers

Typescript on LLVM Language Reference Manual

Language Fundamentals Summary

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

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

LECTURE 02 INTRODUCTION TO C++

Operators. Java operators are classified into three categories:

Programming in C++ 6. Floating point data types

Lesson 3 Introduction to Programming in C

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

CHAPTER 3 BASIC INSTRUCTION OF C++

The C++ Language. Arizona State University 1

EL2310 Scientific Programming

BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Types, Operators and Expressions

JAVA Programming Fundamentals

Basics of Java Programming

Part I Part 1 Expressions

Structure of this course. C and C++ Past Exam Questions. Text books

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

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

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

CS Programming In C

Presented By : Gaurav Juneja

Java Basic Programming Constructs

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Chapter 2. Lexical Elements & Operators

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Fundamental of C programming. - Ompal Singh

Declaration and Memory

Types, Operators and Expressions

Chapter 3 Structure of a C Program

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Chapter 12 Variables and Operators

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

We do not teach programming

C: How to Program. Week /Mar/05

Transcription:

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 -Lower level than C++ & Java -gcc: The GNU C compiler 2

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; $ gcc hello.c -o hello.out $./hello.out Hello World $ _ 3

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Whitespace is ignored #include <stdio.h> int main(void){printf("hello World\n");return 0;} 4

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Include the library that provides printing functions 5

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Every C program must have a main function int main(void) The main function returns an integer value. It must be named main. In this case, it does not receive any parameters. 6

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; The scope encloses the function body 7

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Call the print function with the desired argument printf("hello World\n"); Call the function printf Send it the string to print End statements with a semicolon 8

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Call the print function with the desired argument printf("hello World\n"); Strings literalsare enclosed in double-quotes. \n represents a new line 9

Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Since main is defined to return an integer usually a zero is returned 10

Identifiers -Used to name variables, functions, and userdefined data types -A group of letters, decimal digits, _ -Cannot start with a number -Examples: foo my_var a1 Something_Clever_1 x C is case-sensitive! 11

Constants -Integer Constants: Octal Hexidecimal Equivalent -Character Constants: Enclosed in single-quotes -Real Number Constants: -String Constants: "It s a trap." "It s a" "It s\ a trap." 138 23 4002145 032 0x1f1f \ 'c' 'X' '\n' '\'' '\\' '\"' 3.14 5..1 0.2 500e9 3e-5 " trap." "It s\na\ntrap." "\"It s a trap\"" It s a trap. It s a trap. Exponent 12

Constants -Integer Constants: Octal Hexidecimal Equivalent -Character Constants: Enclosed in single-quotes -Real Number Constants: -String Constants: "It s a trap." "It s a" "It s\ a trap." 138 23 4002145 032 0x1f1f \ 'c' 'X' '\n' '\'' '\\' '\"' 3.14 5..1 0.2 500e9 3e-5 " trap." "It s\na\ntrap." "\"It s a trap\"" It s a trap. It s a trap. Exponent 13

Primitive Data Types Sizes may change from system to system! -Integer types unsigned char /* Holds a value from 0 to 256 */ signed char /* Holds a value from -127 to 128 */ char -Real number types /* May either be unsigned char or signed car depending on the system. This type should be used for character constants. */ int /* Holds a value from -2,147,483,648 to 2,147,483,647 */ unsigned int /* Holds a value from 00000000000000 to 4,294,967,647 */ float /* Single precision floating point */ double /* Double */ Enclose comments in these 14

Enumeration Definitions -Used to name integer values enum operating_system { BSD, /* The value of BSD will be 0 */ LINUX, /* LINUX will be 1, etc. */ MS_WIN, DOS, TOS }; Naming is optional enum { SLACKWARE=23, /* Here we set the first item to 23... */ FEDORA, /* so FEDORA will be 24, etc. */ GENTOO, DEBIAN }; 15

Structure Definitions -A programmer defined type made of variables and other data types. struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; }; You should provide a name Members are declared like variables are normally declared 16

-At definition struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; } particle_a, particle_b; -After definition struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; };... Structure Declarations This way is usually preferred struct particle particle_a, particle_b; 17

Structure Member Access -Use the member access operator, the '.' struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; };... struct particle particle_a, particle_b; particle_a.x = 4; particle_a.y = -4; particle_a.z = 1; particle_a.velocity_x = 1.0; particle_a.velocity_y = 12e3; particle_a.velocity_z = 0; 18

Structure Member Access -Of course, structures may contain other structures struct point { int x, y, z; }; struct velocity { float x, y, z; }; struct particle { struct point p; struct velocity v; };... struct particle particle_a; particle_a.p.x = 4; particle_a.v.x = 1.0; 19

Array Declarations and Access -Specify type and amount when declaring Multidimensional, 10x10 array int my_int_array[10]; struct particle particles[10][10]; -Specify array element when accessing my_int_array[0] = 9; /* The first element */ my_int_array[9] = 46; /* The last element */ particles[3][7].p.x = 4; This structure was defined in the last slide Careful not to overstep array boundaries! The compiler may not warn you about this! my_int_array[10] = 6; my_int_array[-1] = 0; 20

Strings -Strings are just arrays of characters char my_string[20] = "Hello World"; Initialization H e l l o W o r l d \0 my_string[5] = '\0'; The null character Hello World H e l l o \0 W o r l d \0 Hello You cannot assign string literals after initialization! my_string = "Hello World"; But don't worry! There are some library functions that help us copy string literals into arrays. More on that later. 21

Strings -Strings are just arrays of characters char my_string[20] = "Hello World"; Initialization H e l l o W o r l d \0 my_string[5] = '\0'; The null character Hello World H e l l o \0 W o r l d \0 Hello You cannot assign string literals after initialization! my_string = "Hello World"; But don't worry! There are some library functions that help us copy string literals into arrays. More on that later. 22

Strings -Strings are just arrays of characters char my_string[20] = "Hello World"; Initialization H e l l o W o r l d \0 my_string[5] = '\0'; The null character Hello World H e l l o \0 W o r l d \0 Hello You cannot assign string literals after initialization! my_string = "Hello World"; But don't worry! There are some library functions that help us copy string literals into arrays. More on that later. 23

Pointers There is nothing difficult here, just keep the basics in mind. 24

Pointers -Pointers hold memory addresses of stored variables -You can create a pointer for any data type. -The two basic operators: Indirection Address 25

Pointers These are the contents of the memory locations -Keep the memory model in mind These are addresses of the memory locations The denotes uninitialized or junk data in memory 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 26

Pointer Declarations -To declare a pointer of any type, simply use the indirection operator before the 0x00 identifier int *p_to_int; float *p_to_f1, *p_to_f2; -Use caution when declaring multiple pointers at once int *p1, *p2, i1; These are pointers This is an integer 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 27

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 28

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 0x08 0x09 29

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 30

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 0x05 31

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ Don't let this initialization confuse you. All we're really doing is int *p;... p = &i; 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 0x05 32

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 0x05 33

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 4 0x06 0x07 1 0x08 0x09 0x05 34

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 4 0x06 0x07 1 0x08 0x09 0x07 35

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 4 0x06 0x07 7 0x08 0x09 0x07 36

Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; This is called a dereference, it obtains the value stored at the memory location the pointer holds 0x00 0x01 0x02 0x03 0x04 0x05 7 0x06 0x07 7 0x08 0x09 0x07 37

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 38

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l 0x00 0x01 0x02 0 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x09 39

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l 0x00 0x01 0x02 0 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 40

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; Indirect member access operator 0x00 0x01 0x02 0 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 41

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; 0x00 0x01 0x02 2 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 42

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; p->y is the same as (*p).y 0x00 0x01 0x02 2 0x03 4 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 43

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; l.x = p->z; 0x00 0x01 0x02 2 0x03 4 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 44

-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; l.x = p->z; 0x00 0x01 0x02 0 0x03 4 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 45

Type and Storage Specifiers -Common Type Specifier: const const float pi = 3.14159; pi is declared as read only -Common Storage Specifier: extern extern int seconds;... Must have an "extern" and "non-extern" declaration int seconds = 3600; seconds is visible to all files linked the your project 46

Expressions -An expression is one operand and zero or more operators 13 constants may be operands 36 + 16 addition operator calculate_sum(1, 1) functions with return values may be operands (8 * (12 + 4)) use parentheses to group expressions -- innermost expressions are evaluated first 47

Unary Operators Increment: ++x x++ Decrement: --x x-- Positive: +x Negative: -x Logical Negation:!x Bitwise Negation: ~x Address: &x Indirection: *x Size of: sizeof(x) Type casting: (int)x (float)x (type)x Array Subscript: x[3] 48

Binary Operators Addition: x + y Subtraction: x - y Multiplication: x * y Be careful about types Division: x / y Modulus: x % y Bit shift: x << y Bitwise AND: x & y Bitwise OR: x y Bitwise XOR: x ^ y Comparison: x == y x!= y x < y x <= y x > y x >= y Logical: x && y x y Assignment: x = y Compound Assignment: x += y x -= y x *= y x /= y etc. 49

Binary Operators Comma: x, y Member Access: x.y x->y The Ternary Operator x? y : z 50

Next Time -A closer look at the highlighted operators -Statements: actions and control flow -Functions -Program structure -Helpful code snippets 51

52