Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 206 Structured Programming in C

Similar presentations


Presentation on theme: "CSCE 206 Structured Programming in C"— Presentation transcript:

1 CSCE 206 Structured Programming in C
Ki Hwan Yum

2 Chapter 1 Writing an ANSI C Program

3 CSCE 206 Struectured Programming in C
Basic Terms Programs: instruct machines to carry out specific tasks or to solve specific problems. Algorithm: a step-by-step procedure that accomplishes a desired task. Computer: a digital electronic machine composed of 3 main components: processor, memory, input/output. The processor carries out instructions stored in the memory. CSCE 206 Struectured Programming in C

4 CSCE 206 Struectured Programming in C
Basic Terms Operating System: a collection of special programs OS oversees and coordinates the resources of the machine. OS provides tools to users. (e.g. text editor, C compiler, etc.) C code is called source code. A file containing source code is called a source file. CSCE 206 Struectured Programming in C

5 CSCE 206 Struectured Programming in C
First Program – hello.c /* First program */ #include <stdio.h> int main(void) { printf(“Hello, world!\n”); return 0; } CSCE 206 Struectured Programming in C

6 CSCE 206 Struectured Programming in C
Dissection of hello.c /* ……… */: comments. Ignored by the compiler. Lines that begin with #: preprocessing directives. #include causes the preprocessor to include a copy of the standard header file stdio.h at this point in the code. The angle brackets(< >) indicate that this file is to be found in the “usual place”. This stdio.h file is included because it contains information about the printf() function. CSCE 206 Struectured Programming in C

7 CSCE 206 Struectured Programming in C
Dissection of hello.c {: A left brace begins the body of each function. A corresponding right brace must end the function. Braces are also used to group statements together. printf(): a standard library function that prints on the screen. “Hello, world\n”: string constant surrounded by double quotes. \n represent a single character called newline. CSCE 206 Struectured Programming in C

8 CSCE 206 Struectured Programming in C
Dissection of hello.c All declarations and statements in C end with a semicolon. return 0: zero is returned by main() to the OS. Zero means the program completed successfully. Nonzero values are used to tell the OS that main() has been unsuccessful. }: This right brace matches the left brace and ends main(). CSCE 206 Struectured Programming in C

9 CSCE 206 Struectured Programming in C
Another hello.c #include <stdio.h> int main(void) { printf(“Hello, “); printf(“world!\n”); return 0; } Looks different, but the output is the same. CSCE 206 Struectured Programming in C

10 Adding “Hello, universe!”
#include <stdio.h> int main(void) { printf(“Hello, world!\n“); printf(“Hello, universe!\n”); return 0; } Two printf() statements can be replace with one printf() statement. printf(“Hello, world!\nHello, universe!\n); CSCE 206 Struectured Programming in C

11 CSCE 206 Struectured Programming in C
Variables Variables are used to store values. Different kinds of variables are used to store different kinds of data. => The type of each variable must be specified. (integer, float, etc.) CSCE 206 Struectured Programming in C

12 Example Program - Hesperus
Algorithm for the Hesperus Assign the number of fathoms to a variable. Convert fathoms to feet and store in a variable. Convert feet to inches and store in a variable. Print the different units of measure neatly on the screen. CSCE 206 Struectured Programming in C

13 CSCE 206 Struectured Programming in C
#include <stdio.h> int main(void) { int inches, feet, fathoms; fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“%d fathoms\n”, fathoms); printf(“%d feet\n”, feet); printf(“%d inches\n”, inches); return 0; } CSCE 206 Struectured Programming in C

14 Dissection of the Program
In order to use printf(), we need to include the standard header file stdio.h. int inches, feet, fathoms;: A declaration. The variables are declared to be of type int (integer), one of the fundamental types in C. All variables in a program must be declared before they can be used. Declarations end with a semicolon. CSCE 206 Struectured Programming in C

15 Dissection of the Program
fathoms = 7;: This is an assignment statement. = is the basic assignment operator in C. The value of the expression on the right side of = is assigned to the variable on the left side. *: multiplication operator. printf(“%d fathoms\n”, fathoms);: printf() has two arguments. The first argument is always a string, called the control string. %d is called a format that causes the value of the expression in the second argument. CSCE 206 Struectured Programming in C

