Fundamentals of The C Programming

You might also like

You are on page 1of 30

NATIONAL ECONOMICS UNIVERSITY

SCHOOL OF INFORMATION TECHNOLOGY AND DIGITAL ECONOMICS

CHAPTER 2
FUNDAMENTALS OF THE C PROGRAMMING
OUTLINE

¡ Variables
¡ Intro to Data Types
¡ Int, Float and Double Data Types
¡ Char Data Type and ASCII table
¡ Operators
¡ Type Casting
VARIABLES

¡ Variable: a name associated with a memory cell whose value can


change

Address Value

myNumber
00193959A02F 23

Computer Memory

¡ Variable Declaration: tells the compiler name and data type of a


variable
¡ Variable initialization: assigns an initial value for a variable
NAMING VARIABLES

¡ Variable name must start with a letter


¡ May contain letter, number and the underscore character.
¡ Shoud use _ or cameCase, meaningful, not too short/long
names.

iPhone eBay

camel Case numberOfStudent

getDeletedRecords()
INTRO TO DATA TYPES

¡ Data type: a set of values Data Type


and operations that can
be performed on those Primitive Complex

values
int float char void Arrays

int float char Struct

unsigned
long int double char Union

long
short int double

unsigned

...
NUMERIC DATA TYPES

¡ Numeric data types are used to describe numbers


¡ Any number can be classified as one of many numeric data types.
DATA TYPE INTEGER

¡ The int data type is used to represent integers in C.


¡ Some values that you can store in a type int variable are:
-10500 435 +15 -25 32767
¡ Integer types in C:
Type Range
short −32,767 .. 32,767
unsigned short 0 .. 65,535
int −2,147,483,647 .. 2,147,483,647
unsigned 0 .. 4,294,967,295
long −9,223,372,036,854,775,807 .. +9,223,372,036,854,775,807
unsigned long 0 .. +18,446,744,073,709,551,615
DATA TYPE DOUBLE

¡ A real number has an integral part and a fractional part that are
separated by a decimal point.
¡ In C, the data type double is used to represent real numbers. For
example:
3.141592 2.85 150.0
¡ Double types in C:

Type Approximate Range Significant Digits


float 10−37 .. 1038 6
double 10−307 .. 10308 15
long double 10−4931 .. 104932 19
SCIENTIFIC NOTATION WITH FLOATING POINT
NUMBERS

¡ Scientific Notation is a special way of writing numbers. For


example:

¡ Scientific Notation is written in two parts:


FORMAT CHARACTERS

¡ Printf formatting is controlled by Conversion character:


¡ %f
Conversion character
¡ %e %E
¡ %g %G
printf( "%2.3f", 1.2 );
¡ …
¡ Spaces wide format

¡ Precision format Spaces wide Precision


DATA TYPE CHAR

¡ Data type char represents an individual character value: a letter,


a digit, or a special symbol.
¡ Each type char value is enclosed in single quotes:
'A’ ‘z’ ‘2’ ‘9’ '*’ ‘:’ ‘’
ASCII

¡ The ASCII code (American Standard Code for Information


Interchange):
PRINT CHAR AND GET IT FROM USER INPUT

¡ %c format character to print, scan char data:

#include <stdio.h>

int main() {
char x;
scanf("%c", &x);
printf("%c", x); printf(“%c”,x)
return 0;
}
ASCII AND INT CONVERSION

¡ In C programming, a character variable holds ASCII value;


¡ ‘A’ → 65
¡ ‘B’ → 66
¡ ‘C’ → 67 #include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
INTRO TO OPERATORS

¡ Statement, Variable, Expression:

X = 5 + 8;
¡ There are following three types of operators in C language:
¡ Arithmetic Operators: +, -, *, /, %
¡ Increment and Decrement Operators: ++, --
¡ Assignment Operators: =, +=, -=, *=, /=, %=
¡ Relational Operators: <, <=, >, >=, ==, !=
¡ Logical Operators: &&, ||, !
¡ Bitwise Operators: &, |, <<, >>, -, ^
¡ Ternary Operators: ?:
ARITHMETIC OPERATORS

¡ All the arithmetic operators:

Operator Meaning Examples


