S01. CFundamental
S01. CFundamental
C Fundamental
Thanh-Hai Tran
Simple program
Directives
Comments
Main function
Data types
Variables / Constants
Assignment
Type Cast
enum
struct
Formatted Input / Output
2020 2
Writing a simple program
/* hello.c */
#include <stdio.h>
int main() {
printf(“Hello World!\n");
return 0;
}
2020 3
Directives
2020 4
Functions
2020 5
Statements
2020 6
Comments
2020 7
Comment styles
2020 8
Variables and assignment
2020 9
Constants
The value of constant can not be changed during
running
Declaration: const <type> <name> = <value>;
In C, constant allocates memory as variable
Example:
const double PI = 3.14159;
const char* name = "Nguyen Viet Tung";
PI = 3.14; /* sẽ báo lỗi */
Macro definition: #define <name> <value>
Example: #define PI 3.14159
#define is a pre-processing directive, no semicolon at the
end of the line
When a program is compiled, it will replace each marco by
2020 10
Primitive types
Type Size Meaning
char 1 Integer, char
int (tuỳ thuộc: 2, 4, 8) Integer
short 2 Integer
long 4 Integer
long long 8 Integer
float 4 Float (dấu chấm động)
double 8 Float (dấu chấm động)
void 0 Undetermined type
In C, char is a integer of 8 bits
Operator: sizeof() computes the size of a type in bytes:
sizeof(x)
sizeof(int)
2020 11
Primitive types
2020 12
Assignment
A variable can be given a value by means of assignment
Syntax:
<variable> = <constant, variable> or <expression>;
Before a variable can be assigned a value, it must be first
declared.
Example:
count = 100;
value = cos(x);
i = i + 2;
2020 13
Identifiers and keywords
2020 14
Type casting
2020 15
enum
2020 18
typedef
2020 19
struct
typedef struct{
char name[20];
int yob;
int promotion;
} student;
student sv = {"Le Duc Tho", 1984, 56};
sv.yob = 1985;
sv.promotion = 54;
2020 20
Formatted Input / output
2020 21
printf()
2020 22
printf()
Syntax:
printf(“formatted string", <values>);
Conversion specification:
Format:
%[flags] [width] [.precision]type
Example: %+15.5f
2020 23
Example
main(){ Output:
char ch='A';
int i=1234,j=-1234; A^^^^,^^^^A
float x=11.23456789; 1234,1234^^^^^^,^^^^^+123
printf("%- 4, ^^^^001234,-
5c,%5c\n",ch,ch); 0000001234
printf("%d,%-10d,%+10d, 11.234568,
%10.6d, ^^^11.234568,11.2346^^^
%010d\n",i,i,i,i,j); ^^,+000011.2346
printf("%f,%12f,%- ^^^11.234568
12.4f,%+012.4f\n",x,x,x ^^^^^^11.235
,x);
-------------------------
printf("%*f\n",12,x); ----
printf("%*.*f",12,3,x); Quy ước các dấu ‘^’ biểu
getch(); diễn các kí tự trắng để
return; dễ theo dõi.
}
2020
printf()
2020 25
scanf()
Syntax:
scanf(“formatted string", <addresses of
variables>);
Examples:
int age;
scanf("%d", &age);
float weight;
scanf("%f", &weight);
char name[20];
scanf("%s", name);
2020 27
Quick test
2020 28
Exercise
Install VSCode
Write a program that enter two fractions, add these
fractions and print out the result
2020 29
To do
Class representative:
Student grouping (3 students/group)
Google form
Topics
All:
Install C/C++ (Dev C hoặc VS Code)
Code in C
Study the lecture (Slide S02)
PC: Ready for coding
2020 30