0% found this document useful (0 votes)
0 views

03 Variable Type Expression GUI

The document outlines a course titled 'Programming for Engineers' focusing on the C programming language, covering topics such as basic data types, operators, functions, arrays, and memory management. It includes detailed explanations of tokens, variables, constants, and the distinctions between global and local variables. The course is taught by Dr.-Ing. Nguyen Van Binh and is scheduled for February 2025.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

03 Variable Type Expression GUI

The document outlines a course titled 'Programming for Engineers' focusing on the C programming language, covering topics such as basic data types, operators, functions, arrays, and memory management. It includes detailed explanations of tokens, variables, constants, and the distinctions between global and local variables. The course is taught by Dr.-Ing. Nguyen Van Binh and is scheduled for February 2025.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Course: Programming for Engineers (C language)

Dr.-Ing. Nguyen Van Binh


Email: [email protected]
HCMC, Feb. 2025
Content

1. Introduction to Computer and C language


2. Making Decisions
3. Looping
4. Functions
5. Arrays
6. Pointers
7. Structures
8. Characters and Strings
9. File Processing
10.Dynamic Memory Allocation
5 September 2022 Programming for Engineers (C language) NV Binh 2
1 Introduction to Computer and C language
Lecture Content
 Tokens in C
 Basic Data Types
 Constants/Variables
 Global and Local Variables
 Operators
 Type Conversions
 Precedence Order
 Floating Point Arithmetic
 Exercises
5 September 2022 Programming for Engineers (C language) NV Binh 3
1 Introduction to Computer and C language
Tokens in C

5 September 2022 Programming for Engineers (C language) NV Binh 4


1 Introduction to Computer and C language
Tokens in C • Keywords
– These are reserved words of the C language.
– For example int, float, if, else, for, while, etc.
Keywords in C Programming

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while


5 September 2022 Programming for Engineers (C language) NV Binh 5
1 Introduction to Computer and C language
Tokens in C
• Identifiers
– An identifier is a sequence of letters and digits, but must start
with a letter. Underscore (_) is treated as a letter. Identifiers
are case sensitive. Identifiers are used to name variables,
functions etc.
– Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If
– Invalid: 224, short, price$, My Name

• Constants
– Constants like 14, ‘a’, 1.4e-5, etc.

5 September 2022 Programming for Engineers (C language) NV Binh 6


1 Introduction to Computer and C language
Tokens in C
• String Literals
– A sequence of characters enclosed in double quotes as “…”. For
example, “14” is a string literal and not number 14. ‘a’ and “a”
are different.

• Operators
– Arithmetic operators like +, -, *, / ,%, etc.
– Logical operators like ||, &&, !, etc. and so on.

