C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Programming Languages and Paradigms The C Programming Language.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Makefiles Tutorial adapted from and myMakeTutorial.txt.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 5: Control Structures II (Repetition)
Guide To UNIX Using Linux Third Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
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.
Homework Reading Programming Assignments
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4 Program Control Statements
Fundamentals of C and C++ Programming Control Structures and Functions.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
CPS120: Introduction to Computer Science Decision Making in Programs.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 2.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Windows Programming Lecture 03. Pointers and Arrays.
Makefiles CSSE 332 Operating Systems
Chapter 4 – C Program Control
What Is? function predefined, programmer-defined
Debugging with gdb gdb is the GNU debugger on our CS machines.
- Additional C Statements
Program Control Structures
C Preprocessor(CPP).
Chapter 4 - Program Control
Introduction to C Topics Compilation Using the gcc Compiler
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
Homework Any Questions?.
Homework Reading Programming Assignments Finish K&R Chapter 1
Introduction to C Topics Compilation Using the gcc Compiler
Appendix F C Programming Environment on UNIX Systems
COP 3330 Object-oriented Programming in C++
Fundamental Programming
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Chapter 11 Programming in C
What Is? function predefined, programmer-defined
CSC215 Lecture Control Flow.
Presentation transcript:

C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging

Type Conversions main() { int i; unsigned int stop_val; stop_val = 0; i = -10; while (i <= stop_val) { printf ("%d\n", i); i = i + 1; }

Common mistake  Example: int n = 0; while (n < 3) printf(“ n is %d\n”, n); n++;

Troubles with Truth status = scanf(“%ld”, &num); while (status = 1) { printf(“please enter a number\n”); status = scanf(“%ld”, &num); }

The for loop  Syntax: for (exp1; exp2; exp3)  Example: for (count = 1; count < 10; count++) { printf(“count is %d\n”, count); }

Count by charaters char count; for (count = ‘a’; count <= ‘z’; count++) { printf(“count is %c\n”, count); }

Nested loop Example: for (row = 0; row < 10; row++) { for (ch = ‘a’; ch < ‘f’; ch++) { printf(“%c “, ch); }

C Control Statements  if statement  Syntax: if (expression) { statements } else { statements }

Sample code if (x > 0) { printf(“ x is greater than 0.\n”); } else { printf(“ x is less than or equals to 0.\n”); }

Another example int main() { char ch; ch = getchar(); while (ch != ‘\n’) { putchar(ch); ch = getchar(); } return 0; }

Using continue and break ch = getchar(); while (ch != ‘\n’) { if (ch == ‘q’) break; if (ch == ‘$’) { printf(“dollar!\n”); continue; } putchar(ch); ch = getchar(); } printf(“finished.\n”);

Continue and break  continue skips the rest of current iteration and starts the next iteration.  break causes the program to break free of the loop that encloses it and to proceed to the next stage of the program.

Multiple choice: switch and break switch (ch) { case ‘\n’: printf(“newline\n”); break; case ‘q’: exit(0); case ‘$’: printf(“dollor!.\n”); break; default: putchar(ch); break; }

Arrays  An array is composed of a series of elements of one data type.  An array declaration tells the compiler how many elements the array contains and what the type is for these elements.  Example: float candy[365]; char code[12];

Assigning Array Values  Initialization. int arr[6] = {0, 1, 2, 3, 4, 5};  Assigning values. for (counter = 0; counter < 6; counter++) { arr[counter] = counter; }

Multidimensional Arrays  An two dimension array is an array of one dimension arrays.  Example: float rain[5][12]; rain is an array of five elements. Each element is an array of twelve float point numbers.

Pointers and Arrays  Pointers provide a symbolic way to use address.  Array notation is simply a disguised use of pointers. rain = &rain[0] // true

Functions, Arrays and Pointers  Suppose you want to write a function that operates on an array. What would the function call look like? total = sum(marble);  What would the function declaration be? int sum(int *ar);  NOTE: array name is the address of the first elements.

Pointer Operations  Example: int urn[5] = {1, 2, 3, 4, 5}; int *ptr1, *ptr2, *ptr3; ptr1 = urn; ptr2 = &urn[2];  Question: if ptr1[x] == ptr2[1] is true, what is x?

C Preprocessor  #include (or “string.h”) There is a difference between the -- look for the file in the include path and “ “ look first in local directory, then in the include path  #define [name] #define [name] value When [name] is used it is replaced with its value Eg. #define DEBUG #define DEBUG 1  #ifdef [name] #ifndef [name]  #endif

Examples #ifdef DEBUG printf ("entering main ()\n"); #endif … #define TRUE 1 #define FALSE 0 #define BYTE unsigned char BYTE success; success = function (); if (success == FALSE); { #ifdef DEBUG printf ("error\n”); #endif exit (0); }

Makefile  The make command allows you to manage large programs or groups of programs.  The make program keeps track of which portions of the entire program have been changed, compiles only those parts of the program which have changed since the last compile.

Compilation Steps 1.Compiler stage: All C language code in the.c file is converted into a lower-level language called Assembly language. 2.Assembler stage: The assembly language code is converted into object code. 3.Linker stage: The final stage in compiling a program involves linking the object code to code libraries and produces an executable file.

Compile Multiple files  Programmers usually divide source code into separate easily-manageable.c files when programs become large.divide

Separate Compilation Steps  Compile green.o: cc -c green.c  Compile blue.o: cc -c blue.c  Link the parts together: cc green.o blue.o

Using multiple source files  no two files have functions with the same name in it.  no two files define the same global variables.  To use functions from another file, make a.h file with the function prototypes, and use #include to include those.h files within your.c files.  At least one of the files must have a main() function.

Dependencies  Make creates programs according to the file dependencies.

Dependency graphs

How dependency works

How does make do it?

Translating the dependency graph target : source file(s) command (must be preceded by a tab)

Using the Makefile with make  Once you have created your Makefile and your corresponding source files, you are ready to use it by typing make.

Basic Debugging  Prepare code for debugger add –g option to compiler command gcc –g –o  Debug the program gdb  Breakpoints and execution b OR s – step c – continue n – next  Look at variables print