Fundametal of C - Part - 1
Fundametal of C - Part - 1
Program
COMPUTER HARDWARE
Computer hardware includes the physical parts of a computer, such as a case,
central processing unit (CPU), random access memory (RAM), monitor, and mouse
which processes the input according to the set of instructions provided to it by the
user and gives the desired output.
Components of
computer
Hardware Software
INPUT DEVICE
The devices through which users are allowed to input data and other instructions
to the computer.
The most popular are mouse and keyboard.
Other examples are scanners, readers, etc. most input devices are electro-
mechanical in nature.
OUTPUT DEVICES
The devices used to convey the message back to the user. The most common output
device is Monitor.
The input and Output devices are sometimes known as peripheral devices.
ALGORITHMS
Finite set of unambiguous instructions which performs the task correctly.
There are three characteristic features in this description of an algorithm.
Example: write an algorithm to find the sum and average of two numbers
1. Read the numbers a, b.
2. Compute the sum of a & b
3. Store the result in variable s
4. Divide the sum s by 2
5. Store the result in variable avg
6. Print the value of s and avg
7. End of program.
Try yourself:
• Write an algorithm to find Area, Diameter and Circumference of a Circle.
• Write an algorithm to find area of a circle.
• Write an algorithm to find weather a year is a leap year or not.
Structure of a C programs
A simple C program as shown below when executed will produce print I
see, I remember as the output:
#include <stdio.h>
main()
{
/*…….
printf(“I see, I remember”);
……..*/
}
main() is a specific function used by the C system to tell the computer where the
program starts. Every program must have exactly one main function.
If there are more than one main function, the compiler cannot understand which
one is marked as the beginning of the program.
Opening braces “{“in the second line marks the beginning of the function main
and the closing braces “}” in the last line indicates the end of the function. All the
statements within forms the function body.
printf inside the braces is the only executable statement in this example. It is the
predefined standard C function for printing output.
Predefined means a function that has already been written and compiled, it is
linked to our program at the time of linking phase. Note that printf line ends with
a ‘;’
if we want to print the same message in two lines as:
I see,
I remember!
We need to write printf as:
printf(“I see, \n”);
printf(“I remember!”);
Try yourself:
• Develop a program to print your name, class and branch on same line.
• Develop a program to print your name, class and branch on different lines.
• Develop a program to assign an integer number to a variable and print.
Develop a program to assign a decimal number to a variable and print.
Operators and Expressions
Objective:
To develop basic understanding of various operators provided in C and frame an
expression using all possible combinations
Brief Theory:
Operators are used in Programs to manipulate data and variables. It is a symbol that
tells the computer to perform certain mathematical or logical manipulation.
C operators are classified as follows:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operator
5. Increment & Decrement Operator
6. Conditional Operator
7. Bit wise Operators
8. Special Operators
Arithmetic Operators:
provides all basic arithmetic operators as listed below.
Operator Usage
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
Relational Operators:
Operators Meaning
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
Logical Operators:
Operators Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Assignment Operators:
Operators Meaning
a= a+1 a += 1
a= a-1 a -= 1
a= a*1 a *= 1
a= a/1 a /= 1
a= a%b a %= 1
Increment/Decrement Operators:
C has two very useful operators, increment and decrement operators: ++ and
--. The operator ++ adds 1 to the operand, while -- subtracts 1.
Pre/Post Increment/Decrement Operators
PRE means do the operation first followed by any assignment operation.
POST means do the operation after any assignment operation.
++m or m++ and --m or m--
++m is equivalent to m=m+1 (or m+=1)/--m is equivalent to m=m-1 (or m-=1)
While m++ and ++m means the same when they form statements
independently, they behave differently when they are used in expressions on
the right-hand side of an assignment statement.
E.g. (i) m = 5; y = ++m; This statement results y and m = 6
Conditional Operator (ternary operator):
Exp1?: exp 2 : exp3
If expression 1 is true then expression 2 executed else expression 3
Example a=10, b=5
C = (a>b) ? a:b
Bitwise Operator:
Operators Meaning
& Bitwise AND
| Bitwise Or
^ Bitwise exclusive OR
<< Shift left
>> Shift right
~ One’s compliment
Problems:
1. Design an Algorithm, a Flowchart and write a program to compute all
arithmetic operations (listed on page 8) on two numbers.
2. Design an Algorithm, a Flowchart and write a program to shift the data value
by two bits to the left, let’s take the data as 88.
3. Design an Algorithm, a Flowchart and write a program to find maximum of
two numbers, make use of ternary operator.
4. Write a program to swap two numbers, using third variable and without using
third variable.
5. Rohan’s grandfather gave him 341 paisas, out of which he spent a few on 10
chocolates (Rs.15/piece) and donated 50 paisas in a church and 41 paisas in
a temple. and returned back home. His grandfather asked how many rupees
were remaining. Help him identify the remaining rupees by writing a
program showing the remaining amount as output.