LectureIV-Programming in 'C'.pptx

# LectureIV-Programming in 'C'.pptx - C Language Types...

• Notes
• 28

This preview shows pages 1–11. Sign up to view the full content.

Types, Control Flow, and Functions 1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University C Language

This preview has intentionally blurred sections. Sign up to view the full version.

Contents 2 Introduction Data Types Arrays & Functions Control Flow Program Structure
Introduction: Example ‘C’ code 3 #include ... /* print Fahrenheit-Celsius table for fahr = 0, 20, 40, ..., 300, using a loop */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; // upper limit step = 20; // step size fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; // integer value? printf(" %d\t%d\n ", fahr, celsius); fahr = fahr + step; } } ex13.c ex13.c

This preview has intentionally blurred sections. Sign up to view the full version.

printf(" %d \t %d \n", fahr, celsius); %d specifies an integer argument (d: d ecimal). Output 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 ……..
5 Is there any problem in the above output? The Celsius temperatures in the previous out are not accurate. For example 0 o F is -17.8 o C. Then? We can use float data type for fahr and celsius instead of int . float fahr, celsius; Then how to print real numbers using printf() ? celsius = 5 * (fahr-32) / 9; // okay? printf(" %3.0f \t %6.1f \n", fahr, celsius); %f specifies a floating point argument (f: f loating point). 6.1 means Print 6 digits before the decimal point (called width) and one digit after the decimal point (called precision). ex14.c ex14.c

This preview has intentionally blurred sections. Sign up to view the full version.

6 Some easy errors: printf(" %d \t %6.1f \n", fahr, celsius); ??? printf(" %3.0f \t %6.1f \n", fahr); ??? printf(" %6.1f \n", fahr, celsius); ???
Review 7

This preview has intentionally blurred sections. Sign up to view the full version.

Boolean values in ‘C’ 8 When evaluating integers as Booleans in C, are negative numbers true or false? A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
9 If -Else

This preview has intentionally blurred sections. Sign up to view the full version.

Arrays 10 /* * C Program to Find the Largest Number in an Array */ #include <stdio.h> int main() { int array[50], size, i, largest;
This is the end of the preview. Sign up to access the rest of the document.
• Spring '17
• ANKITA
• Math, Global variable, fahr

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern