Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views25 pages

SWE CSN 125 CWD122 Lectures Notes 1

Uploaded by

keridevs
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)
3 views25 pages

SWE CSN 125 CWD122 Lectures Notes 1

Uploaded by

keridevs
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/ 25

SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Introduction
COMPUTER SYSTEMS
―A Computer is an electronic device that stores, manipulates and retrieves the data.
The following are the objects of computer System
a) User ( A person who uses the computer)
b) Hardware
c) Software

Hardware: Hardware of a computer system can be referred as anything which we can


touch and feel. Example : Keyboard and Mouse.

The hardware of a computer system can be classified as :


➢ Input Devices(I/P)
➢ Processing Devices (CPU)
➢ Output Devices(O/P)

COMPUTER SOFTWARE
Software of a computer system can be referred as anything which we can feel and see.
Example: Windows, icons

Computer software is divided in to two broad categories:


➢ system software and
➢ application software .

System software manages the computer resources. .It provides the interface between the
hardware and the users.
Application software, on the other hand is directly responsible for helping users solve their
problems.
System Software
System software consists of programs that manage the hardware resources of a computer
and perform required information processing tasks.
These programs are divided into three classes:
➢ the operating system,
➢ system support, and
➢ system development.

Copyright @ Eugene Tebo April 2023


1
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

The operating system provides services such as a user interface, file and database access,
and interfaces to communication systems such as Internet protocols.
The primary purpose of this software is to keep the system operating in an efficient manner
while allowing the users access to the system.

System support software provides system utilities and other operating services.
Examples of system utilities are sort programs and disk format programs.
Operating services consists of programs that provide performance statistics for the
operational staff and security monitors to protect the system and data.

The last system software category, system development software, includes the language
translators that convert programs into machine language for execution ,debugging tools to
ensure that the programs are error free and computer –assisted software
engineering(CASE) systems.

Application software
Application software is broken in to two classes:
➢ general-purpose software and
➢ application – specific software.

General purpose software is purchased from a software developer and can be


used for more than one application.
Examples of general purpose software include word processors, database management
systems ,and computer aided design systems.
They are labeled general purpose because they can solve a variety of user computing
problems.

Application –specific software can be used only for its intended purpose.

COMPUTER LANGUAGES

To write a program (tells what to do) for a computer, we must use a computer language.
Over the years computer languages have evolved from machine languages to natural
languages.

Copyright @ Eugene Tebo April 2023


2
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

The following is the summary of computer languages :

1) Machine Languages (machine codes):


• Strings of 1s and 0s.
• Only unterstood by integrated circuits, such as microprocessors
Example:
10100010
01011011
10101010
Advantages:
1) High speed execution
2) The computer can understood instructions immediately
3) No translation is needed.
Disadvantages:
1) Machine dependent
2) Programming is very difficult
3) Difficult to understand
4) Difficult to write bug free programs
5) Difficult to isolate an error

2) Assembly Languages:
• English-like abbreviations representing elementary computer
operations.
• translated to machine code by using assemblers.
Example:
MOV AL,3BH
ADD AL, AH
SUB AL,AH
MOV [SI]

Advantages:
1) Easy to understand and use
2) Easy to modify and isolate error
3) High efficiency

Copyright @ Eugene Tebo April 2023


3
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

4) More control on hardware


Disadvantages:
1) Machine Dependent Language
2) Requires translator
3) Difficult to learn and write programs
4) Slow development time
5) Less efficient

3) High-level Languages:
• Codes similar to everyday English
• Use mathematical notations
• translated to machine code by using compilers.
• C, C++, PASCAL, FORTRAN, BASIC are high-level languages.
Example:
c=a+b;
if(a<b)
printf(“a is less than b\n”);
else
printf(“a is NOT less than b\n”);
Advantages:
1) Easy to write and understand
2) Easy to isolate an error
3) Machine independent language
4) Easy to maintain
5) Better readability
6) Low Development cost
7) Easier to document
8) Portable
Disadvantages:
1) Needs translator
2) Requires high execution time
3) Poor control on hardware
4) Less efficient
Difference between Machine, Assembly, High Level Languages