• White Spaces
– Spaces, new lines, horizontal/vertical tabs, comments (a
sequence of characters enclosed in /* and */ or after //) etc.
These are used to separate the adjacent identifiers, keywords and
constants.
5 September 2022 Programming for Engineers (C language) NV Binh 7
1 Introduction to Computer and C language
Basic Data Types
• Integral Types: Integers are stored in various sizes. They can be
signed or unsigned.
Suppose an integer is represented by a byte (8 bits).
Leftmost bit is the sign bit. If the sign bit is 0, the number is treated
as positive. Otherwise (the sign bit is 1), the number is negative.
Example:
01001011 = 75 (decimal).
The largest positive number is 01111111 = 27 – 1 = 127.
Negative numbers are stored in two ways as two’s complement or
as one’s complement.
-75 = 10110100 (one’s complement)
-75 = 10110101 (two’s complement)
5 September 2022 Programming for Engineers (C language) NV Binh 8
1 Introduction to Computer and C language
Basic Data Types
Example: 2’s complement
• To add 6+3 using 5 bit 2’s complement
representation,
00110
+
00011
——–
01001
• To subtract 6-3, rewrite as 6+ (-3):
00110
+
11101 (2s-complement of 3)
———
5 September 2022 00011 Programming for Engineers (C language) NV Binh 9
1 Introduction to Computer and C language
• Data Types
Basic Integral Types
Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255


unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767 or -2,147,483,648 to
int 2 or 4 bytes
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
-9223372036854775808 to
long 8 bytes
9223372036854775807
unsigned
5 September 2022 long 8 bytes 0 to 18446744073709551615
Programming for Engineers (C language) NV Binh 10
1 Introduction to Computer and C language
Basic Data Types
• Floating Point Numbers
– Floating point numbers are rational numbers and always signed numbers.

– Float:approximate precision of 6 decimal digits.


• Typically stored in 4 bytes (32 bits) with 24 bits of signed mantissa and 8 bits of
signed exponent.
– Double:approximate precision of 14 decimal digits.
• Typically stored in 8 bytes (64 bits) with 56 bits of signed mantissa and 8 bits of
signed exponent.

5 September 2022 Programming for Engineers (C language) NV Binh 11


1 Introduction to Computer and C language
Variable
• Variables are used to store data, they are named so because their contents can
change.
• Each variable used must be declared.
Example: int i;
i=3;
char x,y,z
• Naming a variable double d1, d2, d3 = 1.2;

– Must not be a keyword.

– Must not begin a number.

– Names are case sensitive.

– Variables are identified by only first 32 characters.

– Naming Styles: Uppercase style and Underscore style

lowerLimit lower_limit incomeTax income_tax


5 September 2022 Programming for Engineers (C language) NV Binh 12
1 Introduction to Computer and C language
Constants
 C Constants is the most fundamental and essential part of the C programming
language.
 Constants in C are the fixed values that are used in a program, and its value
remains the same during the entire execution of the program.
Syntax: const type constant_name;
Example: #include<stdio.h>
main() {
const int SIDE = 10; // int const SIDE = 10;
int area;
area = SIDE*SIDE;
printf("The area of the square with side: %d is: %d sq. units" , SIDE,
area); }

5 September 2022 Programming for Engineers (C language) NV Binh 13


1 Introduction to Computer and C language
Constants

5 September 2022 Programming for Engineers (C language) NV Binh 14


1 Introduction to Computer and C language
Constants
• Numerical Constants
– Constants like 12 or 253 are stored as int type. No decimal point.
– 12L or 12l are stored as long int.
– 12U or 12u are stored as unsigned int.
– 12UL or 12ul are stored as unsigned long int.
– Numbers with a decimal point (12.34) are stored as double.
– Numbers with exponent (12e-3 = 12x10-3) are stored as double.
– 12.34f or 1.234e1f are stored as float.
– These are not valid constants:
25,000 7.1e 4 $200 2.3e-3.4 etc.
5 September 2022 Programming for Engineers (C language) NV Binh 15
1 Introduction to Computer and C language
Constants

• Character and string constants


– ‘c’, a single character in single quotes are stored as char.
Some special characters are represented as two characters in single quotes.
– ‘\n’ = newline, ‘\t’= tab, ‘\\’ = backlash, ‘\”’ = double quotes.
– Char constants also can be written in terms of their ASCII code.
– A sequence of characters enclosed in double quotes is called a string
constant or string literal.
– For example:
“Charu” “A” “3/9” “x = 5”

5 September 2022 Programming for Engineers (C language) NV Binh 16


1 Introduction to Computer and C language
Global and Local Variables

• Global Variables
/* Compute area and perimeter of a circle */
#include <stdio.h>
float pi = 3.14159; /* Global */

int main() {
– These variables are declared float rad;
outside all functions.
printf(“Enter the radius: ”);
scanf(“%f”, &rad);
– Lifetime of a global variable is the
if (rad > 0.0) {
entire execution period of the float area = pi * rad * rad;
program. float peri = 2 * pi * rad;

printf(“Area = %f\n”, area);


printf(“Peri = %f\n”, peri);
– Can be accessed by any function }
defined below the declaration, in a else
printf(“Negative radius\n”);
file.
return 0;
}

5 September 2022 Programming for Engineers (C language) NV Binh 18


1 Introduction to Computer and C language
Global and Local Variables

• Local Variables /* Compute area and perimeter of a circle */


#include <stdio.h>
float pi = 3.14159;

– These variables are declared int main() {


inside some functions. float rad; /* Local */

printf(“Enter the radius: ”);


– Lifetime of a local variable is scanf(“%f”, &rad);
the entire execution period of
if (rad > 0.0) {
the function in which it is float area = pi * rad * rad; /* Local */
defined. float peri = 2 * pi * rad; /* Local */

printf(“Area = %f\n”, area);


– Cannot be accessed by any other printf(“Peri = %f\n”, peri);
function. }
else
printf(“Negative radius\n”);
– In general, variables declared
inside a block are accessible return 0;
}
only in that block.
5 September 2022 Programming for Engineers (C language) NV Binh 19
1 Introduction to Computer and C language
Operators
• Arithmetic Operators
– +, – , *, / and the modulus operator %
– + and – have the same precedence and associate left to right
3–5+7=(3–5)+73–(5+7)
3+7–5+2=((3+7)–5)+2
– *, /, % have the same precedence and associate left to right
– The +, – group has lower precedence than the *, /, % group
3–5*7/8+6/2
3 – 35 / 8 + 6 / 2
3 – 4.375 + 6 / 2
3 – 4.375 + 3
-1.375 + 3
1.625

5 September 2022 Programming for Engineers (C language) NV Binh 20


1 Introduction to Computer and C language
Operators
• Arithmetic Operators
– % is a modulus operator. x%y results in the remainder when x is
divided by y and is zero when x is divisible by y.
– Cannot be applied to float or double variables.

Example
if (num % 2 == 0)
printf(“%d is an even number
\n”, num);

else
printf(“%d is an odd number
\n”, num);

5 September 2022 Programming for Engineers (C language) NV Binh 21


1 Introduction to Computer and C language
Operators
• Relational Operators
– <, <=, >, >=, ==, != are the relational operators. The expression
operand1 relational-operator operand2
takes a value of 1 (int) if the relationship is true and 0 (int) if relationship is
false.
– Example
int a = 25, b = 30, c, d;
c = a < b; //true
d = a > b; //false
value of c will be 1 and that of d will be 0.

5 September 2022 Programming for Engineers (C language) NV Binh 22


1 Introduction to Computer and C language
Operators

• Logical Operators
– &&, || and ! are the three logical operators
 expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero
 expr1 || expr2 has a value 1 if either expr1 or expr2 is nonzero
 !expr1 has a value 1 if expr1 is zero else 0
– Example
if (marks >= 40 && attendance >= 75) grade = ‘P’
if (marks < 40 || attendance < 75) grade = ‘N’

5 September 2022 Programming for Engineers (C language) NV Binh 23


1 Introduction to Computer and C language
Operators
• Assignment operators
– The general form of an assignment operator is:
v op= exp
where v is a variable and op is a binary arithmetic operator. This
statement is equivalent to:
v = v op (exp)
– a=a+b can be written as a += b
– a=a*b can be written as a *= b
– a=a/b can be written as a /= b
– a=a-b can be written as a -= b

5 September 2022 Programming for Engineers (C language) NV Binh 24


1 Introduction to Computer and C language
Operators
• Increment and Decrement Operators
– The operators ++ and -- are called increment and decrement operators.
– Pre-increment ++a and post-increment a++ are equivalent to a += 1
– Pre-decrement --a and post-decrement a-- are equivalent to a -= 1
– ++a op b is equivalent to a++; a op b;
– a++ op b is equivalent to a op b; a++;
– Example
Let b = 10 then:
(++b) + b + b = 33

b + (++b) + b = 33
b + b + (++b) = 31
b + b * (++b) = 132

5 September 2022 Programming for Engineers (C language) NV Binh 25


1 Introduction to Computer and C language
Operators
• Increment/decrement operators
main()
{
int c;
c = 5;
printf(“%d\n”, c);
printf(“%d\n”, c++);
printf(“%d\n\n”, c);

c = 5;
printf(“%d\n”, c);
printf(“%d\n”, ++c);
printf(“%d\n”, c);

return 0;
}
5 September 2022 Programming for Engineers (C language) NV Binh 26
1 Introduction to Computer and C language
Precedence and Order of evaluation
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= Right to left
|=
Comma , Left to right
5 September 2022 Programming for Engineers (C language) NV Binh 27
1 Introduction to Computer and C language
Type Conversions
– The operands of a binary operator must have the same type and the result is
also of the same type.
– Integer division:
c = (9 / 5) * (5 - 32)
– The operands of the division are both int, and hence, the result also would be
int. For correct results, one may write:
c = (9.0 / 5.0) * (5 - 32)
– In case the two operands of a binary operator are different, but compatible,
then they are converted to the same type by the compiler. The mechanism (set of
rules) is called Automatic Type Casting.
c = (9.0 / 5) * (5- 32)
– It is possible to force a conversion of an operand. This is called Explicit Type
casting.
c = ((float) 9 / 5) * (5 – 32)

5 September 2022 Programming for Engineers (C language) NV Binh 31


1 Introduction to Computer and C language
Automatic Type Casting
1. char and short operands are converted to int
2. Lower data types are converted to the higher data types and Hierarchy
result is of higher type.
double
3. The conversions between unsigned and signed types may not
float
yield intuitive results.
long
4. Example
float f; double d; long l; int

int i; short s; short and char

d+f f will be converted to double; double result


i/s s will be converted to int; int result
l/i i is converted to long; long result

5 September 2022 Programming for Engineers (C language) NV Binh 32


1 Introduction to Computer and C language
Explicit Type Casting
– The general form of a type casting operator is:
(type-name) expression
– It is generally a good practice to use explicit casts than to
rely on automatic type conversions.
– Example
c = (float)9 / 5 * ( 5 – 32 )
– float to int conversion causes truncation of fractional part

– double to float conversion causes rounding of digits

– long int to int causes dropping of the higher order bits

5 September 2022 Programming for Engineers (C language) NV Binh 33


1 Introduction to Computer and C language
Floating Point Arithmetic
• Representation
– All floating-point numbers are presented as ±0.d1d2...dp x Be such that:
 d1 d2 d3 is nonzero

 B is the base

 p is the precision or number of significant digits

 e is the exponent

– All these put together have finite number of bits (usually 32 or 64 bits) of storage.
– Example
Assume B = 10 and p = 3
23.7 = +0.237E2
23.74 = +0.237E2
37000 = +0.370E5
37028 = +0.370E5
-0.000124 = -0.124E-4

5 September 2022 Programming for Engineers (C language) NV Binh 35

You might also like