Vous êtes sur la page 1sur 52

Introduction to Programming Languages

cccdigital.io
Evolution of programming languages

To solve any problem through computer machine, support of some


programming language is required.

A programming language is a tool that binds the instructions coded on to the


machine.

It is used to implement the plan of the problem solving.

The terms like high-level language, middle-level language, mid to low-level


language etc. are heard quite often. It really depends on what programming
language is used for and the level is simply referring to the amount of abstraction
between English and a compiled heap of binary
digits.

2
cccdigital.io
Evolution of programming languages: Machine Friendly Languages

The programming languages of this era dealt with front panel switches that gave
instructions directly to the processor. The instruction set contained binary set of
0’s and 1’s.

The programmer’s difficulty level in comprehending the same was very high.
However the code ran very fast, was efficient and precise because instructions
directly execute on the processor and hence was machine friendly.

Fixing the errors in the code was almost impossible and the code was purely
machine dependent and would not run on any machine other than for which it
was intended.

Non-portability, machine dependence and complex error fixing became prime


causes for the urge of a new type of programming.

3
cccdigital.io
Evolution of programming languages: Assembly Languages

The code in this era was written and read by the coders.

To make the machine understand it has to be converted to a form which is


machine readable. This process is called assembly.

The shortfalls here were, the programming done is specific to a kind of


processor and environment.

4
cccdigital.io
Evolution of programming languages: High Level Languages

The languages of this era support structured programming. They are machine
independent and programmer friendly.

The languages include ForTran, ALGOL, COBOL C, C++, BASIC, Pascal, Java,
though they are separable based on their characteristics.

The concept of aggregate data types has made this era languages greatly
distinguishable from the era of assembly codes, thus introducing new phrase
called “high level languages”.

The compiler or interpreter was employed to translate the codes to machine


language.

5
cccdigital.io
Evolution of programming languages: High Level Languages

While the Architecture independent languages still prevail, a higher level


abstraction of the hardware details has grown in demand making the
programming more powerful and efficient.

While the previous era programming languages like C, C++, Java still sustained
their importance and popularity, languages like Accell/SQL, Cogon’s,
PowerHouse and Audit have evolved with higher support for database
management, report generation, mathematical optimization, User Interface
development etc.

6
cccdigital.io
Program Development Environments

Development environment precisely is a combination of the required tools


and a set of supported processes to write a code and to create product.

It normally comprises an editor, compiler and any required libraries or


linkages.

Most of the programming languages use compilers that provide a digest of


the code to the processor in machine understandable form.

Execution of the code highly depends on the compiler compatibility to the


code. Various compilers look at the code through their own definitions.

7
cccdigital.io
Introduction to C

cccdigital.io
Introduction to C

Computer programming is no more an alien concept, and has much to do


while configuring most of the devices of daily use.

Programming is not limited to just one area; it has penetrated into almost all
areas of engineering and sciences, as the versatility of computational devices
is vast.

There are many methods of programming. Various compilers extend their


support for the same.

9
cccdigital.io
Introduction to C

“C” seems a strange name for a programming language but it is one of the
most widely used languages in the world.

C was introduced by Dennis Ritchie in 19 72 at Bell Laboratories as a


successor of the language called B (Basic Combined Programming Language
–BCPL).

Since C and UNIX have evolved hand in hand over the period of time, most
of the C attributes have the flavors of UNIX.

Operating systems of UNIX variant has the support for C code compilation
as an integral part of the operating system.

10
cccdigital.io
Structure of a C Program

A C program basically has the


following form:
Subprogram Section
Documentation Section Function_1()
Link Section {
Definition Section Statements;
Global Declaration Section }
Function_2()
main() Function Section {
{ Statements;
Declaration Part }
Executable Part
}

11
cccdigital.io
Code execution steps

The steps for the execution of the C program are as:


1. Program creation : code written in an editor
2. Program compilation : compiled using a compatible compiler
(gcc in case
of Linux)
3. Program execution : execute the compiled code

12
cccdigital.io
Tokens

The smallest part identified by the compiler in the code is called a token. It
may be a single character or a word (sequence of characters) to form a
single part.
Tokens can be classified as keywords, literals, identifiers, operators, etc.

13
cccdigital.io
Keywords

Programming languages define a set of words, called reserved words or


