0% found this document useful (0 votes)
11 views32 pages

(ICCS121) L08 - C Programming

The document is a lecture on C programming, covering fundamental concepts such as data types, control structures, input/output operations, and file handling. It also discusses preprocessor directives, arrays, structs, and defining new data types. The course emphasizes ethical use of knowledge in cybersecurity and includes a take-home exercise for practice.

Uploaded by

talktome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views32 pages

(ICCS121) L08 - C Programming

The document is a lecture on C programming, covering fundamental concepts such as data types, control structures, input/output operations, and file handling. It also discusses preprocessor directives, arrays, structs, and defining new data types. The course emphasizes ethical use of knowledge in cybersecurity and includes a take-home exercise for practice.

Uploaded by

talktome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ICCS121

SY S . S KIL LS & L OW-LV . P ROGRA M MIN G

LE CT URE 0 8:
C P R O G RA M M IN G

S. TEERAKANOK,
Mahidol University
International College (MUIC)
CompTIA CSIE | CEH | CC | BTL1
CISA | SAL1 | CBROPS | CTT+ | PROJECT+
WHOAMI?
Dr. Songpon
TEERAKANOK (Oan)
Sec+ | Pentest+ | CySA+ | SecurityX | CISA
Proj+ | CTT+ | CC | BTL1 | SAL1 | CBROPS | CEH

Area of Expertise:

• Cybersecurity
• Cryptography and Network Security
• Digital Forensics

Contact: [email protected]
Website: songpon.teerakanok.me
FB Page: CyberSkills – by Aj. Songpon
Medium: https://medium.com/@teerakanok

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 2


DISCLAIMER ! IP & Ethical Conduct

Intellectual Property

• All course materials are protected under the Intellectual


Property Act. Unauthorized copying, modification, or
distribution is prohibited without explicit written permission.

Ethical Use of Knowledge

• This course includes sensitive cybersecurity and privacy


content. Participants must commit to using acquired skills
ethically, avoiding harm, offense, or illegal personal gain.

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 3


Credits

• Slide Courtesy of Aj. Sunsern Cheamanunkul


ICCS121, Mahidol University International College

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved ***


>> RECAP

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 5


Data Types

• Like other languages, C works with multiple data types


o int
o char
o float

• Note 1: These data types represent values


o Int value of 65 is a letter ‘A’ in char

• Note 2: C only support basic data types


o You can define your own complex data types
o No boolean → 0 means false, non-zero means true

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 6


Control
Structure – if

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 7


Control Structure – switch-case

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 8


Loop

• while(statement)

• do-while(statement)

• You can use “break;” to break out of the loop

• You can use “continue;” to skip the current


iteration of the loop

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 9


>> I/O & Files

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 10


Basic In/Output – Printf & Scanf

• To print something to the standard output, use printf

printf(“Hello World %d”,10); // print Hello World 10

• To read from the standard input, use scanf

scanf(“%d”, &a); // read the number from the input,


// store it in the variable a

• You will want to practice with format string in our in-class

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 11


File Pointers

• Accessing the file is done to a file pointer

FILE *file_pointer_name;
fp = fopen(“path_to_file”, mode)
fclose(fp);

• See how to use it here:


https://www.gnu.org/software/libc/manual/html_node/Opening-Streams.html

• If there is an error → fopen returns NULL

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 12


Reading from a File

• Instead of scanf(“%d”,&A) you use

fscanf(file_pointer,”%d”,&A) → Read one integer to A

• The same concept also applies for


o getc
o gets
o See https://en.wikibooks.org/wiki/C_Programming/Stream_IO

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 13


Writing to a File

• Instead of printf(“%d”,A) you use

fprintf(file_pointer,”%d”,A) → Write one integer to A

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 14


End of File (EOF)

• An end of a file is denoted as end-of-file (EOF)

• You can use the function feof(file_pointer) to check if you reach the end of file

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 15


>> Preprocessor Directives

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 16


Preprocessor Directives

• Special commands in C, C++, and similar languages that tell the preprocessor to
modify your source code before it's passed to the compiler.

• This essentially process information on your c code

• The header files (all the .h files)

• The define (#define)

• Etc. (things listed with the # symbol such as #ifndef

• Then, it tokenize your file


Basically process your program text and prepare these tokens for the compiler

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 17


Header Files

• Include function definitions for your program

• Include some codes that you want to leave out of the main .c file

• Include function definitions of pre-build functions


stdio.h, stdlib.h

• You can also use this for installed libraries


o Example: boost library for C++

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 18


#define = ???

• It defines a macro in your code

• Think of it as whatever you put there replace the matching text in your code
i.e., search and replace

• Example
#define DEBUG 1
o This will replace any occurrence of DEBUG to 1

• More explanation here: https://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 19


Fancy #define

• Because it is a macro, it can take arguments ☺

• Example
#define min(A,B) ((A) < (B)? (A): (B))
Then you can just do min(3, 4) or min(input1, input2)

• Function-like macros:
https://gcc.gnu.org/onlinedocs/cpp/Function-like-Macros.html#Function-like-Macros

• Object-like macros:
https://gcc.gnu.org/onlinedocs/cpp/Object-like-Macros.html#Object-like-Macros

• Doing this can also be dangerous


See Macros pitfalls: https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html#Macro-Pitfalls

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 20


>> Array

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 21


Array ?

• Collection of data that has the same type

• Contiguous in the memory

src: https://www.geeksforgeeks.org/dsa/what-is-array/

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 22


Array (2)

• You can use [] to specify an array of certain data types

• Example:
int i[10];
char a[20];

o Question: How can I tell that it is the end of string?

• What else?

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 23


Array (3) – Accessing Elements

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 24


Array (4) – Strings

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 25


>> Structs & Creating New Data Type

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 26


Struct ??

• You can also combine multiple elements into one single unit

• This is called a struct

Struct some_name{
int a;
float b;
};

• This create a structure called some_name, with an int and a float


o some_name.a → The int element
o some_name.b → The float element

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 27


Struct (2) – Example

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 28


Struct (3) – More about Struct

• It’s common practice to define structs in header files (.h).


o If they need to be shared across multiple source files (.c) !

• Creating a dedicated .h file for structs that will be used across the
entire project is a good practice in C programming.
o Especially for larger projects !!!

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 29


Array + Struct ??

• You can also create an array of struct

• Used similar to a typical array

//declare an array of user


struct user user_list[1000];

//accessing age info of the target user at index 0


user_list[0].age

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 30


Defining a New Data Type

• You can define a new name for data type, including struct using typedef <type> <name>

//declare (renaming)
typedef struct oldname newname;

//then use
newname arr_name[1000];

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 31


Take-home Exercise

• NO SUMISSION REQUIRED

• https://www.w3schools.com/c/index.php
Skip the following sections:
• Memory Addresses
• Pointers
• Memory Management

• https://www.hackerrank.com/domains/c?filters%5Bskills%5D%5B%5D=C%20%2
8Basic%29&filters%5Bdifficulty%5D%5B%5D=easy

*** ICCS121 – COPYRIGHT © S. TEERAKANOK All Rights Reserved *** 32

You might also like