Unit 1 Introduction
Unit 1 Introduction
Introduction
FE - C Programming
Why Learn C?
•It is one of the most popular programming
language in the world
•If you know C, you will have no problem
learning other popular programming
languages such as Java, Python, C++, C#, etc,
as the syntax is similar
•C is very fast, compared to other
programming languages, like Java and Python
● Features of the C language
1.Simple
3.Memory Management
It supports the feature of dynamic memory allocation.
4. Speed
compilation and execution time of C language is fast
5. Pointer
C provides the feature of pointers. We can directly interact
with the memory by using the pointers. We can use
pointers for memory, structures, functions, array, etc.
6. Recursion
In C, we can call the function within the function. It
provides code reusability for every function.
Unit 1: Introduction
Introduction to components of a Computer
System
● WHAT IS A COMPUTER?
• Computer is a device that transforms data
into meaningful information.
• Data can be anything like marks obtained
by you in various subjects.
• It can also be name, age, sex, weight,
height, etc. of all the students in a class.
• Computer can also be defined in terms
of functions it can perform.
• A computer can
i)accept data,
ii)store data,
iii)process data as desired, and
iv)retrieve the stored data as and when
required and
v)print the result in desired format.
The major characteristics of a computer are
high speed, accuracy, and storage.
● Computer Organisation
1.
● The computer performs basically five major operations
These are
1)it accepts data or instruction by way of input,
2)it stores data,
3)it can process data as required by the user,
4)it gives results in the form of output, and
5)it controls all operations inside a computer.
1.Input: this is the process of entering data and programs into the
computer system.
2.Control Unit (CU): The process of input, output, processing and storage is
performed under the supervision of a unit called 'Control Unit'. It decides when
to start receiving data, when to stop it, where to store data, etc. It takes care of
step -by-step processing of all operations in side the computer.
3.Memory Unit: Computer is used to store data and instructions.
4.Arithmetic Logic Unit (ALU): The major operations performed by the
ALU are addition, subtraction, multiplication, division, logic and comparison.
5.Output: This is the process of producing results from the data for getting
useful information.
The ALU and the CU of a computer system are jointly known as the central
processing unit (CPU). handling everything from basic input/output
operations to complex operations,
● SOFTWARE
Step 1: Start
Step 2: Declare variables num1,
num2 and sum.
Step 3: Read values num1 and
num2.
Step 4: Add num1 and num2 and assign the
result to sum. sum=num1+num2
Step 5: Display sum
Step 6: Stop
❖Principles of Flowchart
1.Simplicity: -
-Easy to put on paper
-Easy to draw
-Readable and meaningful
2.Organization:
-putting ideas together and organizing those
ideas in a logical way.
3.Communication: A flowchart is a
better way of communicating the logic of a
program.
Symbols Used In Flowchart
Algorithm
Step-1 Start
Step-2 Input first numbers say A
Step-3 Input second number say B
Step-4. SUM = A + B
Step-5 Display SUM
Step-6 Stop
2. Flowchart to find largest
number of two numbers
C Program can be divided into header, main() function, variable
declaration, body, and return type of the program.
C is a structured programming language because it divides the
programs into small modules called functions which makes the
execution easier.
Program in C
This is a preprocessor command that includes standard input output header file(stdio.h)
from the C library before compiling a C program
#include <stdio.h>
int main(): An int data type used with the main() function that
indicates the function should return an integer value.
Conio.h is a header file stand for console input-
output.It is a header file with several built-in
functions that are commonly used to execute
input/output on the console, ie: it is used to take
input from the keyboard given by the user and
display output on the screen.
clrscr()
This function is used to clear the output on the
screen.
return 0 in the main function means that the program
executed successfully. return 1 in the main function means
that the program does not execute successfully
int main()
{
char c;
// using %c for character input
scanf("Enter some character: %c", &c);
KEYWORDS IN C LANGUAGE:
Example :
const int height = 100;
4. C – Variable
Type Syntax
data_type variable_name;
Variable declaration
Example: int x, y, z;
data_type variable_name = value;
Variable initialization
Example: int x = 50, y = 30;
Operators and Expressions
• Arithmetic operators
• Assignment operators
• Relational operators
• Logical operators
• Bit wise operators
• Conditional operators (ternary
operators) Increment/decrement
operators
• Special operators
Arithmetic Operator is used to
performing mathematical operations such
as addition, subtraction, multiplication,
division, modulus, etc., on the given
operands.
// C program to demonstrate syntax of binary arithmetic
operators
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// printing a and b
printf("a is %d and b is %d\n", a, b);
res = a + b; // addition
printf("a + b is %d\n", res);
res = a - b; // subtraction
printf("a - b is %d\n", res);
res = a * b; // multiplication
printf("a * b is %d\n", res);
res = a / b; // division
printf("a / b is %d\n", res);
res = a % b; // modulus
printf("a %% b is %d\n", res);
return 0;
}
Relational Operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
return 0;
}
Logical Operators
Logical operators in C are used to combine
multiple conditions/constraints. Logical
Operators returns either 0 or 1, it depends on
whether the expression result is true or false.
Reverse the
result, returns
0 if the result
is 1
Logical Operators
int main()
{
int x = 5;
int y = 3;
int main() {
int x = 5;
int y = 3;
Output: 0
Logical AND Operator
The "&&" operator tests whether two
conditions are true simultaneously. It returns
true if both conditions are true, and false
otherwise.
Logical OR Operator
The "||" operator tests whether at least one of two
conditions is true. It returns true if either of the conditions
is true, and false if both are false.
Output
Both values are greater than 0
Bitwise Operator in C
• The bitwise operators are the operators used
to perform the operations on the data at the
bit-level.
• It consists of two digits, either 0 or 1.
• It is mainly used in numerical computations
to make the calculations faster.
Bitwise AND operator
Two integer operands are written on both
sides of the (&) operator. If the corresponding
bits of both the operands are 1, then the
output of the bitwise AND operation is 1;
otherwise, the output would be 0.
Ex:
1.We have two variables a and b.
2.a =6;
3.b=4;
4.The binary representation of the above two variables
are given below:
5. a = 0110
6. b = 0100
7.When we apply the bitwise AND operation in the abo
ve two variables, i.e., a&b, the output would be:
8.Result = 0100
n the above code, we have created two variables, i.e., 'a' and 'b'. The
values of 'a' and 'b' are 6 and 14 respectively. The binary value of 'a'
and 'b' are 0110 and 1110, respectively. When we apply the AND
operator between these two variables,
a AND b = 0110 && 1110 = 0110
Bitwise OR operator
Right-shift operator
It is an operator that shifts the number of bits
to the right side.
Syntax of the right-shift operator is given
below:
1.Operand >> n;
Where,
Operand is an integer expression on which
we apply the right-shift operation.
N is the number of bits to be shifted.
In the case of the right-shift operator, 'n' bits will be
shifted on the right-side. The 'n' bits on the right-side
will be popped out, and 'n' bits on the left-side are
filled with 0.
For example,
1.Suppose we have a statement,
2.int a = 7;
3.The binary representation of the above variable wo
uld be:
4.a = 0111
5.If we want to right-
shift the above representation by 2, then the stateme
nt would be:
6.a>>2;
7.0000 0111 >> 2 = 0000 0001
#include <stdio.h>
int main()
{
int a=7; // variable initialization
printf("The value of a>>2 is : %d ", a>>2);
return 0;
}
// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
return 0;
}
Assignment Operators
Increment/decrement operators
These operators are used to either increase or
decrease the value of the variable by one.
Assignment Operators
There are 2
categories of
assignment
operators in C
language. They
are,
1.Simple
assignment
operator (
Example: = )
2.Compound
// C program to demonstrate working of Assignment operators
#include <stdio.h>
int main()
{
return 0;
}
Modulus of a, b is : 0
PROGRAM FOR ASSIGNMENT OPERATORS:
}
// for loop is used to repeat a block of code until the specified
condition is met
Loops in C is used to execute the block of code
several times according to the condition given in
the loop.
for loop
When you know exactly how many times you
want to loop through a block of code, use the for
loop.
for loop is used to repeat/iterate a block of
code(statement) until specified condition is met
Syntax
for (expression 1; expression 2; expression 3)
{ }
}
//A list is a collection of different kinds of
values or items. An element of the list can be
any data
}
Example
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 5; i++)
{
printf("%d\n", i);
}
return 0;
}
Example explained
expression 1 sets a variable before the loop
starts (int i = 0).
expression 2 defines the condition for the
loop to run (i must be less than 5). If the
condition is true, the loop will start over again,
if it is false, the loop will end.
expression 3 increases a value (i++) each
}
●Syntax:
Increment operator: var_name++;
Decrement operator: var_name –
-;
●Example:
Increment operator : i ++
; Decrement operator : i –
–;
Conditional Operator in C
return 0;
}
• In the above code, we are taking
input as the 'age' of the user.
• After taking input, we have applied
the condition by using a
conditional operator.
• In this condition, we are checking
the age of the user. If the age of
the user is greater than or equal to
18, then the statement1 will
execute, i.e., (printf("eligible for
voting")) otherwise, statement2 will
execute, i.e., (printf("not eligible
for voting"))
Data Types
●Each variable in C has an associated data
type.
●It specifies the type of data that the variable
can store like integer, character, floating,
double, etc.
●Each data type requires different amounts of
memory and has some specific operations
which can be performed over it..
●data type specifies the size and type of
information the variable will store
●data type defines the type of data we are
going to store in variable and how much
memory we require to store that data
Data Types
- float - typedef
-void - enum
-double
int main()
{
int a = 4000; // positive integer data
char c = 'Z'; // char data type
long d = 41657; // long positive integer data
short g = 130; // short +ve integer data type
short h = -130; // short -ve integer data type
double i = 4.1234567890; // double float data
Int: Short and long
Short and long are the size modifier
%u Unsigned int
%lf Double
Syntax:
char var_name;
#include <stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c \n", c);
}
#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f
\n", f);
}
double
Stores fractional numbers. Double has 8
bytes, which is equal to 64 bits in size
Syntax: double data1;
format specifier "%lf" is used to
display a floating point number with one
digit after the decimal point.
#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}
#include <stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c \n",
c);
}
//Program 3: float data type
#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f \n", f);
}
#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n",
d);
}
Data Type Modifiers
in C
In C, data type modifiers are used to modify
the length or size of data that various data
types hold such as int, char, and double as per
the requirements. By using these data type
modifiers we can precisely utilize the
computer memory.
Types of Modifiers
In C, we have 4 data type modifiers that are used to
modify data types.
1.short
2.long
3.signed
4.unsigned
#include <stdio.h>
int main()
{
int a;
return 0;
}
Output
Size of a: 4
Size of b: 2
With scanf, the "h" modifier indicates that it's reading a
short integer.So the "%hd" is necessary to write only
two bytes (on most machines) instead of the 4 bytes
that "%d" writes.
‘long’ Type Modifier in C
long data type modifier is used to increase the size
of a int or double data type by two times that also
increases the range of these data types. For example,
if int take 4 bytes of space it will take 8 bytes of
space after using long with int
return 0; Outputs:
} Size of num1: 4
size of num2: 4
unsigned’ Type Modifier in C
• unsigned int can store positive values.
• if the range of int is “-2,147,483,648 to
2,147,483,647” then after using unsigned with int
the range became “0 to 4,294,967,295”
Data Types with Unsigned
•unsigned int
•unsigned short int
•unsigned long int
return 0;
}
Output
num1: -1
num2: 4294967295
Syntax:
ch = getchar( ); //where ch is a char Var.
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character: ");
/*
Take a character as
input and store it in
variable c
*/
c = getchar();
putchar(c);
}
getchar() & putchar() functions
getchar is used for input, while putchar is used for output.
getchar()function reads a character from the terminal and returns it as an integer
function reads only a single character at a time. This method in a loop in case
want to read more than one character. +
putchar() function displays the character passed to it
on the screen and returns the same character. This function too displays
only a single character at a time. In case you want to display more than
one character, use method in a loop.
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character");
/*
Take a character as input and
store it in variable c
*/
c = getchar();
/*
display the character stored
in variable c
*/
putchar(c)
;
}
When you will compile the above code, it will ask you to enter a value. When you will enter the
value, it will display the value you have entered.
10 + 20 * 30
For example: ‘*’ and ‘/’ have same precedence and their
100 + 200 / 10 - 3 * 10
1)Associativity is only used when there are two or more operators
of same precedence.
2)All operators with the same precedence have same
associativity For example + and – have the same
associativity.
() Parentheses (function call) (see Note 1)
++ — Prefix increment/decrement
+– Unary plus/minus
* Dereference
* / % Multiplication/division/modulus left-to-right
+ – Addition/subtraction left-to-right
left-to-right
> >= Relational greater than/greater than or equal to
|| Logical OR left-to-right
1) isalnum()
.
This function checks whether character is
alphanumeric or not.
Alphanumeric characters are made up of the
26 letters of the alphabet (A through Z) and the
digits 0 through 9
2) isalpha()
This function checks whether character is
alphabet or not
3) isdigit()
This function checks whether character is
digit or not
)
4 isupper()
This function checks whether character is an
uppercase character or not.
5) islower()
This function checks whether character is a
lowercase character or not.
6)isprint()
This function checks whether character is
printable or not.
/* C example program of isalpha().*/
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
if (isalpha(ch))
printf("%c is an alphabet.\n", ch);
else
printf("%c is not an alphabet.\n", ch);
return 0;
}
• floor( ) is a library function in C defined in the
<math.h> header file.
#include<stdio.h>
#include<math.h>
int main()
{
double a = 45.98;
printf("The square root of %lf: %lf\n", a, sqrt(a));
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double num = 4.0;
double result = pow(num, 2.0);
printf("The square of %.2f is: %.2f\n",
num, result);
return 0;
}
Output
The square of 4.00 is: 16.00
tanh ( ) This function is used to
calculate hyperbolic tangent.
sinh ( ) This function is used to
calculate hyperbolic sine.
Description
Function
This function is used to print the character,
printf() string, float, integer, octal and hexadecimal
values onto the output screen
This function is used to read a character,
scanf() string, numeric data from keyboard.
gets() It reads line from keyboard
Functions Description