Feature Machine Assembly High Level


Form 0‘s and 1‘s Mnemonic codes Normal English
Machine
Dependent Dependent Independent
Dependent
Translator Not Needed Needed(Assembler) Needed(Compiler)
Execution Time Less Less High
Different Different
Languages Only one
Manufactgurers Languages

Copyright @ Eugene Tebo April 2023


4
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Nature Difficult Difficult Easy


Memory Space Less Less More

Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be excuted by the computer.
1) Compiler: It is a program which is used to convert the high level language
programs into machine language
2) Assembler: It is a program which is used to convert the assembly level
language programs into machine language
3) Interpreter: It is a program, it takes one statement of a high level language
program, translates it into machine language instruction and then immediately
executes the resulting machine language instruction and so on.
Comparison between a Compiler and Interpreter

COMPILER INTERPRETER
An interpreter is used to translate
A Compiler is used to compile an entire
each line of
program and an executable program is
the program code immediately as it
generated through the object program
is entered
The executable program is
The executable program is stored in a
generated in RAM
disk for
and the interpreter is required for
future use or to run it in another
each run of
computer
the program
The compiled programs run faster The Interpreted programs run slower
A very few languages use
Most of the Languages use compiler
interpreters.

CREATING AND RUNNING PROGRAMS


The procedure for turning a program written in C into machine Language. The process is
presented in a straightforward, linear fashion but you should recognize that these steps are
repeated many times during development to correct errors and make improvements to the
code.

The following are the four steps in this process


1) Writing and Editing the program
2) Compiling the program
3) Linking the program with the required modules
4) Executing the program

Copyright @ Eugene Tebo April 2023


5
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Sl. No. Phase Name of Code Tools File Extension


C Compilers
1 TextEditor Source Code Edit, .C
Notepad Etc..,
2 Compiler Object Code C Compiler .OBJ
Executable
3 Linker C Compiler .EXE
Code
Executable
4 Runner C Compiler .EXE
Code

Writing and Editing Programs


The software used to write programs is known as a text editor. A text editor helps
us enter, change and store character data. Once we write the program in the text editor
we save it using a filename stored with an extension of .C.
This file is referred as source code file.
Compiling Programs
The code in a source file stored on the disk must be translated into machine language.
This is the job of the compiler.
The Compiler is a computer program that translates the source code written in a high-level
language into the corresponding object code of the low-level language.
This translation process is called compilation.
The entire high level program is converted into the executable machine code file.
The Compiler which executes C programs is called as C Compiler.

Copyright @ Eugene Tebo April 2023


6
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Example Turbo C, Borland C, GC etc.,

The C Compiler is actually two separate programs:

➢ The Preprocessor
➢ The Translator

The Preprocessor reads the source code and prepares it for the translator.
While preparing the code, it scans for special instructions known as preprocessor
commands. These commands tell the preprocessor to look for special code libraries.
The result of preprocessing is called the translation unit.

After the preprocessor has prepared the code for compilation, the translator does the
actual work of converting the program into machine language. The translator reads the
translation unit and writes the resulting object module to a file that can then be combined
with other precompiled units to form the final program.
An object module is the code in the machine language.

Linking Programs
The Linker assembles all functions, the program‘s functions and system‘s functions into
one executable program.

Executing Programs
To execute a program we use an operating system command, such as run, to load the
program into primary memory and execute it.
Getting the program into memory is the function of an operating system program known as
the loader. It locates the executable program and reads it into memory. When everything is
loaded the program takes control and it begin execution.

ALGORITHM
Algorithm is a finite sequence of instructions, each of which has a clear meaning and can
be performed with a finite amount of effort in a finite length of time. No matter what the
input values may be, an algorithm terminates after executing a finite number of instructions.

We represent an algorithm using a pseudo language that is a combination of the


constructs of a programming language together with informal English statements.

Copyright @ Eugene Tebo April 2023


7
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

The ordered set of instructions required to solve a problem is known as an algorithm.

The characteristics of a good algorithm are:


