Module 1.Pptx
Module 1.Pptx
- industry-standard language
5
C Standardization
• 1989 ANSI and ISO -> Standard C
• 1999 C99
• 2011 C11
Later Languages
• 1979 C++ by Bjarn Stroustrup also at Bell
– Object orientation
• 1991 Java by Sun
– Partial compile to java bytecode: virtual
machine code
– Write once, run anywhere
– Memory manager – garbage collection
– Many JVMs written in C / C++
Introduction to Components of a computer System.
A computer is an electronic device that accepts data,
performs operations, displays results, and stores the
data or results as needed.
The output unit accepts the information from the CPU and
displays it in a user-readable form.
Low Level vs High level Languages
Language Processors: Assembler, Compiler and
Interpreter
1. Compiler
statements
void main()
{
printf("This is my first C program.");
getch();
}
{
Main fun.
printf("Hello"); Calling of user
display (); defined section
} Sub program
void display() Definition of sec
user defined
{ section
printf(“Hi am Sudeshna ”);
Steps in the Execution of the C Program
Source code
First c
Preprocessor
First .i
Compile
Syntax
Error
Yes
First .asm NO
Assembler
First .obj
Link with system Libraries
First .exe
Input data Execute
Logic
and
data
Error No O/p
steps of execution
of the C program
into the C compiler
Syntax of the steps of execution of C program.
Loader
Compiler
Basics of a Typical C Program Development Environment
Program is created in
the editor and stored
Editor Disk on disk.
Phases of C Programs: Preprocessor
Preprocessor Disk program
1. Edit processes the code.
Compiler creates
2. Preprocess Disk object code and
stores it on disk.
3. Compile
Linker Disk Linker links the object
4. Link code with the libraries
Primary
5. Load Memory
printf()
The printf() method, in C, prints the value passed as the parameter
to it, on the console screen.
Input:
Enter an integer: 25
Output:
Syntax:
printf("%f...", ...);
scanf("%e...", ...);
printf("%E...", ...);
Output
Using %f: 12.670000
Using %e: 1.267000e+01
Using %E, 1.267000E+01
Changing the width (continued)
Number Formatting Print as:
23 %1d 23
23 %2d 23
23 %6d ….23
23 %8d ……23
11023 %4d 11023
11023 %6d .11023
-11023 %6d -11023
-11023 %10d ….-11023
Changing The Precision
Number Formatting Prints as:
2.718281828 %8.5f `2.71828
2.718281828 %8.3f ```2.718
2.718281828 %8.2f ````2.72
2.718281828 %8.0f ````````3
2.718281828 %13.11f 2.71828182800
2.718281828 %13.12f 2.718281828000
FLOW CHARTS : SYMBOLIC
REPRESENTATION
FLOW-CHART
ADVANTAGES
• Communication
• Effective analysis
• Proper documentation
• Efficient coding
• Proper debugging
• Efficient program maintenance
Example 1:
ALGORITHM :
Definition of Algorithm
The word Algorithm means ” A set of finite
rules or instructions to be followed in
calculations or other problem-solving
operations ”
Or
C consist of
Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
1. Keywords
2. Identifiers
3. Variables
4. Constants
5. Strings
6. Special Symbols
7. Operators
Constants
•As the name suggests, a constant in C is a variable that
cannot be modified once it is declared in the program.
We can not make any change in the value of the
constant variables after they are defined.
Character constants
•A character constant is a constant which uses
single quotation around characters. For example:
'a', 'l', 'm', 'F'
String constants
•String constants are the constants which are
enclosed in a pair of double-quote marks. For
example: "good" ,"x","Earth is round\n"
Example of Constants in C
Output
In function 'main':
10:9: error: assignment of read-only variable 'var'
10 | var = 20;
| ^
Defining Constant using #define Preprocessor
We can also define a constant in C using #define preprocessor. The
constants defined using #define are macros that behave like a
constant.
These constants are not handled by the compiler, they are handled
by the preprocessor and are replaced by their value before
compilation.
SYNTAX:
Output
The value of pi: 3.14
Note: This method for defining constant is not preferred as it may introduce bugs and make the
code difficult to maintain.
Variables
• A variable is nothing but a name given to a storage area that our
programs can manipulate.
Defined_var: 0
Value of defined_var after initialization: 12
Value of ini_var: 25
Keywords
• Keywords are predefined or reserved words that have special meanings
to the compiler.
• These are part of the syntax and cannot be used as identifiers in the
program.
•
• Keywords are nothing but system defined identifiers.
• They have specific meaning in the language and cannot be used by the
programmer as variable or constant names
• 32 Keywords in C Programming
Keywords
Example:
char
char keyword in C is used to declare a character variable in the C
programming language.
char x = 'D';
Output
a
Example:
double and float
The doubles and floats are data types used to declare decimal type
variables. They are similar, but doubles have 15 decimal digits,
and floats only have 7.
Output
Float value:
0.300000
Double
value:
10.670000000
000000
Identifiers
• Identifiers are unique names that are assigned to
variables, structs, functions, and other entities.
They are used to uniquely identify the entity within
the program.
• Identifiers is nothing but a name given to these
elements
float area;
float area()
3. Braces{}: These opening and ending curly braces mark the start and end
of a block of code containing more than one executable statement.
Syntax of Integer:
We use int keyword to declare the integer variable:
int var_name;
NOTE:
2. short int: It is lesser in size than the int by 2 bytes so can only
store values from -32,768 to 32,767.
3. long int: Larger version of the int data type so can store values
greater than int.
// AS PREFIX
++m
// AS POSTFIX
m++
1. Pre-Increment
In pre-increment, the increment operator is used as the prefix. Also known
as prefix increment, the value is incremented first according to the
precedence and then the less priority operations are done.
2. Post-Increment
In post-increment, the increment operator is used as the suffix of the
operand. The increment operation is performed after all the other
operations are done. It is also known as postfix increment.
POSTFIX
Postfix:
• (a) x = a++;
First action: store value of a in memory location for
variable x.
Second action: increment value of a by 1 and store
result in memory location for variable a.
• (b) y = b––;
First action: put value of b in memory location for
variable y.
Second action: decrement value of b by 1 and put
result in memory location for variable b.
PREFIX
Prefix :
• (a) x = ++a;
First action: increment value of a by 1 and store
result in memory location for variable a.
Second action: store value of a in memory location
for variable x.
• (b) y = ––b;
First action: decrement value of b by 1 and put
result in memory location for variable b.
Second action: put value of b in memory location
for variable y.
Output:
Prefix Increment: 6
Postfix Increment: 5
Output
Prefix = 4
Postfix = 5
Increment and Decrement Operators Example:
Syntax:
a << b;
Right Shift(>>)
It is a binary operator that takes two numbers, right shifts the bits of the
first operand, and the second operand decides the number of places to
shift. In other words, right-shifting an integer “a” with an integer “b”
denoted as ‘(a>>b)‘ is equivalent to dividing a with 2^b.
Syntax:
a >> b;
C Relational Operators
•Relational operators in C are vital for making
comparisons and decisions in programming.
•A relational operator checks the relationship between
two operands.
•If the relation is true, it returns 1; if the relation is
false, it returns value 0.
2. Logical OR Operator ( || )
The condition becomes true if any one of them is
non-zero. Otherwise, it returns false i.e., 0 as the value.
logical NOT
Syntax logical AND
(operand_1 && operand_2)
Output
Both values are
greater than 0
Syntax logical OR
(operand_1 || operand_2)
Output
Any one of the given value is
greater than 0
Syntax logical NOT
!(operand_1 && operand_2)
OUTPUT:
Both values are less than
0
HW Example
Output
False True
The && (logical AND) operator indicates
whether both operands are true.
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Working of Conditional/Ternary Operator in C
The working of the conditional operator in C is as follows:
● Step 1: Expression 1 is the condition to be evaluated.
● Step 2A: If the condition(Expression 1) is True then
Expression 2 will be executed.
● Step 2B: If the condition(Expression 1) is false then
Expression 3 will be executed.
● Step 3: Results will be returned.
Flowchart of Conditional/Ternary Operator in C
Example 1: C Program to Store the greatest of the two
Numbers using the ternary operator
Output
m is greater than n that is 5 > 4
Example 2: C Program to check whether a year is a leap year
using ternary operator
Output
The year 1900 is not a leap year
LOgic ::
Given a year, check if it is a leap year or not.
For this reason, the following years are not leap years:
This is because they are evenly divisible by 100 but not by 400.
This is because they are evenly divisible by both 100 and 400.
Generally used when we need a short
conditional code such as assigning value to
a variable based on the condition.
Output
8
3. To find out the number of elements in an array:
Sizeof can be used to calculate the number of
elements of the array automatically.
Output
Number of elements:11
Operator Precedence and Associativity in C
100 + 200 / 10 – 3 * 10 = 90
HW
Output:
10
But among first pair, which function (the operand) evaluated first is
not defined by the standard.
Library Functions
In C language, a built-in function is a pre-defined operation that you
can use in your code without having to write the code for that
operation from scratch.
They save time and effort by allowing you to reuse common operations.
These functions are the built-in functions i.e., they are predefined in
the library of the C.
Some of the library functions are printf, scanf, sqrt, etc. To use these
functions in the program the user has to use a header file associated
with the corresponding function in the program.
math.h: This header file contains mathematical functions. It contains functions
such as sqrt, pow, exp, log, sin, etc.
sqrt(): This function takes a double argument and returns the square root. It can
be mathematically expressed as ✔a Prototype
Prototype: double sqrt (double a)
Example:
#include <stdio.h>
#include<math.h>
int main()
{
double a = 25.5637;
printf("%lf",sqrt(a));
return 0;
}
Output: 5.056056
pow(): This function takes two arguments, the base and the exponent. It returns
the value of the base raised to the exponent.
double pow(double a, double b)
Example:
#include <stdio.h>
#include<math.h>
int main() {
double a = 4.0;
double b = 3.0;
double c = pow(a,b);
printf("%.1lf",c);
return 0;
}