Sie sind auf Seite 1von 53

COMP6599 – Algorithm and

Programming
Week 2
Session 3
Format Input Output
Sub Topics
Format Input & Output
• C Standard library
• C Structure
• Comments in C
• Escape Sequences in C
• Character, Identifier, Keyword in C
• Variable
• Data Type
• Constant
• Size Of
• Input & Output Operation
• Input & Output Formatting
• Input & Output Example
C STANDARD LIBRARY
C Standard Library
When programming in C, you’ll typically use the following
building blocks:

•C Standard Library Functions


Example:
- <math.h> : Mathematical Functions
- <stdio.h> : Input and Output
- <stdlib.h> : Utility Functions
- <string.h> : String Functions
- <time.h> : Time and Date Functions
•Functions you create yourself
•Functions other people have created and made available to
you
C STRUCTURE
C Sturcture
• C language is a structural programming language
• It consists of functions
• There is no separation between function and
procedure (if you are from Pascal language
background)
• Each C program has one main function called main
• Program will be started from the first line of the main
function
• C language is case sensitive
• Every statement should be ended with a semi-colon (;)
C Sturcture
• C language is a structural programming language
• It consists of functions
• There is no separation between function and procedure
(if you are from Pascal language background)
• Each C program has one main function called main
• Program will be started from the first line of the main
function
• C language is case sensitive
• Every statement should be ended with a semi-colon (;)
C Sturcture
main() main()
1. { 3. {
statements; statements;
} return(0);
}

void main() int main()


2. { 4. {
statements; statements;
} return(0);
}
C Sturcture
• However, not all C compiler is familiar with all main function
format described previously
• No. 2 and 4 would be a general/standard main function
format
• return(0), suggesting a normal program exit
• A default integer (int) data type will be given for every function as
a default. No. 3 and 4 have the same meaning
• Example:
 using Turbo C 2.0 (DOS) and Microsoft Visual C++ (windows)
compiler, (2), and (4)  success, but (1) and (3) warning
 using Dev-C (windows) and gcc (linux) (1), (3), and (4) 
success, but (2) warning
C Sturcture
Using Turbo C 2.0, the
int main()
{ code will result in error.
printf (“Welcome to BINUS\n”); Error Message:
return 0;
} Function should have a
function prototype

#include is a directive
#include<stdio.h>
int main() command to tell the
{ computer to search for
printf (“Welcome to BINUS\n”);
return(0); printf function prototype
} at header file stdio.h as
well
C Sturcture
• Directive #include generally written at the beginning
of a program
• Coding Style (depends to the programmer)
COMMENTS IN C
Comments
• Used for readability of the program
• Not accounted as a command/statement by the compiler
• Using /* and */
• Using // at the beginning of line for one line comment
• Example:
/*--------------------------
My First Program
--------------------------*/
#include<stdio.h>
void main(){
printf (“Hello, BINUSIAN\n”);

}
// This program will simply print out a message
ESCAPE SEQUENCE IN C
Escape Sequences
• \a bell, alert, system beep
• \b back space
• \t horizontal tab
• \n new line, line feed
• \v vertical tab
• \r carriage return
• \’ single quote
• \” double quote
• \\ backslash
• \xdd hexadecimal notation
• \ddd octal notation
CHARACTER , IDENTIFIER, AND
KEYWORD IN C
Character in C
• C program is written using ASCII character subset:
- Capital letters A…Z
- Lower Case a…z
- Digit 0…9
- Special characters ‘!’, ‘&’, ‘+’, ‘\’, ‘_’, etc.