➢ Precision – the steps are precisely stated (defined).
➢ Uniqueness – results of each step are uniquely defined and only depend on the input
and the result of the preceding steps.
➢ Finiteness – the algorithm stops after a finite number of instructions are executed.
➢ Input – the algorithm receives input.
➢ Output – the algorithm produces output.
➢ Generality – the algorithm applies to a set of inputs.

Example
Q. Write a algorithm to find out if a number is odd or even?
Ans.
step 1 : start
step 2 : input number
step 3 : rem=number mod 2
step 4 : if rem=0 then
print "number even"
else
print "number odd"
endif
step 5 : stop

FLOWCHART
Flowchart is a diagrammatic representation of an algorithm. Flowchart is very helpful in
writing program and explaining program to others.
Symbols Used In Flowchart
Different symbols are used for different states in flowchart, For example: Input/Output and
decision making has different symbols. The table below describes all the symbols that are
used in making flowchart

Copyright @ Eugene Tebo April 2023


8
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Examples of flowcharts in programming


Draw a flowchart to add two numbers entered by user.

Copyright @ Eugene Tebo April 2023


9
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Draw flowchart to find the largest among three different numbers entered by user.

INTRODUCTION TO C LANGUAGE

C is a general-purpose high level language that was originally developed by Dennis Ritchie
for the Unix operating system. It was first implemented on the Digital Equipment Corporation
PDP-11 computer in 1972
The Unix operating system and virtually all Unix applications are written in the C language.
C has now become a widely used professional language for various reasons.

➢ Easy to learn
➢ Structured language
➢ It produces efficient programs.
➢ It can handle low-level activities.
➢ It can be compiled on a variety of computers.

Facts about C
➢ C was invented to write an operating system called UNIX.
➢ C is a successor of B language which was introduced around 1970
➢ The language was formalized in 1988 by the American National Standard Institue
(ANSI).
➢ By 1973 UNIX OS almost totally written in C.
➢ Today C is the most widely used System Programming Language.
➢ Most of the state of the art software have been implemented using C

Copyright @ Eugene Tebo April 2023


10
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Why to use C?
C was initially used for system development work, in particular the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as code written in assembly language.

Some examples of the use of C might be:


• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Data Bases
• Language Interpreters
• Utilities

C Program File
All the C programs are written into text files with extension ".c" for example hello.c. You can
use "vi" editor to write your C program into a file
BASIC STRUCTURE OF C PROGRAMMING

1. Documentation section: The documentation section consists of a set of comment lines


giving the name of the program, the author and other details, which the programmer
would like to use later.
2. Link section: The link section provides instructions to the compiler to link functions
from the system library such as using the #include directive.
3. Definition section: The definition section defines all symbolic constants such using
Copyright @ Eugene Tebo April 2023
11
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

the #define directive.


4. Global declaration section: There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all
the user-defined functions.
5. main () function section: Every C program must have one main function section.
This section contains two parts; declaration part and executable part
➢ Declaration part: The declaration part declares all the variables used in the
executable part.
➢ Executable part: There is at least one statement in the executable part.
These two parts must appear between the opening and closing braces.

The program execution begins at the opening brace and ends at the closing brace.

The closing brace of the main function is the logical end of the program.

All statements in the declaration and executable part end with a semicolon.

6. Subprogram section: If the program is a multi-function program then the subprogram


section contains all the user-defined functions that are called in the main () function.
User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.

PROCESS OF COMPILING AND RUNNING C PROGRAM


We will briefly highlight key features of the C Compilation model here

Copyright @ Eugene Tebo April 2023


12
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

The C Compilation Model


The Preprocessor
The Preprocessor accepts source code as input and is responsible for
➢ removing comments
➢ Interpreting special preprocessor directives denoted by #

For example

• #include -- includes contents of a named file. Files usually called header files.
➢ #include <math.h> -- standard library maths file.
➢ #include <stdio.h> -- standard library I/O file

• #define -- defines a symbolic name or constant. Macro substitution.


➢ #define MAX_ARRAY_SIZE 100

