C Programming Getting started Variables Basic C operators Conditionals

Slides:



Advertisements
Similar presentations
C Programming Getting Started Getting Started Hello World Hello World Data types and variables Data types and variables main() main() I/O routines I/O.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Programming Languages and Paradigms The C Programming Language.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
True or false A variable of type char can hold the value 301. ( F )
Structure of a C program
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Khalid Rasheed Shaikh Computer Programming Theory 1.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
C is a high level language (HLL)
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Programming Languages and Paradigms
Programming Paradigms
C Basics.
Introduction to C Programming Language
Chapter 2 - Introduction to C Programming
Variable Names Names are a sequence of visible characters
Introduction to C Programming
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Module 2 Variables, Data Types and Arithmetic
Programming Languages and Paradigms
An Overview of C.
Introduction to C Programming
INTRODUCTION TO C.
Presentation transcript:

C Programming Getting started Variables Basic C operators Conditionals Loops Functions

Getting started

Hello world example #include <stdio.h> main() { printf(“hello, world\n”); } Prints “hello, world” to the screen Type this in with a text editor and save it as hello.c

Breaking down hello.c #include <stdio.h> Tells the compiler to use the library functions described in stdio.h printf() function is inside stdio

Breaking down hello.c main() Beginning of the main function All code execution starts at main() {} Curly brackets group lines of code All functions start and end with curly brackets

Breaking down hello.c printf(…); Prints to the screen The argument is in parenthesis The argument is what is printed Note the semicolon at the end

Breaking down hello.c ”hello, world\n” This is the argument to printf() which appears on the screen It is a string because it is in quotes (“”) \n is a special character that indicates newline

Variables

Variables Names that represents values in the program Similar to algebraic variables All variables have a type which must be declared int x; float y; Type determines how arithmetic is performed, how much memory is required

Types and type qualifiers Several built-in types, different sizes Type qualifiers exist: short, long char is 8 bits on all platforms Type Size char 1 byte Fixed size int 16 bit minimum Typically, word size float 64 bits, typical Floating point double 64, 128 bits, typical Double precision

Variable names A sequence of visible characters Must start with a non-numerical character int testval2 (O) int 2testval (X) No C language keywords if, else, while (X)

Constants Can use #define compiler directive #define ANSWER 42 Any instance of the string is substituted at compile time Character constants Written as a single character in single quotes #define TERMINATOR ‘x’ Integer equal to the ASCII value of the character Some characters are not easy to represent (i.e., bell)

Global variables int global_i; void foo() { extern int global_i; } A variable is global if it is defined outside of any function A global variable must be declared as an extern in any function using it extern not needed if global declaration is before the function Variables can be global across files

Globals can be dangerous void foo() { void bar () { extern int global_i; extern int global_i; … … global_i = a+b–c; x = global_i*… ; } } Global variables can propagate bugs Bug in foo can cause bar to crash Debugging can become harder Reduce modularity of the code

Basic C operators

Arithmetic/Relational operators + (addition), - (subtraction),* (multiplication), / (division) % is the modulo operator, division remainder 9 % 2 = 1 9 % 3 = 0 ++ (increment), -- (decrement) ==, <, >, <=, >=, != if (x<5) …

Logical operators && (and), || (or), ! (not) Treat arguments as 1-bit binary values 0 is FALSE !0 is TRUE if ( (A==1) && !B ) The above is TRUE if A is TRUE and B is FALSE

Conditionals

Conditional statements if (expression) if (expr1) statement1 state1 else else if (expr2) statement2 state2 else state3 else is optional expression is evaluated Execute statement1 if TRUE, statement2 if FALSE expr2 evaluated if expr1 is FALSE

Conditional example int main() { int x=1; if (x==1) printf(“Correct”); else printf(“Incorrect”); }

Switch switch (expression) { case condition1: statement1 default: statement3 } expression is evaluated, compared to conditions statements corresponding to the first matching conditions are executed default is optional

Break in a switch switch (x) { case 0: y=1; case 1: y=2; break; } Without a break statement, the case will not end If x==0 then both y=1; and y=2; are executed

Loops

For loops for (expr1; expr2; expr3) { statement } Initialization and increment are built into the for loop

While and do-while loops expr1; expr1; while (expr2) { do { statement statement expr3; expr3; } } while (expr2); Condition checked at the top of a while loop Condition checked at the bottom of do-while loop

While example int main() { int i=0; while (i<3) { printf(“%i”, i); } Three passes through the loop; i=0,1,2 Exits loop when i=3

All built into the for statement int main() { int i; for (i=0; i<3; i++) { printf(“%i”, i); } Initialization: i=0 Termination: i<3 Step: i++

Break and continue while (x>5) { while (x>5) { y++; y++; if (y<3) break; if (y<3) continue; x++; x++; } } break jumps to the end of a loop continue jumps to the next iteration of a loop

Functions

Functions void foo(int a, int b) { // function definition int x, temp; temp = a + b; x = temp; printf(“%i”,x); } int main() { foo(2,3); // function call Functions can replace groups of instructions Data can be passed to functions as arguments

Function return value int foo(int a, int b) { // function definition int x; x = a + b; return x; } int main() { printf(“%i”, foo(2,3)); // function call Functions can return a value to the caller The type of the return value must be declared

Lab

Blink example extended Come back to the Blink example. Write a function void blink_n_times(int n, int ms) which turns the on-board LED on for ms milliseconds and off for ms milliseconds n times

Blink example extended Use for loops and blink_n_times() function from loop() to have your Arduino repeat the following ON OFF For the first 1 second Next second Next second Next second Next second