keywords, and reserve some meaning to them, restricting the users to use
them in the meaning conferred earlier.

14
cccdigital.io
Constants and Literals

Literals can be further classified as numeric constants, character


constants and string constants.

If the program needs a value to remain unchanged throughout the


program, then the value is declared to be constant.

Constant is a value that cannot be changed.

15
cccdigital.io
Data Types

A data type in a programming language is a set properties or attributes


associated with data.

It is used to identify the type of the data. It also specifies the size and range of
the data under a certain type.

These attributes of data depend on the choice of the programming language


chosen and may differ from language to language.

Storage representations and machine instructions to handle data types differ


from machine to machine.

16
cccdigital.io
Data Types

In programming different types of data to be processed.


C language supports 4 categories of data types:

• Primitive/Basic Data Types


• Derived Data Types
• User Defined Data Types
• Empty Data Type

17
cccdigital.io
Primitive/Basic Data Types

1. Integral Data Types:


char, int

char data type can be processed in the form of ASCII format.

2. Real Data Types:


float, double

char: a single byte, capable of holding one character in the local character set

int: an integer, reflects the size of integers on the host machine

float: single-precision floating point

double: double-precision floating point

18
cccdigital.io
Qualifiers
In addition, there are a number of qualifiers that can be applied to these
basic types. short and long apply to integers:

short int sh; long int counter;


Size of short int on any compiler is 2 bytes
Size of int is 2 or 4 bytes

(ANSI C compilers give 4 bytes for int, DOS compilers give 2 bytes for int)

Size of long int on any compiler is 4 bytes

19
cccdigital.io
Qualifiers

The qualifier signed or unsigned may be applied to char or any integer.


signed numbers contains both positive and negative numbers

signed char range is -128 to 127


unsigned char range is 0 to 255

signed int range is -2^31 to 2^31 - 1


unsigned char range is 0 to 2^32 - 1

20
cccdigital.io
Literals

Integer Literals
- decimal ( 123, 1000, 10, etc...)
- octal ( 0123, 01000, 010, etc...)
- hexadecimal (0x12, 0xAB, 0xabc, etc...)

Floating point Literals


-Standard Notation( 1.234 , 12.455, etc...)
-Scientific Notation(3.15e+6, 3.17E-3, etc...)

Character Literals ('a', 'N', '$', etc...)

String Literals ("add","abc", "m", etc...)

21
cccdigital.io
Representation of a Positive Number

Data can be represented in binary format


Ex: int a=25;
The representation of value of a in 8 bit format:
a=25 0 0 0 1 1 0 0 1

Sign bit:
25 is positive value that’s why sign bit set to zero

Ex: char ch=‘A’;


ASCII value of A is 65, 65 will be represented in binary format
0 1 0 0 0 0 0 1
Ch=‘A’

Sign bit

22
Representation of a Negative Number
Negative numbers can be Represented in 2’ complement form 2’s
complement of a number is it’s one’s complement + 1 And negative
numbers sign bit set to 1

