C Programming Language Fundamentals
C Programming Language Fundamentals
C Programming Language
Fundamentals
By: Võ Văn Hải
Email: vovanhaiqn@gmail.com
Session Objectives
▸The C Programming Language introduction
▸C Fundamental
▸C Input/Output
▸C Programming Operators
1
17/10/2023
3. Coding
5. Install/Deploy
6. Maintenance
Programming Languages
Introduction
▸A programming language is a set of instructions and syntax used to
create software programs. Some of the key features of programming
languages include:
- Syntax: The specific rules and structure used to write code in a programming
language.
- Data Types: The type of values that can be stored in a program, such as numbers,
strings, and booleans.
- Variables: Named memory locations that can store values.
- Operators: Symbols used to perform operations on values, such as addition,
subtraction, and comparison.
- Control Structures: Statements used to control the flow of a program, such as if-else
statements, loops, and function calls.
- Libraries and Frameworks: Collections of pre-written code that can be used to
perform common tasks and speed up development.
- Paradigms: The programming style or philosophy used in the language, such as
procedural, object-oriented, or functional.
2
17/10/2023
Programming Languages
Basic Terminologies
▸ Algorithm: A step-by-step procedure for solving a problem or performing a task.
▸ Variable: A named storage location in memory that holds a value or data.
▸ Data Type: A classification that specifies what type of data a variable can hold, such as integer,
string, or boolean.
▸ Function: A self-contained block of code that performs a specific task and can be called from
other parts of the program.
▸ Control Flow: The order in which statements are executed in a program, including loops and
conditional statements.
▸ Syntax: The set of rules that govern the structure and format of a programming language.
▸ Comment: A piece of text in a program that is ignored by the compiler or interpreter, used to
add notes or explanations to the code.
▸ Debugging: The process of finding and fixing errors or bugs in a program.
▸ IDE: Integrated Development Environment, a software application that provides a
comprehensive development environment for coding, debugging, and testing.
▸ Operator: A symbol or keyword that represents an action or operation to be performed on one
or more values or variables, such as + (addition), – (subtraction), * (multiplication), and /
(division).
▸ Statement: A single line or instruction in a program that performs a specific action or
operation.
3
17/10/2023
Result:
4
17/10/2023
Compiler working
C Fundamentals
10
10
5
17/10/2023
Fundamentals
C Keywords and Identifiers
▸Character set
A character set is a set of alphabets, letters and some special characters that are valid
in C language.
- Alphabets
• Uppercase: A B C ................................... X Y Z
• Lowercase: a b c ...................................... x y z
**C accepts both lowercase and uppercase alphabets as variables and functions.
- Digits
• 0123456789
- Special Characters
, < > . _
( ) ; $ :
% [ ] # ?
' & { } "
^ ! * / |
- \ ~ +
- White space Characters
• Blank space, newline, horizontal tab, carriage return and form feed.
11
11
Fundamentals
C Keywords
▸Keywords are predefined, reserved words used in programming that
have special meanings to the compiler. Keywords are part of the syntax,
and they cannot be used as an identifier.
▸As C is a case sensitive language, all keywords must be written in
lowercase. Here is a list of all keywords allowed in ANSI C.
C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
12
12
6
17/10/2023
Fundamentals
C Identifiers
▸Identifier refers to name given to entities such as variables, functions,
structures etc.
▸Identifiers must be unique. They are created to give a unique name to an
entity to identify it during the execution of the program.
- For example:
int money;
double accountBalance;
13
13
Fundamentals
Rules for naming identifiers
▸A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.
▸The first letter of an identifier should be either a letter or an underscore.
▸You cannot use keywords like int, while etc. as identifiers.
▸There is no rule on how long an identifier can be. However, you may run
into problems in some compilers if the identifier is longer than 31
characters.
▸You can choose any name as an identifier if you follow the above rule,
however, give meaningful names to identifiers that make sense.
▸Should read:
Naming convention
14
14
7
17/10/2023
15
15
16
16
8
17/10/2023
17
17
18
18
9
17/10/2023
▸You can also define a constant using the #define preprocessor directive.
#define X 5
printf("%d",X);
19
19
C Data Types
Basic Types
▸In C programming, data types are declarations for variables. This
determines the type and size of data associated with variables.
Type Size (bytes) Format Specifier
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int ko co so sau phay at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
20
20
10
17/10/2023
C Data Types
Basic Types
▸In C programming, data types are declarations for variables. This determines the
type and size of data associated with variables. Depend on compiler, the
datatype size may be differed.
32-bit 1 2 4 4 8 4 8 8 4 4 4
Microsoft
32-bit 1 2 4 4 8 4 8 12 4 4 4
GNU gcc
32-bit 1 2 4 4 8 4 8 12 4 4 4
Clang
64-bit 1 2 4 4 8 4 8 8 8 8 8
Microsoft
64-bit 1 2 4 8 8 4 8 16 8 8 8
GNU gcc
64-bit 1 2 4 8 8 4 8 16 8 8 8
Clang
64-bit 1 2 4 4 8 4 8 16 8 8 8
MinGW
21
21
- Base 16(Hexadecimal) — Represent any number using 10 digits and 6 characters [0–
9, A, B, C, D, E, F]
22
22
11
17/10/2023
23
C Data Types
Derived Data Types
▸Data types that are derived from fundamental data types are derived
types.
- For example: arrays, pointers, function types, structures, etc.
24
24
12
17/10/2023
C Input / Output
25
25
return 0;
}
26
26
13
17/10/2023
27
28
14
17/10/2023
C Comments
▸There are two ways to add comments in C: /**
* This method takes two parameters and
- // Single Line Comment * then finds the maximum between them
- /∗ ⋯ ∗/ Multi-line Comment * @param x parameter x
▸Use of Comments in C * @param y parameter y
* @return maximum value
- Make Code Easier to Understand
*/
- Using Comments for debugging int findMax(int x, int y){
if(x>y)
return x;
//sample inline comment
return y;
}
▸Note:
- Comments are not and should not be used as a substitute to explain poorly written
code.
- Always try to write clean, understandable code, and then use comments as an
addition.
- In most cases, always use comments to explain 'why' rather than 'how' and you are
good to go.
29
29
C Programming Operators
30
30
15
17/10/2023
++a++
31
31
C Programming Operators
C Arithmetic Operators
▸An operator is a symbol that operates on a value or a variable. For
example: + is an operator to perform addition.
▸C has a wide range of operators to perform various operations.
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)
32
32
16
17/10/2023
C Programming Operators
C Increment and Decrement Operators
▸C programming has two operators increment ++ and decrement -- to
change the value of an operand (constant or variable) by 1.
▸Increment ++ increases the value by 1 whereas decrement -- decreases
the value by 1. These two operators are unary operators, meaning they
only operate on a single operand.
int main()
{ Results
int a = 10, b = 100;
float c = 10.5, d = 100.5; ++a = 11
--b = 99
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b); ++c = 11.500000
printf("++c = %f \n", ++c);
--d = 99.500000
printf("--d = %f \n", --d);
return 0;
}
33
33
C Programming Operators
C Assignment Operators
▸An assignment operator is used for assigning a value to a variable. The
most common assignment operator is = int main()
{
Operator Example Same as int a = 5, c;
= a=b a=b
c = a; // c is 5
+= a += b a = a+b printf("c = %d\n", c);
-= a -= b a = a-b c += a; // c is 10
printf("c = %d\n", c);
*= a *= b a = a*b c -= a; // c is 5
/= a /= b a = a/b printf("c = %d\n", c);
c *= a; // c is 25
%= a %= b a = a%b printf("c = %d\n", c);
c /= a; // c is 5
c=5 printf("c = %d\n", c);
c = 10 c %= a; // c = 0
c=5 printf("c = %d\n", c);
Results
c = 25
c=5 return 0;
c=0 }
34
34
17
17/10/2023
C Programming Operators
C Relational Operators
▸A relational operator checks the relationship between two operands. If
the relation is true, it returns 1; if the relation is false, it returns value 0.
▸Relational operators are used in decision making and loops.
Operator Meaning of Operator Example int a = 5, b = 5, c = 10;
35
35
C Programming Operators
C Logical Operators
▸An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used in
decision making in C programming.
int a = 5, b = 5, c = 10, result;
36
36
18
17/10/2023
C Programming Operators
C Bitwise Operators
▸During computation, mathematical operations like addition, subtraction,
multiplication, division, etc. are converted to bit-level which makes processing
faster and saves power.
Operators Meaning of operators X Y X&Y X|Y X^Y
& Bitwise AND 0 0 0
0 0
| Bitwise OR
0 1 1
^ Bitwise exclusive OR 0 1
~ Bitwise complement 1 0 0 1 1
<< Shift left
1 1 1 1 0
>> Shift right
37
C Programming Operators
C Bitwise Operators - example
//C Program to demonstrate use of bitwise operators
int main() {
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
return 0;
}
38
38
19
17/10/2023
C Programming Operators
Other Operators
▸Comma Operator
- Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
39
39
Operators Precedence in C
▸The concept of operator precedence and associativity in C helps in
determining which operators will be given priority when there are
multiple operators in the expression.
▸It is very common to have multiple operators in C language and the
compiler first evaluates the operator with higher precedence.
▸It helps to maintain the ambiguity of the expression and helps us in
avoiding unnecessary use of parenthesis.
Example
10 + 20 * 30
40
40
20
17/10/2023
* Dereference Operator
& Addressof Operator
sizeof Determine size in bytes
41
41
42
42
21
17/10/2023
12 || Logical OR Left-to-Right
= Assignment
43
43
Reviews
▸We have learned about the following topics so far:
1. Variables and Constants
2. Data Types
4. Operators
44
44
22
17/10/2023
Exercises
Write programs that:
1. to Add / Sum Two Numbers.
7. to Print ASCII Value (tip: read character, print number of this char)
45
45
46
46
23