• ASCII
American Standards Committee for Information
Interchange
http://www.asciitable.com/
Identifier
• The naming mechanism for various element in a
program such as: variable, function, constant, etc.
• Started with a letter or underscore_
• It is case sensitive
• Maximum length is vary for every compiler
Example: Turbo 2.0 (DOS), max 32 characters
• Never use reserved word/keyword
(such as: for, while, if, main)
• Example:
name, x1, _total, cubic()
wrong: 1time, int
Keywords
• Keywords/reserved words are words that have special
meaning to the C compiler.
• Example:
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
VARIABLE
Variable
• Identifier for storing data/information
• Each variable has its name, address (L-value), type, size
and data (R-value)
• Data or variable value can be modified at run time
• Declaration format:
<data type> <variable name>;
<data type> <variable name> = <initial
value>;
• Example:
int a, b, c, total;
float salary, bonus;
int num_students = 20;
Variable
• Variable Declaration:
– Variable can be declared at every statement block
– Block statement or compound statement is
statement exists between { and } sign
– Example: int x;
int y;
int z;

or:
int x, y, z;

or:

int x; int y; int z;


DATA TYPE
Data Type
• In C, there are 5 data types and 4 modifiers
Data types:
– Character  char
– Integer  int
– Floating point  float
– Double floating point  double
– Void  void
Modifiers:
- signed
- unsigned
- long
- short
Data Type
DATA TYPE SYNTAX MEMORY RANGE

character unsigned char 1 byte 0 to 255

char 1 byte -128 to 127

integer unsigned int 2 byte 0 to 65535

int 2 byte -32768 to 32767

short int 1 byte -128 to 127

unsigned long 4 byte 0 to 4294967295

long 4 byte -2147483648 to


2147483647
float float 4 byte 3.4E-38 to 3.4E+38

double 8 byte 1.7E-308 to 1.7E+308

long double 16 byte 3.4E-4932 to 1.1E+4932


CONSTANT
Constant
Constant / symbolic constant does not have address (only value) and its value can not
be changed at run time.
Constant type:
 Integer constant  -5
 Floating-point constant  3.14
 Character constant  'C' '1' '$'
 Escape sequence  \n \t \''
 String constant  ''BiNus''
Symbolic constant  #define PHI 3.14
 const float PHI=3.14;
 'H‘ is a character constant
 ''H'‘ is a string constant
 1 is a integer constant
 '1‘ is a character constant
 const float Pi= 3.1415926; Pi is a symbolic constant
SIZEOF
Sizeof
• sizeof is an operator to find out size of a data type in
C language

• Syntax: sizeof expression

• Example :
sizeof(int) = 4 => Dev-V (Windows)
sizeof(int) = 2 => Turbo C ver 2.0 (DOS)
INPUT & OUTPUT
OPERATION
Output Operation
• To show data on the display screen/monitor. Some of
standard library function in C :
printf();
putchar();
puts();
etc.
Output Operation:
printf() function

• To display some data on the standard output, using


certain format

• Standard output is the monitor.

• Syntax:
printf(const char *format[,argument, …]);

• Header file : stdio.h


Output Operation:
putchar() function
• Syntax:
int putchar(int c)

• Functionality:
– Displaying character on the monitor at cursor position.
After display, cursor will move to the next position
– Return EOF if error, and return the displayed character
after successfully done
– putchar is a macro similar to : putc(c, stdout )
– Header File : stdio.h

• Example :
char ch=’A’;
putchar(ch);
Output Operation:
puts() function
• Syntax:
int puts(const char *str);

• Functionality :
– Display string to the monitor and move the cursor to new line
– Return non-negative value when successful and EOF if error
– Header file: stdio.h

• Example :
puts(”Welcome”);
puts(”to Binus”);
Output on monitor:
Welcome
to Binus
Input Operation
• Standard library function that is related to input operations
are:
scanf();
getchar();
gets();
etc.

• Input operation: function/operation of getting the data into


the memory using standard I/O devices (keyboard, disk,
etc.)
Input Operation:
scanf() function
• Header file: stdio.h
• Format:
int scanf( const char *format [,argument]... );
• All the argument type are pointers (address of a variable)
• To get the address of a variable use “&” sign
• Example :
int aValue;
scanf(”%d”,&aValue);

• Input format: ”%type”


