C Programming Primer 8/31/15 1

Similar documents
CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

EK131 E5 Introduction to Engineering

Another Simple Program: Adding Two Integers

CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I. We are here!

EC 413 Computer Organization

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

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

H192 Midterm 1 Review. Tom Zajdel

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

A Fast Review of C Essentials Part I

Computer Programming for Engineering Applica4ons. Intro to Programming 10/2/13 ECE 175. Limita4ons of Reference by Value. The Concept of Pointers

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

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

BLM2031 Structured Programming. Zeyneb KURT

EL2310 Scientific Programming

Condi(onals and Loops

Introduction to C Programming

CS16 Exam #1 7/17/ Minutes 100 Points total

The C language. Introductory course #1

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

Chapter 5: Control Structures

Lecture 2: C Programming Basic

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

UIC. C Programming Primer. Bharathidasan University

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Differentiate Between Keywords and Identifiers

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Language, Token, Keywords, Constant, variable

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

AMCAT Automata Coding Sample Questions And Answers

C: How to Program. Week /Mar/05

Presented By : Gaurav Juneja

Course Outline Introduction to C-Programming

C introduction: part 1

Introduction to C An overview of the programming language C, syntax, data types and input/output

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

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Programming in C. What is C?... What is C?

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

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

Chapter 2 - Introduction to C Programming

Introduction to C Language

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

Language comparison. C has pointers. Java has references. C++ has pointers and references

Introduction to Computing Lecture 01: Introduction to C

Chapter 2. Introduction to C language. Together Towards A Green Environment

Structured Programming. Dr. Mohamed Khedr Lecture 9

Learning objec<ves. Classes, Objects, and Methods. Problem Decomposi<on. Problem Decomposi<on (cont d)

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Decision Making and Loops

Fundamental of Programming (C)

Work relative to other classes

Introduction. C provides two styles of flow control:

Programming. Data Structure

Chapter 1 & 2 Introduction to C Language

Lecture 6. Statements

Module 4: Decision-making and forming loops

CMPE-013/L. Introduction to C Programming

Chapter-8 DATA TYPES. Introduction. Variable:

COMP 208 Computers in Engineering

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

BASIC ELEMENTS OF A COMPUTER PROGRAM

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Fall 2017 CISC124 9/16/2017

CS313D: ADVANCED PROGRAMMING LANGUAGE

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

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

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

C: How to Program. Week /Apr/23

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Procedures, Parameters, Values and Variables. Steven R. Bagley

Computer Programming

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Use of scanf. scanf("%d", &number);

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Chapter 2. Lexical Elements & Operators

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSE 303 Lecture 8. Intro to C programming

CSC Web Programming. Introduction to JavaScript

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

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

Operators and Control Flow. CS449 Fall 2017

ET156 Introduction to C Programming

C++ Programming Applied to Robotics, Mark Aull Lesson 2: Intro to Visual Studio, debugger Intro to C++, variables, conditionals, loops, strings

Lecture 3. More About C

Programming in C - Part 2

Outline. Pointers arithme.c and others Func.ons & pointers

Transcription:

C Programming Primer 8/31/15 1

Objec5ves Develop a func5onal knowledge of C programming concepts Understand basic variables, constructs, and control flow statements 8/31/15 2

Special Notes Copying and pas5ng code from these slides can be problema5c. It is best to look at these slides and type directly into your programming environment when developing your own programs. 8/31/15 3

What is C? Programming language created between 1969 & 1973 by Dennis Ritchie WriRen to create the UNIX opera5ng system Popular programming language for malware authors 8/31/15 4

Basic Structure of C Program #include <stdio.h> int main(void){ This program will print the following output to a console screen: printf( Hello World \n ); return 0; 8/31/15 5

Basic Structure of C Program #include <stdio.h> Standard Header Files int main(void){ printf( Hello World \n ); return 0; //End of program 8/31/15 6

Standard Header Files Header file contains one or more func5on declara5ons Gives access to previously created func5ons Any number of standard headers can be included stdio.h provides access to various func5ons that allow input and output opera5ons prin^ func5on 8/31/15 7

Basic Structure of C Program #include <stdio.h> Standard Header Files int main(void){ printf( Hello World \n ); return 0; //End of program Main Func5on 8/31/15 8

Main Func5on Global func5on that designates the start of a program Every C program must have a main func5on Func5on may contain slight varia5ons but will always have following structure Return type for the func5on. Body sec5on must return the type specified here int main() { body Name of func5on and parameters to pass to the func5on Actual code to be executed by the main func5on 8/31/15 9

Basic Structure of C Program #include <stdio.h> Standard Header Files int main(void){ printf( Hello World ); return 0; //End of program Main Func5on Print Statement 8/31/15 10

Print Statement prin^ Writes C strings to standard output prin^ is a func5on that is included under the stdio.h header file \n indicates a newline character Not including this on a print statement will print all statements on the same line 8/31/15 11

Print Statement Example #include <stdio.h> int main(){ printf( Hello World \n ); return 0; #include <stdio.h> int main(){ printf( Hello World ); return 0; 8/31/15 12

Basic Structure of C Program #include <stdio.h> Standard Header Files int main(void){ printf( Hello World \n ); return 0; //End of program Main Func5on Print Statement Return Statement 8/31/15 13

Return Statement Returns value from a func5on aher it has been called Value return must be the same as the func5on type A func5on can return any type specified in C If a func5on is of type void, it does not return a value 8/31/15 14

Basic Structure of C Program #include <stdio.h> Standard Header Files int main(void){ printf( Hello World \n ); return 0; //End of program Main Func5on Print Statement Return Statement User Comments 8/31/15 15

User Comments Comments are not executed as part of the program Provides clarity as to what is occurring in a program Comments can be noted by // or /* */ //A C program would not execute //lines such as these. /* Anything between these two symbols would not be executed. */ 8/31/15 16

Semicolons in C No5fy C compiler of the end of a statement They are used aher statements such as: prin^("hello, World! \n"); return 0; 8/31/15 17

Variables Name given to a storage area that computer programs can manipulate Different variable types will be able to represent different types of values Variable names can be composed of lerers, digits, and the underscore character It must begin with an underscore or lerer Variable1 Accepted _Var2 Accepted 13Var3 Not Accepted 8/31/15 18

Variables cont Variables are also case sensi5ve Using upper and lower case lerers creates different variables Example Number number NumbeR The strings above would create three dis5nct variables 8/31/15 19

Variables cont.. Integer Types Floa5ng- Point Types 8/31/15 20

Variable Declara5on Variables can only be declared using valid C data types The general structure is defined below Type variable_name; int num1; char lerer; double price; int num1, num2, num3; char lerer, lererb, lerers; double price, net_price, total; 8/31/15 21

Variable Ini5aliza5on Variables can also be given a value during declara5on int num1=10; char lerer= a ; double price=3.14; int num1=3, num2=17, num3=47; char lerer= V, lererb= b ; double price=3.33, net_price=19.54, total=37.80; 8/31/15 22

Variables Example #include <stdio.h> int main(){ int var = 65; float var2 = 65; char var3 = A ; printf( var %i \n,var); printf( var2 %f \n,var2); printf( var3 %c \n,var3); return 0; 8/31/15 23

More prin^ Info C uses formared output The % sign with a character following it designates a certain format for a variable int num=1; prin^( We are number %i \n, num); Tells func5on to look for integer value Provides integer variable to use in print statement 8/31/15 24

Local vs. Global Variables Local variable- declared inside a func5on Global variable declared outside of all func5ons A local variable can only be used in the func5on where it is declared. A global variable can be used in all func5ons. 8/31/15 25

Global/Local Variables Example #include <stdio.h> int x = 17; int main(){ int y =20/2; x = x*2; printf("value of global variable x: %d\n", x); printf("value of local variable y: %d\n",y); return 0; 8/31/15 26

If Statements Statement in C which tells a program what to execute based on a given condi5on Programs will ohen check if a variable is greater, smaller, or equal to another value Rela5onal Operators > greater than 7 > 4 is TRUE < less than 3 < 9 is TRUE >= greater than or equal 5 >= 5 is TRUE <= less than or equal 2 <=4 is TRUE == equal to 6 == 6 is TRUE 8/31/15!= not equal to 9!=4 is TRUE 27

If Statement Structure if (statement is TRUE){ Execute line of code int x =15; if (x > 10){ printf( Greater than ten ); Example of how statements work. The variable x is equal to 15. x is greater than 10, so the print statement will be executed. int x = 9 if (x!= 9){ printf( Not equal to 9 ); The variable x is equal to 9. Since x is equal to 9 the print statement will not execute. It only executes when x is not equal to 9. 8/31/15 28

If/Else Statements Else statements add more control to how a program can be executed By using else statements addi5onal condi5ons can be checked if (statement is TRUE){ Execute these lines of code if condition is TRUE else{ Execute these lines of code if condition is FALSE 8/31/15 29

If/Else Examples void main(){ int x = 12; if( x < 19){ printf( x is less than 19 ); else{ printf( x is not less than 19 ); x is equal to 12. The program checks the first if statement. x < 19 is TRUE so it executes the first print statement. The else statement is not considered since the if statement was TRUE. 8/31/15 30

If/Else Examples cont.. void main(){ int y = 30; if( x < 19){ printf( x is less than 19 ); else{ printf( x is not less than 19 ); x is equal to 30. The program checks the first if statement. x < 19 is FALSE. It does not enter into the if statement. It does enter the else statement and prints x is not less than 19. 8/31/15 31

Loops Used to perform repeated opera5ons un5l a condi5on is reached Like if statements, loops use rela5onal operators and condi5on statements to determine how long to execute Here while loops and for loops will be studied 8/31/15 32

Two components Test condi5on Counter modifica5on while (condition is TRUE){ x=0 While Loop -Execute lines of code -Modify variable that affects test condition while (x < 10){ printf( Hello ); x++; //Increments x by 1 8/31/15 33

While Loop Example #include <stdio.h> int main(){ int counter=10; while (counter > 0){ printf("counter is greater than 0\n"); counter--; printf("counter is %d\n",counter); return 0; 8/31/15 34

While Loops & If Statements #include <stdio.h> int main(){ int counter=10; while (counter > 0){ if (counter > 5){ else{ printf("counter is greater than 5\n"); printf("counter is less than or equal to 5\n"); counter--; printf("counter is %d\n",counter); return 0; 8/31/15 35

While Loops & If Statements 8/31/15 36

For Loops Three components Variable ini5aliza5on Test condi5on Variable modifica5on for (variable ini5aliza5on; test condi5on; variable modifica5on){ Code to execute when the test condi5on is true 8/31/15 37

For Loop Example #include <stdio.h> void main(){ int x; for(x=0;x<10;x++){ printf( x is %i\n,x); 8/31/15 38

Switch Statements Helps control complex condi5onal and branching opera5ons Takes a variable and test it for equality against a set of values switch ( <variable> ) { case this- value: Code to execute if <variable> == this- value break; default: Code to execute if <variable> does not equal the value following any of the cases break; 8/31/15 39

void main(){ int a = 10; const int b = 10; const int c = 20; Switch Statement Example switch ( a ) { case 10: printf( a equals b ); break; case 20: default: printf( a equals c ); break; printf( Execute default case ); break; 8/31/15 40

Array Data Structures C data structure that stores a fixed size sequen5al collec5on of elements Can be thought of a collec5on of variables of same type Parts of the array can be accessed via an index Declaring Array s type arrayname [arraysize]; Actual Example int numbers[10]; 8/31/15 41

Array Example int number[5]; number[0]=5; number[1]=0 number[2]=17; number[3]=81; number[4]=52; 5 0 17 81 52 0 1 2 3 4 Index Posi5ons **Every array starts at index 0** 8/31/15 42

Array Examples #include <stdio.h> void main(){ int numbers[10]; int x; for(x=0;x<10;x++){ numbers[x]=x; printf( Value is %i\n,numbers[x]); 8/31/15 43

Addi5onal Material Links for addi5on material hrp://www.tutorialspoint.com/cprogramming/ cprogramming_tutorial.pdf hrp://www.learn- c.org/ 8/31/15 44

Summary Presented basic C programming constructs Discussed basic variables, constructs, and control flow statements 8/31/15 45

Ques5ons 8/31/15 46