C Compiler
The C compiler translates source to assembly code. The source code is received from the
preprocessor.
Assembler
The assembler creates object code. On a UNIX system you may see files with a .o suffix
(.OBJ on MSDOS) to indicate object code files.
Link Editor
If a source file references library functions or functions defined in other source files the link
editor combines these functions (with main()) to create an executable file.

Copyright @ Eugene Tebo April 2023


13
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
• The following program displays "Hello World" on the computer screen (monitor).

The program output


Hello World
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Comments:
• Text surrounded by /* and */ is ignored by computer.
• Used to describe program.

#include <stdio.h>
Preprocessor directive:
• Tells computer to load contents of a header file <stdio.h>,
• which includes standard input/output functions.
• For example printf()is one of the standard input/output functions.
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
int main()
• C programs contain one or more functions,

Copyright @ Eugene Tebo April 2023


14
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

• One of the functions must be main().


• Parenthesis used to indicate a function
• int means that main "returns" an integer value
• Braces ({ and }) indicate a block
• The bodies of all functions must be contained in braces.
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
printf("Hello World\n");
• printf() function print the string of characters within quotes (" ")
• All statements must end with a semicolon (;)
• \n is the newline character.

return 0;
• A way to exit a function.
• return 0, in this case, means that the program terminated normally.

Right brace }
• Indicates end of main has been reached.
Example 1: Write a C program which displays your name and surname in two consecutive
lines .
Example 2: Write a C program which displays the following lines.
Today
is a
nice
day
C Program: Addition of two integer numbers
/* This program adds two integer numbers */
#include <stdio.h>
int main()
{
int a, b, sum; /* variable declarations */
printf("Enter first integer\n"); /* prompt the user */
scanf( "%d", &a); /* read first integer */
printf("Enter second integer\n"); /* prompt the user */

Copyright @ Eugene Tebo April 2023


15
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

scanf( "%d", &b); /* read second integer */


sum = a + b; /* calculate the sum */
printf( "Sum = %d\n", sum ); /* print the calculated sum*/
return 0; /* indicate that program ended successfully */
}
Enter first integer
15
Enter second integer
26
Sum = 41
C Program: Addition of two integer numbers
int a, b, sum;
• Declaration of variables
- Variables: locations in memory where a value can be stored
• int means the variables can hold integer numbers (-1, 3, 0, 47)
• Variable names (identifiers)
- a, b, sum;
- Identifiers: consist of letters, digits (cannot begin with a digit) and
underscores( _ ). They are Case sensitive
• Declarations appear before executable statements
- If an executable statement references and undeclared variable it
will produce a syntax (compiler) error.
C Program: Addition of two integer numbers
scanf( "%d", &a );
• Obtains(reads/inputs) a value from the user
▪ scanf uses standard input (usually keyboard)
• This scanf statement has two arguments
%d - indicates data should be a decimal integer
&a – location (address) in memory to store variable a.
& is confusing in beginning – for now, just remember to include it with the variable name in
scanf statements.
• When executing the program the user responds to the scanf statement by typing in a
number, then pressing the enter (return) key.
= (assignment operator)
• Assigns a value to a variable
• Is a binary operator (has two operands)
sum = a + b;
sum gets a + b;
• Variable receiving value on left
printf( "Sum is %d\n", sum );
• Similar to scanf

Copyright @ Eugene Tebo April 2023


16
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

- %d means decimal integer will be printed


- sum specifies what integer will be printed
• Calculations can be performed inside printf statements
printf( "Sum is %d\n", a + b );
C Program: Addition of two integer numbers

• Example 3: Write a C program which calculates and displays the


addition of integers 7, 8 and 14..

• Example 4: Write a C program which asks the user to enter 3


integer numbers and outputs the sum of these three numbers.

C TOKENS
C tokens are the basic buildings blocks in C language which are constructed together to
write a C program.

Each and every smallest individual unit in a C program is known as C tokens.


C tokens are of six types. They are
Keywords (eg: int, while),
Identifiers (eg: main, total),
Constants (eg: 10, 20),
Strings (eg: “total”, “hello”),
Special symbols (eg: (), {}),
Operators (eg: +, /,-,*)

