Beruflich Dokumente
Kultur Dokumente
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:
#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:
• 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
• Syntax:
printf(const char *format[,argument, …]);
• 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.
• 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);
• 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
• Example :
– Using previous example, if a string “good morning
every one” entered then ss value will only contain
“good”
Input Formatting & Example
• Example:
char ss[40];
scanf(”%[^\n]”,ss);
• 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