Pemrograman C - Gadjah Mada University

Download Report

Transcript Pemrograman C - Gadjah Mada University

Pemrograman C Risanuri Hidayat

Why C

         Compact, fast, and powerful “Mid-level” Language Standard for program development (

wide acceptance

) It is everywhere! (

portable

) Supports modular programming style Useful for all applications C is the native language of UNIX Easy to interface with system devices/assembly routines C is terse

Structure in C

#include main() { /* My first program */ printf("Hello Sayang…. \n"); }

Listing 1. hello.c

 

C is case sensitive

. All commands in C must be lowercase.

C has a free-form line structure

.

End of each statement must be marked with a semicolon

. Multiple statements can be on the same line.

White space

is ignored. Statements can continue over many lines.

Structure in C

 The C program starting point is identified by the word

main().

The two braces, { and }, signify the begin and end segments of the program.

#include

is used to allow the

printf

statement to provide program output. 

printf()

is actually a function (procedure) in C that is used for printing variables and text. Where text appears in double quotes

""

, it is printed without modification, with some exceptions.

Structure in C

    Keluaran layar: Hello Sayang….

Comments can be inserted into C programs by bracketing text with the

/*

and

*/

delimiters.

/* My first program */ Header files contain definitions of functions and variables #include

statement.

which can be incorporated into any C program by using the pre-processor To use any of the standard functions, the appropriate header file should be included. This is done at the beginning of the C source file. For example, to use the function

printf()

in a program, the line

#include

Structure in C

#include

#include

#include "mylib.h"

 The use of angle brackets

<>

informs the compiler to search the compiler’s include directories for the specified file.  The use of the double quotes

""

around the filename informs the compiler to start the search in the current directory for the specified file.

Structure in C

Identifiers in C

must begin with a character or underscore

, and may be followed by any combination of characters, underscores, or the digits 0-9.

summary

Jerry7 exit_flag Number_of_moves i _id

Structure in C

Keywords

– are reserved identifiers that have strict meaning to the C compiler. C only has 29 keywords. Example keywords are:

if, else, char, int, while

 Names given to values that cannot be changed. Implemented with the –

#define

preprocessor directive.

#define N 3000

– – –

#define FALSE 0 #define PI 3.14159

#define FIGURE "triangle"

Structure in C

    Preprocessor statements begin with a

#

listed at the beginning of the source file.

symbol, and are NOT terminated by a semicolon. Traditionally, preprocessor statements are Preprocessor statements are handled by the compiler (or preprocessor) before the program is actually compiled. Once this substitution has taken place by the preprocessor, the program is then compiled.

In general, preprocessor constants are written in UPPERCASE. This acts as a form of internal documentation to

enhance program readability and reuse

.

In the program itself, values cannot be assigned to symbolic constants.

Example

#include #define TAXRATE 0.10

main () { float balance; float tax; balance = 72.10; tax = balance * TAXRATE; printf("The tax on %.2f is %.2f\n",balance, tax); } The tax on 72.10 is 7.21