Professional Documents
Culture Documents
Example program
In style of “Hello, world!” .c file structure
Syntax: comments, macros, basic declarations
The main() function and function structure
Expressions, order-of-operations
Basic console I/O (printf(), scanf(), etc.)
Example program:
hello-0.c
hello-1.c
hello-3.c
Writing the first program
Go back to the first set of slides Lec-00 to login to
Linux
Shown on screen is hello-world.c
Write the program using vi
Compile the program using gcc
Run the program
Raise both hands after you finish
First program hello world
#include <stdio.h>
/* header file about standard input/ output*/
/* The main() function */
int main ( void )/*entry point, return will be integer */
{
/*write message to console*/
/* put indentation of the lines to indicate the */
/* part of of main */
printf ( "hello, DKP students\n" );
return (0); / ∗ exit (0 => success) ∗ /
}
Hello world explanation
printf(): output text to console window (stdout)
and end the line (\n)
String literal: written surrounded by double quotes
return 0; exits the function, returning value 0 to
caller
Flow chart of program (1/2)
Flow chart must have ‘Begin’ and ‘End’
The above symbol is an oval
Connection between symbols is using
line/ arrow
The arrow indicates the flow off the
process
The ‘Begin’ chart can have one output
only (arrow leaving the oval)
The ‘End’ chart can have one input only
(arrow entering the oval)
Flow chart of program (2/2)
There is an output to the screen
Symbol of output is parallelogram (‘jajaran
genjang’)
Parallelogram can have one input and one output
only
Same symbol is used also for input
Input or output is indicated inside the parallelogram
Flowchart for ‘hello_world.c’
Write your first program
Duplicate the program hello_wold.c mentioned
before
Check the output, you can also change the output
anyway you want
After you finish and satisfied, raise both hands
Hello world again (variation)
Alternatively, store the string in a variable first:
int main ( void )/ ∗ entry point ∗ /
{
const char msg[] = "hello, DKP students" ;
/ ∗ write message to console ∗ /
printf (“%s\n”, msg);
return (0);
}
const keyword: qualifies variable as constant cannot be
modified during program execution
char: data type representing a single character; written in
quotes: ’a’, ’3’, ’n’
const char msg[]: a constant array of characters
String characters
Strings stored as character array
Null-terminated (last character in array is ’\0’ null)
Not written explicitly in string literals
Special characters specified using \ (escape
character):
\\ – backslash, \’ – apostrophe, \” – quotation
mark
\b, \t, \r, \n – backspace, tab, carriage
return, linefeed
\ooo, \xhh – octal and hexadecimal ASCII
character codes, e.g. \x41 – ’A’, \060 – ’0’
Input – output (console or terminal)
stdout, stdin: console output and input
streams
printf(string): print string to stdout
stdin
string = gets(string): read line from