C KEYWORDS

C keywords are the words that convey a special meaning to the c compiler. The keywords
cannot be used as variable names.
The list of C keywords is given below:

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

C IDENTIFIERS
Identifiers are used as the general terminology for the names of variables, functions and

Copyright @ Eugene Tebo April 2023


17
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

arrays.
These are user defined names consisting of arbitrarily long sequence of letters and digits
with either a letter or the underscore(_) as a first character.

There are certain rules that should be followed while naming c identifiers:

➢ They must begin with a letter or underscore (_).


➢ They must consist of only letters, digits, or underscore. No other special character is
allowed.
➢ It should not be a keyword.
➢ It must not contain white space.
➢ It should be up to 31 characters long as only first 31 characters are significant.

Some examples of c identifiers:

Name Remark
_A9 Valid
Invalid as it contains special character other than the
Temp.var
underscore
void Invalid as it is a keyword

C CONSTANTS
A C constant refers to the data items that do not change their value during the program
execution. Several types of C constants that are allowed in C are:

Integer Constants
Integer constants are whole numbers without any fractional part. It must have at least one
digit and may contain either + or – sign. A number with no sign is assumed to be positive.
There are three types of integer constants:

Decimal Integer Constants


Integer constants consisting of a set of digits, 0 through 9, preceded by an optional – or +
sign.
Example of valid decimal integer constants
341, -341, 0, 8972

Octal Integer Constants


Integer constants consisting of sequence of digits from the set 0 through 7 starting with 0 is
said to be octal integer constants.

Copyright @ Eugene Tebo April 2023


18
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Example of valid octal integer constants


010, 0424, 0, 0540

Hexadecimal Integer Constants


Hexadecimal integer constants are integer constants having sequence of digits preceded
by 0x or 0X. They may also include alphabets from A to F representing numbers 10 to 15.

Example of valid hexadecimal integer constants


0xD, 0X8d, 0X, 0xbD
It should be noted that, octal and hexadecimal integer constants are rarely used in
programming.

Real Constants
The numbers having fractional parts are called real or floating point constants. These may
be represented in one of the two forms called fractional form or the exponent form and
may also have either + or – sign preceding it.

Example of valid real constants in fractional form or decimal notation


0.05, -0.905, 562.05, 0.015

Representing a real constant in exponent form


The general format in which a real number may be represented in exponential or scientific
form is
mantissa e exponent

The mantissa must be either an integer or a real number expressed in decimal notation.
The letter e separating the mantissa and the exponent can also be written in uppercase i.e.
E
And, the exponent must be an integer.

Examples of valid real constants in exponent form are:


252E85, 0.15E-10, -3e+8

Character Constants
A character constant contains one single character enclosed within single quotes.
Examples of valid character constants
‘a‘ , ‘Z‘, ‘5‘
It should be noted that character constants have numerical values known as ASCII values,

Copyright @ Eugene Tebo April 2023


19
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

for example, the value of ‘A‘ is 65 which is its ASCII value.

Escape Characters/ Escape Sequences


C allows us to have certain non graphic characters in character constants. Non graphic
characters are those characters that cannot be typed directly from keyboard, for example,
tabs, carriage return, etc.
These non graphic characters can be represented by using escape sequences epresented
by a backslash() followed by one or more characters.

NOTE: An escape sequence consumes only one byte of space as it represents a single
character.
Escape Sequence Description
a Audible alert(bell)
b Backspace
f Form feed
n New line
r Carriage return
t Horizontal tab
v Vertical tab
\ Backslash
― Double quotation mark
‗ Single quotation mark
? Question mark
Null

STRING CONSTANTS
String constants are sequence of characters enclosed within double quotes. For example,
“hello”
“abc”
“hello9”

Every sting constant is automatically terminated with a special character „‟ called thenull
character which represents the end of the string.
For example, “hello” will represent “hello” in the memory.

