0% found this document useful (0 votes)
8 views29 pages

S01. CFundamental

Uploaded by

Sơn Nguyễn
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)
8 views29 pages

S01. CFundamental

Uploaded by

Sơn Nguyễn
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
You are on page 1/ 29

C/C++ Programming Techniques

C Fundamental

Thanh-Hai Tran

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
Content

 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

 Directives: #include <stdio.h>


 The information in stdio.h will be included into the program
before it is complied
 C does not have built-in read and write commands
 Stdio.h contains information about C’s standard I/O library
 Directives begin with a character # to distinguish them
from other items in a C program.
 By default, directives are one line long; no semi colon or
other special marker at the end of the directive

2020 4
Functions

 Functions are procedure or sub-routines – the building


blocks from programs
 C is little more than a collection of functions
 Function is a series of statements that are grouped
together and given a name
 Functions fall into 2 categories:
 Provided by C implementation
 Written by programmer
 A C program may consist of many function, but only
main() function is mandatory.
 Main gets called automatically when the program is
executed

2020 5
Statements

 A statement is a command to be executed when the


program runs
 Two types of statements:
 Return statement
 Function call: asking a function to perform its assigned task
is known as calling the function
 Each statement ends with semicolon ;
 Directives are normally one line long , they don’t end with
a semicolon

2020 6
Comments

 Every program should contain identifying information:


 Program name, Data, Author
 Purpose of the program, So forth
 This information could be placed in comments
 The symbol /* */ marks the beginning and the end of a
comment

2020 7
Comment styles

2020 8
Variables and assignment

 Type: every variable must has a type


 Primitive types:
 char, int, short, long
 float, double
 Declaration: <type> <list of variable>;
 Example:
 int i; // int is integer type, i is a
variable
 float f; // float is floating point type, f
is a variable
 Declaration can be combined: int a, b, c, d;

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

 Signed and unsigned:


signed char (8 bits) –128 ~ +127
signed short (16 bits) –32768 ~ +32767
signed int (32 bits) –2147483648 ~ +2147483648
signed long (32 bits) –2147483648 ~ +2147483648
unsigned char (8 bits) 0 ~ +255
unsigned short (16 bits) 0 ~ +65535
unsigned int (32 bits) 0 ~ +4294967295
unsigned long (32 bits) 0 ~ +4294967295
 Notice:
 Signed by default
 int has size depending on computer configuration

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;

 A variable can be initialized by a value


 int count = 100;

 char key = 'K';

2020 13
Identifiers and keywords

 Identifier: name for variables, functions, macros, other


entities. It can contain letters, digits, underscores, but
must begin with a letter or a underscore.
 Keywords: C is case sensitive, keywords can not be used
as identifiers

2020 14
Type casting

 Cast operator: Converting one datatype into another is


known as type casting. You can convert the values from
one type to another explicitly using the cast operator
 Default type casting:
 float a = 30;
 int b = 'a';
 Explicit type casting:
 int a = (int)5.6; /* lấy phần nguyên */
 float f = (float)1/3;
 Impossible type casting
 char* s = 2.3; /* không dịch được */
 int x = "7"; /* dịch được nhưng sai */

2020 15
enum

 Enumeration is a user defined datatype in C language.


 It is used to assign names to the integral constants which
makes a program easy to read and maintain.
 The keyword “enum” is used to declare an enumeration.
 Declaration: enum <type name> { <values> };
 Example:
 Declaration:
enum DongVat { Meo, Cho, Ho, Bao };
enum Ngay { Thu2 = 2, Thu3, Thu4, Thu5, Thu6,
Thu7, CN = 1 };
 Usage:
enum DongVat dv = Meo;
dv = Bao;
2020 enum Ngay n = Thu5; 16
struct

 Structure is another user defined data type available in C


that allows to combine data items of different kinds
 Structures are used to represent a record. Suppose you
want to keep track of your books in a library. You might
want to track the following attributes about each book −
 Title
 Author struct <type name> {
 Subject
 Book ID member definition;
member definition;
...
member definition;
};
2020 17
Struct example
struct Student {
char name[20];
int yob;
int promotion;
};
struct Student sv = {"Le Duc Tho", 1984,
56};
sv.yob = 1985;
sv.promotion = 54;

2020 18
typedef

 Typedef is used to give a type a new name


 Syntax: typedef <original type> <new type>;
 Examples: Decration
 typedef double ChieuCao;
 typedef unsigned char byte;
 typedef enum DongVat DV;
 typedef struct { … } SinhVien;
 Usage
 ChieuCao d = 165.5;
 byte b = 30;
 DV dv = Cho;

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

 Input / Output operations must meet a number of


requirements:
 Allow to give out all the data types that the user requires, at
least the basic data types.
 Allow data to be outputted to various peripheral devices,
such as to a screen, to a printer or to a file for archiving.
 The data presented must be accurate, complete and clear.
 Formatted Input / output: stdio.h

2020 21
printf()

 Designed to display the contents of a string (format string)


with possibly inserted at specified points in the string
 The values displayed could be
 Constant,
 Variables
 Or more complicated expression
 There is no limit on the number of values can be printed
 The format of string: ordinary string or conversion
specifications (begin with the % character)
 C is not required to check the number of conversion
specifications, as well as if it is appropriate to the type of
item to be printed.

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

You might also like