Programming in C
Hoang Quang Huy-iBME Lab
hoangquanghuy@[Link]
[Link]
Content
1. Introduction to C 7. function
2. Input/Output 8. struct
3. Data types 9. file
4. Athrimetic Expressions 10. pointer
5. for/while 11. Memory management
6. arrays
Textbooks
1. Brian Kernighan and Dennis Ritchie - The C
Programming Language-Prentice Hall PTR
(1988)
2. Stephen G Kochan, Programming in C, 2015
IDE
+ Dev-C++
[Link]
+ Visual Studio Code
[Link]
+ Visual Studio
[Link]
…
Programming in Python
1. Python Programming Fundamentals - Kent D.
Lee, 2nd ed. 2014 - 978-1-4471-6642-9
2. The Python Workbook - Ben Stephenson, 2014 -
978-3-319-14240-1
3. A Beginners Guide to Python 3 Programming -
John Hunt, 1st ed. 2019 - 978-3-030-20290-3
4. Advanced Guide to Python 3 Programming -
John Hunt, 1st ed. 2019 - 978-3-030-25943-3
Programming in Java
1. C. Thomas Wu - A Comprehensive Introduction
to Object-Oriented Programming with Java
(2008, McGraw Hill)
2. Liang, Y. Daniel - Introduction to Java
programming _ brief version (2018, Pearson)
Lession 1: Introduction to C
7
Content
1. Introduction
2. Terminology
1. Key words
2. Variables
3. Basic data types
4. Constant-Variables
5. Type casting
6. Program structure
7. Command line interface (CLI)
8
1. Introduction
• High-level programming language
• Can be used at/with low-level (writing OS,
driver..)
• The most preferred language for embedded
systems
• The foundation for C++, C#, Java, Python…
• C will be used in many other
courses/applications
9
1. Standard C
• “K & R” C
• ANSI C (C89 – C90)
• C99: extended C89/C90, new data types ,
array..
• gcc by default : C89 + C99.
10
C Orientation
• Created in 1972 to write OSs
(Unix in particular)
– By Dennis Ritchie
– Bell Labs
• Evolved from B
• Can be portable to other hardware
(with careful design –
use Plauger’s The Standard C Library book)
• Built for performance and memory management
– operating systems, embedded systems, real-
time systems, communication systems
C Standardization
• 1989 ANSI and ISO -> Standard C
• 1999 C99
• 2011 C11
• Don’t get thrown when you lookup
information on websites and find conflicts
based upon standards
Later Languages
• 1979 C++ by Bjarn Stroustrup also at Bell
– Object orientation
• 1991 Java by Sun
– Partial compile to java bytecode: virtual machine
code
– Write once, run anywhere
– Memory manager – garbage collection
– Many JVMs written in C / C++
A First Program
makes input
and output available
#include <stdio.h>
to us
header
int main(void)
{
printf("This is my first C program.\n");
return(0);
}
open and close braces mark statements
the beginning and end
A First Program – What Does It Do?
printf("This is my first C program.\n");
return(0);
Prints the message
This is my first C program.
Ends the program Feeds a new line
\n the newline character
Java Reminder
Program C Java
#include<stdio.h> public class HelloWorld {
public static void main(String[]
int main(void) {
args) {
Hello
printf("Hello\n"); [Link]("Hello");
return 0; }
} }
C Program Phases
• Editor - code by programmer
• Compiling using gcc:
– Preprocess – expand the programmer’s code
– Compiler – create machine code for each file
– Linker – links with libraries and all compiled
objects to make executable
• Running the executable:
– Loader – puts the program in memory to run it
– CPU – runs the program instructions
Run First Program
Run First Program
21
22
2. Terminology
1. Key words
2. Identifiers
3. Basic data types
4. Variables/Constant
5. Type casting
6. Program structure
7. Command line interface
23
2.1 Key words
• Key words are reserved words (special words)
▪ word has a specific meaning in C
▪ include, int, main, void, return
▪ Other key words
▪ if, switch, for, while, do, break, continue, ...
• Predefined functions : stored in library (.h)
▪ stdio.h: printf, scanf, gets, …
▪ conio.h: getch
▪ math.h: sqrt, pow, exp, ....
▪ string.h: strlen, strcpy, strcmp, ....
• #include ” …” or #include <…>
24
2.2 Identifiers
One feature present in all computer languages is the
identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address.
25
2.2 Identifiers
• Used to name constants, variables, structs…
• Rules
– Starts with an alphabet char or underscore symbol (_)
– Consists of letters, digits and underscore.
– Can not be a C reserved word
– Case sensitive
• Ex : valid identifier: C_, _myPtr, Go4it, testCase, first_name
invalid id Reason
won@last uses @
3_Times Starts with digits
char reserved word
First variable has space
f(x) uses ( )
26
27
2.3 Basic data types
No Types Sizeof() Data Example
whole number
1 char 1 65, ’A’
letter
2 int 4 whole number 16, 020, 0x10
3 float 4 real number 0.00125f, 2.25e-3
4 double 8 real number Default in Dev-C++
sizeof(): is a function to show the memory size (byte) allocated for variables
(datta types)
- Modifiers can apply to specific data types
No Modifiers Types Data Range
1 short int whole number Compiler
whole positive
2 unsigned char/int number char: 0 ➔ 28-1 = 255
4 long int/double real number Compiler
5 long long int/double real number Compiler 28
29
2.4 Constant-Variables
• Constant has a fixed value
• Variables : for storing computers computations and
results
• Constant and variables :
– assigned symbolic names
– must be declared before they are used
– if not , errors when compiling
• Constant declaration
▪ const type name = value;
▪ #define name replacement_text;
• Declaration for variables
▪ type variable_name = value;
▪ type variable_name;
30
2.4 Constant-Variables
• Example
const int m_to_mm = 1000;
#define PI 3.14
float R = 5;
float area;
int a;
31
2.6 Program structures
33
The Greeting Program
The Greeting Program
Examples of Block Comments
36
Examples of Line Comments
37
Exercises [1,2]
Exercise 1-1. Run the ``hello, world'' program on your system.
Experiment with leaving out parts of the program, to see what
error messages you get.
Exercise 1-2. Experiment to find out what happens when prints's
argument string contains\c, where c is some character not listed
above
38
3. What output would you expect from the following program?
#include <stdio.h>
int main (void)
{
printf ("Testing...");
printf ("....1");
printf ("...2");
printf ("..3");
printf ("\n");
return 0;
}
4. Write a program that subtracts the value 15 from 87 and
displays the result, together with an appropriate message, at
the terminal.
39
5. Identify the syntactic errors in the following program.
Then type in and run the corrected program to ensure you
have correctly identified all the mistakes.
#include <stdio.h>
int main (Void)
(
INT sum;
/* COMPUTE RESULT
sum = 25 + 37 - 19
/* DISPLAY RESULTS //
printf ("The answer is %i\n" sum);
return 0;
}
40
6. What output might you expect from the following program?
#include <stdio.h>
int main (void)
{
int answer, result;
answer = 100;
result = answer - 10;
printf ("The result is %i\n", result + 5);
return 0;
}
41
42
Template a.c
43