16 General Form of a Simple Program
preprocessing directives int main(void) { declarations statements } CSCE 206 Struectured Programming in C

17 Declarations and Statements
The declarations must come before the statements. They tell the compiler what kind of data can be stored in each of the variables. The statements in the program carry out the desired computations and display information on the screen. CSCE 206 Struectured Programming in C

18 CSCE 206 Struectured Programming in C
Identifiers A variable name (identifier) consists of letters, digits and underscores. It may not begin with a digit. Identifiers should be chosen to reflect their use in the program. They can serve as documentation. Make the program more readable. Certain keywords (reserved words) cannot be used as identifiers. (char, int, for, while, etc.) See p. 46 for all keywords in C. CSCE 206 Struectured Programming in C

19 CSCE 206 Struectured Programming in C
Expressions Expressions typically are found on the right side of assignment operators and as arguments to functions. Simplest: constants (e.g. 6), a variable Combinations of operators with variables and constants Binary arithmetic operators: +, -, *, /, % Integer division: the result is an integer 29/5 = %: Modulus operator. The remainder after division 29%5= CSCE 206 Struectured Programming in C

20 CSCE 206 Struectured Programming in C
char Type character type #include <stdio.h> int main(void) { char c; c = ‘A’; printf(“%c\n”, c); return 0; } CSCE 206 Struectured Programming in C

21 CSCE 206 Struectured Programming in C
Floating Types 3 floating types: float, double, long double used to manipulate real numbers (or floating numbers). integer 7 ≠ floating 7.0 CSCE 206 Struectured Programming in C

22 CSCE 206 Struectured Programming in C
Example #include <stdio.h> int main(void) { float x, y; x = 1.0; y = 2.0; printf(“The sum is %f\n”, x+y); return 0; } CSCE 206 Struectured Programming in C

23 CSCE 206 Struectured Programming in C
Initialization When variables are declared, they may also be initialized. char c = ‘A’; int i = 1; CSCE 206 Struectured Programming in C

24 CSCE 206 Struectured Programming in C
#define and #include #include <filename>: The preprocessor looks for the file only in standard places (typically /usr/include). #include “filename”: A search for the file is made first in the current directory and then in other system-dependent places. #define LIMIT 10: The preprocessor changes all occurrences of LIMIT to 10. CSCE 206 Struectured Programming in C

25 CSCE 206 Struectured Programming in C
printf() and scanf() printf() is used for printing formatted output. scanf() is used for reading formatted input. Both have a list of arguments. control_string and other_arguments CSCE 206 Struectured Programming in C

26 printf() Conversion Characters
How the corresponding argument is printed c character d decimal integer e floating-point number in scientific notation f floating-point number g e-format or f-format, whichever is shorter s string CSCE 206 Struectured Programming in C

27 CSCE 206 Struectured Programming in C
Example printf(“Get set: %s %d %f %c%c\n”, “one”, 2, 3.33, ‘G’, ‘0’); CSCE 206 Struectured Programming in C

28 scanf() Conversion Characters
How characters in the input stream are converted c character d decimal integer f floating-point number (float) lf floating-point number (double) Lf floating-point number (long double) s string CSCE 206 Struectured Programming in C

29 CSCE 206 Struectured Programming in C
Example char first, middle, last; int age; scanf(“%c%c%c%d”, &first, &middle, &last, &age); CSCE 206 Struectured Programming in C

30 CSCE 206 Struectured Programming in C
while Statement Performs a repetitive action. Its general form is while (expression) statement CSCE 206 Struectured Programming in C

31 CSCE 206 Struectured Programming in C
Example #include <stdio.h> int main(void) { int i = 1, sum = 0; while (i <= 10) { sum = sum + i; i = i + 1; } printf(“Sum = %d\n”, sum); return 0; CSCE 206 Struectured Programming in C


Download ppt "CSCE 206 Structured Programming in C"

Similar presentations


Ads by Google