25 00011001
11100110 (1's Complement of 25)
1
-25 1 1 1 0 0 1 1 1 (2's Complement of 25)

-25 1 1 1 0 0 1 1 1

Sign bit

23
Representation of a Negative Number

Find the bit pattern of -73

The binary equivalent of 73 is 01001001


One’s complement of 73 is 10110110
Adding one 1
By adding 1 we will get 1 0 1 1 0 1 1 1 is the representation of -73

24
Representation of a Negative Number

Find the value of this bit pattern 11001101

sign bit
In the given pattern sign bit set to 1 therefore it is a representation of a
negative number(2’s complement)

11001101
00110010
+1
00110011 = 51

Therefore the given pattern is of - 51

25
Magic of Number Representation

Positive Numbers Negative Numbers

0 00000000 -128 10000000


1 00000001 -127 10000001
2 00000010 -126 10000010
3 00000011 -125 10000011
4 00000100 -124 10000100
5 00000101 ...
... ...
... -1 11111111
127 01111111

26
Variables

A variable is a named memory location which can store a value and that value
can be modified subsequently.
Ex: char ch=‘A’;

The variable ch has been declared as char type and it has a value ‘A’. ASCII value of ‘A’ is 65.
Therefore 65 will be stored in binary format in the memory
which has been allocated for variable ch.
int a=100;

The variable a has been declared as integer type and value 100 will be stored
in binary format in the memory which has been allocated for variable a.

27
Expressions

Operands can be linked up by using operators to perform computation in an expression of a C


program. An operand can be a variable, constant, element of an array, function reference.

1. Arithmetic expressions
2. Relational expressions
3. Logical expressions
4. Conditional expressions

28
OPERATORS
• The symbols which are used to perform logical and mathematical operations
in a C program are called C operators.

Operators are mainly two categories:


1. Unary operators: Unary operators can be applied on single operand ex: -a (- is
unary minus), *b (* is redirection operator), &c (& is
address of operator) etc.

2. Binary operators: Binary operators can be applied on two operands


ex: a+b (+ is for addition +), a-b (- is for subtraction), a&b (& is for bitwise and
operation) etc.

29
OPERATORS

• Arithmetic operators • Bit wise operators

• Assignment operators • Conditional operators (ternary operators)

• Relational operators • Increment/decrement operators

• Special operators
• Logical operators

30
Arithmetic Expressions:

* / % Multiplication/Division/Modulo
+- Addition/Subtraction

Mathematical computations can be done in this arithmetic expressions.

Ex: int a=5+(3/2)*4/3-1

Evaluation of the above expression:

1. (3/2) will be computed first and result is integer quotient 1

2. Result in step 1 multiplied by 4 and result is 4

3. Result in step 2 divided by 3 and result is integer quotient 1

4. Result in step 3 added to 5 and result is 6

5. 1 will be subtracted form the Result in step 4 i.e 6-1 and final result is 5

31
Arithmetic operators

int main() {
int num1,num2;
scanf(“%d%d”,&num1,&num2);
printf(“\nAddition is%d”,num1+num2);
printf(“\nSubtraction is %d”,num1-num2);
printf(“\nMultiplication is %d”,num1*num2);
printf(“\nQuotient is %d”,num1/num2);
printf(“\nRemainder is %d”,num1%num2);
return 0;

32
Assignment operators
Assignment operator can be used to assign the simplified value of right hand
side expression to left side variable.
Ex:
int a=10, b=20;
int c=a+b;//addition of a and b can be assigned to c
= Assignment Operator
Composite Assignments:

*= /= %= Mulplication/division/modulo assignment
+= -= Addition/Subtraction assignment
&= |= ^= <<= >>= Bitwise assignment
Composite assignment ex:
int a = 10;
a += 5;//This is equal to a=a+5.

33
Relational operators
Relational operators can be used to get the relation between two
operands. Usually these are used to form conditions.

< > Comparison less than/Comparison greater than


<= Comparison less than or equal to
>= Comparison greater than or equal to
== != Comparison equal to/Comparison not equal to

The result of the relational expression can be either zero or non-zero value. Here, the zero
value is equivalent to a false and non-zero value is equivalent to true.

Ex: a<=b, a!=b, n%3==0 etc

34
Logical operators
Logical operators are used to form compound conditions by adding two or
more conditions.
&& Logical AND
|| Logical OR
! Logical NOT

Logical AND (&&):


if(a>b && a<c){ statements; }
here two conditions are combined to form a compound condition. If both
conditions are true then only the compound condition is also true. If the first condition is
false the compiler will not evaluate the second condition.

35
Logical operators
Logical OR (||):
if( a>b | | a<c ) {statements; }
here two conditions are combined to form a compound condition. If both
conditions are false then only the compound condition is also false.
If the first condition is true the compiler will not evaluate the second
condition.

Logical NOT(!): if !( a > b )//this gives the contradiction of a>b

36
Bitwise Operators:

Bitwise operators are used to perform operations at bit level.

Bitwise AND : &

Bitwise OR : |

Bitwise XOR : ^

Left shift : <<

Right shift : >>

Bitwise One’s Compliment : ~

37
Bitwise AND (&)

Truth table for bitwise &

Bit1 Bit2 Bit1 & Bit2


1 1 1
1 0 0
0 1 0
0 0 0

cccdigital.io
38
Bitwise AND (&)

#include <stdio.h>
int main()

{
printf("%d\n",(0&0)); //gives 0
printf("%d\n",(0&1)); //gives 0
printf("%d\n",(1&0)); //gives 0
printf("%d\n",(1&1)); //gives 1
return 0

cccdigital.io
39
Bitwise AND (&)

#include <stdio.h>
int main()
{
int a=112, b=17;
int c;
c=a&b;
printf(“\nBitwise and is %d “,c);
return 0;
}
Output: 16
Binary representation of 1 1 2 is 01110000
Binary representation of 17 is 00010001
a&b = 106 & 15 is 00010000 = 16

cccdigital.io
40
Bitwise OR (|)

Truth table for bitwise


|
Bit1 Bit2 Bit1 | Bit2
1 1 1
1 0 1
0 1 1
0 0 0

cccdigital.io
41
Bitwise OR (|)

#include <stdio.h>
int main()

{
printf("%d\n",(0|0)); //gives 0
printf("%d\n",(0|1)); //gives 1
printf("%d\n",(1|0)); //gives 1
printf("%d\n",(1|1)); //gives 1
return 0;

cccdigital.io
42
Bitwise OR (|)
#include <stdio.h>
int main()
{
int a=112, b=17;
int c;
c=a|b;
printf(“\nBitwise and is %d “,c);
return 0;
}

Output: 113
Binary representation of 112 is 01110000
Binary representation of 17 is 00010001
a| b = 106 | 15 is 01110001 = 113

cccdigital.io
43
Bitwise eXclusive OR (^)

Truth table for bitwise XOR (^)

Bit1 Bit2 Bit1 ^ Bit2


1 1 0
1 0 1
0 1 1
0 0 0

cccdigital.io
44
Bitwise eXclusive OR (^)

#include <stdio.h>
int main()

{
printf("%d\n",(0^0)); //gives 0
printf("%d\n",(0^1)); //gives 1
printf("%d\n",(1^0)); //gives 1
printf("%d\n",(1^1)); //gives 0
return 0;

cccdigital.io
45
Bitwise eXclusive OR - XOR(^)
#include <stdio.h>
int main()
{
int a=112, b=17;
int c;
c=a^b;
printf(“\nBitwise and is %d “,c);
return 0;
}

Output: 97
Binary representation of 112 is 01110000
Binary representation of 17 is 00010001
a^b = 1 1 2 ^ 17 is 01100001 = 97

cccdigital.io
46
Bitwise one’s Complement

Invert the bits


~0 = 1
~1 = 0
Ex: int x=17
Binary representation of 1 7 is 00010001
~x is 1 1 1 0 1 1 1 0 = -18

#include<stdio.h> If ~ n = -(n+1)
int main()
{
printf("%d",~25); Then
}
~ 72 = ?
Ourput : -26 ~ -72 = ?

cccdigital.io
47
Conditional operator (ternary operator) ?:

?: Ternary Conditional Operator

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a,b; int a,b;
scanf(“%d%d”,&a,&b); scanf(“%d%d”,&a,&b);
if(a>b){ printf(“a is big); } (a>b)?printf(“a is big):printf(“b is big”);
else{ printf(“b is big”); } return 0;
return 0; }
}

48
Increment/decrement operators

++ : increment operator increases the value of a variable by 1.


-- : decrement operator decreases the value of a variable by 1.

49
Pre increment vs Post increment:
Ex: int a=10, b;
b=a++;

The above expression can be as


b = a;
a = a+1;
i.e. a = 11 , b = 10;

int a = 10, b;
b = ++a;

The above expression can be as


a = a+11;
b = a;
i.e. a = 11 , b = 10;

50
Pre decrement vs Post decrement:
Ex : int a = 10, b;
b = a--;

The above expression can be as


b = a;
a = a-1;
i.e. a = 9, b =10;

int a=10,b;
b=--a;

a = a-1;
b = a;
i.e. a = 9, b = 9;

51
Special operators
& ➔ gives address of a variable
* ➔ Acts as a re directional operator
sizeof( )➔ operator used to get size of a specific variable
; ➔ every statement in c program ends with semi colon
, ➔ two or more expressions can be separated by comma
. ➔ Members of structures and unions can be accessed using dot
: ➔ is used switch statement and ternary operator
() ➔ are mentioned as function calling
[] ➔ are for array indexing
{} ➔ are for block opening and closing

52

Vous aimerez peut-être aussi