Thus, the size of the string is the total number of characters plus one for the null character.
Special Symbols
The following special symbols are used in C having some special meaning and thus, cannot
be used for some other purpose.
[] () {} , ; : * … = #

Copyright @ Eugene Tebo April 2023


20
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Braces{}: These opening and ending curly braces marks the start and end of a block of
code containing more than one executable statement.
Parentheses(): These special symbols are used to indicate function calls and function
parameters.
Brackets[]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.

VARIABLES
A variable is nothing but a name given to a storage area that our programs can
manipulate.
Each variable in C has a specific type, which determines
➢ the size and layout of the variable's memory;
➢ the range of values that can be stored within that memory;
➢ and the set of operations that can beapplied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character.
It must begin with either a letter or an underscore. Upper and lowercase letters are distinct
because C is case-sensitive.
Based on the basic types explained, there will be the following basic variable types -

Type Description
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.

C programming language also allows defining various other types of variables like
Enumeration, Pointer, Array, Structure, Union, etc.

Variable Definition in C
A variable definition tells the compiler where and how much storage to create for the
variable.
A variable definition specifies a data type and contains a list of one or more variables of
that type as follows -

type variable_list;

Copyright @ Eugene Tebo April 2023


21
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

Here, type must be a valid C data type including char, w_char, int, float, double, bool, or
any user-defined object; and variable_list may consist of one or more identifier names
separated by commas. Some valid declarations are shown here –
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which instruct the compiler to
create variables named i, j and k of type int.

Variables can be initialized (assigned an initial value) in their declaration. The initializer
consists of an equal sign followed by a constant expression as follows –
type variable_name = value;

Some examples are -


extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.

For definition without an initializer: variables with static storage duration are implicitly
initialized with NULL (all bytes have the value 0); the initial value of all other variables are
undefined.
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with
the given type and name so that the compiler can proceed for further compilation without
requiring the complete detail about the variable.
A variable definition has its meaning at the time of compilation only; the compiler needs
actual variable definition at the time of linking the program.
A variable declaration is useful when multiple files are used.

OPERATORS AND EXPRESSIONS


C language offers many types of operators. They are,
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators

Copyright @ Eugene Tebo April 2023


22
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

6. Conditional operators (ternary operators)


7. Increment/decrement operators
8. Special operators

S.no Types of Operators Description


These are used to perform mathematical calculations
1 Arithmetic_operators like addition, subtraction, multiplication, division
and modulus
Assignment_operator These are used to assign the values for the variables
2
s in C programs.
These operators are used to compare the value of two
3 Relational operators
variables.
These operators are used to perform logical operations
4 Logical operators
on the given two variables
These operators are used to perform bit operations on
5 Bit wise operators
given two variables.
Conditional (ternary) Conditional operators return one value if condition is
6
operators true and returns another value is condition is false.
Increment/decreme
These operators are used to either increase or
7 nt
decrease the value of the variable by one.
operators
8 Special operators &, *, sizeof( ) and ternary operators.

ARITHMETIC OPERATORS IN C
C Arithmetic operators are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus in C programs.
Arithmetic
S.no Operation Example
Operators
1 + Addition A+B
2 – Subtraction A-B
3 * multiplication A*B
4 / Division A/B
5 % Modulus A%B

EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS


In this example program, two values “40” and “20” are used to perform arithmetic
operations such as addition, subtraction, multiplication, division, modulus and output is
displayed for each operation.
#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;

Copyright @ Eugene Tebo April 2023


23
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
OUTPUT:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0

ASSIGNMENT OPERATORS IN C
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as
“sum = 10;”
Other assignment operators in C language are given below.

Copyright @ Eugene Tebo April 2023


24
SWE_CSN125_CWD122 : Programming 1:, Lecture Notes 01

EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATORS:


In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
Assignment operators such as “=” and “+=” are used in this program to assign the values
and to
sum up the values.
# include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}

OUTPUT:
Total = 45

RELATIONAL OPERATORS IN C
Relational operators are used to find the relation between two variables. i.e. to compare
the values of two variables in a C program.

Copyright @ Eugene Tebo April 2023


25

You might also like