where type can be substituted with one of the following list:
(next page)
Input Operation:
scanf() function

• Format Type :
Type Used to scan
d integer
u unsigned integer
x hexadecimal
e, f, g floating point
C single character
s string ended with whit space
O data unsigned octal
[…] string ended with non of the value inside [...]
[^..] string ended with the value inside [...]
Input Operation:
scanf() function
• If exist an x integer variable, state the difference of x
and &x?
Variable Name Address Value
X 45678 234

Answer:
x : 234
&x : 45678
Input Operation:
scanf() function
• scanf() function returns an integer that stated how many
fields are successfully assigned

• Example :
int x,y,z,w;
x = scanf("%d %d %d",&y,&z,&w);

– Input three values of integer 6 7 8, then x = 3;


– Input four values 6 7 8 9 then x = 3 (successfully
assign 3 variables y z w)
Input Operation:
getchar() function
• Syntax:
int getchar(void);

• Functionality:
– Return the next ASCII character from keyboard buffer
– Shown on the monitor screen
– Awaiting for ENTER pressed
– Header file: stdio.h

• Example :
char ch;
ch = getchar();
Input Operation:
gets() function
• Syntax:
char *gets(char *buffer)

• Functionality :
– read a string from keyboard till find new-line and save in
buffer
– new-line will later on replace with null character
– will return NULL if error and return its argument (buffer) if
success

• Example :
char buffer[40];
char *ptr;
ptr = gets(buffer);
INPUT & OUTPUT
FORMATTING
Output Formatting
• Output also has formatted specification:
%[flags][width][.precision] type

width : number of columns provided type :


precision : digit number d –or- i : signed decimal
flags : o : unsigned octal
can be changed into: u : unsigned decimal
none : right justify
x : unsigned hexadecimal
- : left justify
-+ : for positive & negative value
f : floating point
e : floating point
(exponent)
c : single character
s : string
% : % character
p : pointer
Output Formatting
• For long data type, add l at the front of data type:
– long double  ( “ %lf “)
– unsigned long int  ( “ %lu “)
– long int  ( “ %ld “)
Input Formatting
• Space char, tab, linefeed, carriage-return, form-feed,
vertical-tab, and new-line entitle ”white-space
characters”

• Example :
– Using previous example, if a string “good morning
every one” entered then ss value will only contain
“good”
Input Formatting & Example

• To get string that ended with certain character for example


Enter, use scanf() with format: [^\n]

• Example:
char ss[40];
scanf(”%[^\n]”,ss);

• Using the previous example, if a string “good morning


every one” then ENTER, the ss variable will contain “good
morning every one”
INPUT & OUTPUT EXAMPLE
Output Example
• Example 1:
printf (“%6d”, 34); ….34
printf (”%-6d”, 34); 34….

• Example 2
printf (“%10s”, “BINUS”); …..BINUS
printf (“%-10s”, “BINUS”); BINUS…..
printf (“%8.2f”, 3.14159 ); ….3.14
printf (“%-8.3f”, 3.14159 ); 3.141…
Input Example

scanf()
function
can use
/* Program Calculating rectangle area v2*/ more than
#include <stdio.h> one
int main(){ argument
int width, height, area;
scanf(“%d %d”,&width, &height);
area = width * height;
return(0);
}
SUMMARY
Summary
• C language is a structural programming language
• C language consists of functions
• C program has one main function called main
• C language is case sensitive
• Every statement in C language should be ended
with a semi-colon (;)
• Syntax for Output:
printf, putchar, putch, puts
• Syntax for Input:
scanf, getchar, getch, getche, gets
References
• Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program.
PEAPH. New Jersey. ISBN:978-0-13-705966-9 Chapter 9
• Reading from and Writing to Standard I/O:
http://aelinik.free.fr/c/ch05.htm
• Intro to File Input/Output in C:
http://www.cs.bu.edu/teaching/c/file-io/intro/
Thank You

Das könnte Ihnen auch gefallen