5 + 2 is 7
+ Addition
5.0 + 2.0 is 7.0
5 − 2 is 3
- Subtraction
5.0 − 2.0 is 3.0
5 * 2 is 10
* Multiplication
5.0 * 2.0 is 10.0
5 / 2 is 2
/ Division
5.0 / 2.0 is 2.5
% Remainder 5 % 2 is 1
MODULUS OPERATOR

¡ The moduls operator (%) returns the remainder left over when
one operand is divided by a second operand.
¡ For example, 6 (slices) % 5 (eaters) will result in 1

1 left over
INCREMENT AND DECREMENT OPERATORS

Increment Operator: ++ Decrement Operator: --

int i = 2 int i = 2
i = i + 1 i = i - 1

++i or i++ --i or i--


Prefix Suffix Prefix Suffix
increment increment decrement decrement
ASSIGNMENT OPERATORS

¡ Assignment operators are used to assigning value to a variable.


¡ The value on the right side must be of the same data-type of the
variable on the left side

Operator Example Equivalent Result


(m=10) Expression
= m = 10
+= m += 10 m = m+10 25
-= m -= 10 m = m-10 5
*= m *= 10 m = m*10 150
/= m /= 10 m = m/10 1
*= m %= 10 m = m%10 5
OTHER OPERATORS

¡ Relational Operators: <, <=, >, >=, ==, !=


¡ Logical Operators: &&, ||, !
¡ Bitwise Operators: &, |, <<, >>, -, ^
¡ Ternary Operators: ?:
OPERATOR PRECEDENCE

Precedence Operator Description Associativity


1 ++ -- Suffix/postfix increment and decrement Left-to-right
++ -- Prefix increment and decrement
2 +- Unary plus and minus Right-to-left
!~ Logical NOT and bitwise NOT
3 */% Multiplication, division, and remainder
4 +- Addition and subtraction
5 << >> Bitwise left shift and right shift
6 < <= > >= For relational operators < and ≤ respectively
Left-to-right
7 == != For relational = and ≠ respectively
8 &^| Bitwise Operators
9 && Logical AND
10 || Logical OR
11 ?: Ternary conditional
Right-to-Left
12 += -= *= /= %= Assignment Operators
STRONGLY TYPED
VS LOOSELY TYPE LANGUAGES

Strongly typed Loosely typed


int x; var x;
x = 5; x = 5;
x = 2.3; //x = ? x = 2.3;
int z = x + 2.3 x = “hello”

C JavaScript
BASIC TYPE CASTING

¡ Converting one datatype into another is known as type casting

1 quarter

4 * 0.25 dollar = 1.0 dollar


1 Dollar
(int) 1.0 dollar = 1 dollar
IMPLICIT CASTING

¡ Also known as “automatic type conversion”

#include <stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;

printf("x = %d, z = %f", x, z);


return 0;
}
EXPLICIT CASTING

¡ The general form of the cast operator is


specifies variable
the data or
you want expression

(data type) x

¡ For example:
¡ (double)a ¡ (int)(5.6*4)

¡ (int)2.5 ¡ (double)(2+(int)3.4)
¡ 10/(float)2
¡ (float)2+3
EXERCISE 1

¡ Variable declaration :
int a = 125, b = 12345;
long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;
¡ Write a C program to display;
a + c, x + c, dx + x, ((int) dx) + ax, a + x, s +
b, ax + b, s + c, ax + c, ax + ux
EXERCISE 2

¡ Write a C program to calculate a bike’s average consumption


(litters/100km) from the given total distance (integer value) traveled
(in km) and spent fuel (in liters, float number – 2 decimal point)
EXERCISE 3

¡ Write a C program to read an amount (integer value) and break the


amount into smallest possible number of bank notes. (1.000 -> 500.
000)
¡ Input the amount: 375.000

3 Note(s) of 100.000
1 Note(s) of 50.000
1 Note(s) of 20.000
0 Note(s) of 10.000
1 Note(s) of 5.000
0 Note(s) of 2.000
0 Note(s) of 1.000
EXERCISE 4

¡ Write a C program to convert a given integer (in


seconds) to hours, minutes and seconds.
SUMMARY

¡ Variables
¡ Intro to Data Types
¡ Int, Float and Double Data Types
¡ Char Data Type and ASCII table
¡ Operators
¡ Type Casting

You might also like