W2-3 Basics C Component

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ALFATIHAH

OR

Any recitation based on ones believes & faith

INTRODUCTION
C structured language :
Sequential statement
Selection statement (if..else)
Repetition statement (do..while, for.., while)

Suitable for top-down solution algorithm


C is also a procedural language programs can break into

multiple modules called functions

Standard functions provided by run-time library


stdio.h, utilib.h, math.h
printf(), getchar(), sqrt()
input/output, mathematical, string, dynamic storage, etc

A COMPLETE C PROGRAM
Comments will not
be executed by
compiler
/*.*/ OR //.
Pre-processor statement /
library
main() function
entry point for all
programs / start here
all C statements
MUST end with ;

All functions / modules / blocks


must start with { and end with }

printf(), scanf(), getchar()


functions imported from
library stdio.h

1. COMMENTS
Format :
/* .. */

usually use for multiple line comments

// .

only for single line comments

No need to end with ;


Do not cause the computer to perform any action when the program is run.
Use for document programs and improve program readability.
Ignored by the C compiler and do not cause any machine-language object

code to be generated.
Help other people read and understand your program

2. PREPROCESSOR DIRECTIVES
Format :

#include <name of library>

to import standard C library

#define, #ifndefine, #endif

to create new library

#include name of custom-made library to import custom-made library


# are processed by the preprocessor before compilation.
Line 7 tells the preprocessor to include the contents of the standard input/output header

(<stdio.h>) in the program.


This header contains information used by the compiler when compiling calls to standard

input/output library functions such as printf() & scanf()


# can also be used to create new library, import custom-made library or any pre-processing

tasks before compilation

3. main() Function
Format :

<return_data_type> <function_name> (<list_arguments>)


void main( )
main() function is the entry point in C program.
If the C program consists of >1 files, there will be ONLY 1 main() function
Return data type what kind of data will be returned by the function once its

execution in completed

void no data returned


List of arguments list of data to be received by the function, send by the

caller

Function name to be given by programmer

3. main() Function

A left brace, {, begins the body of every function (line 11).


A corresponding right brace, }, ends each function (line 21).
This pair of braces and the portion of the program between

the braces is called a block

4. Output statement printf()


Format :
printf(<statement>);
printf(conversion specifier,<address operator+variable name>);
Namely to print on the screen the string of characters marked by the

quotation marks
Like any other statement, printf() must end with ;
Conversion specifier types of data that need to be displayed
Address operator = & = the address of the variable in memory

5. Escape Sequence
Format : \<special character>
\ is called escape character. Combining \ with a set of

special character will form escape sequence


Escape sequence to instruct printf to do special task

6. Input statement scanf()


Format :
scanf(<conversion specifier>, <address operator+variablename>);
To get standard input from user keyboard
Must end with ;

Conversion specifier types of data that need to be displayed


Address operator = & = the address of the variable in

memory

You might also like