0% found this document useful (0 votes)
73 views

Unit - 2 PPS

The document discusses key features and concepts related to the C programming language including: - C is a simple, efficient, fast, portable language with function-rich libraries and support for dynamic memory management and pointers. - A C program structure includes documentation, link, definition, global declaration, main function, and sub-program sections. - Comments are used to explain code and come in single-line and multi-line formats. - Header files contain standard library functions included using #include. - Languages can be high-level, assembly-level, or machine-level. Flowcharts and algorithms are used to design programs and solve problems through logical steps.

Uploaded by

Jigar Dalwadi
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)
73 views

Unit - 2 PPS

The document discusses key features and concepts related to the C programming language including: - C is a simple, efficient, fast, portable language with function-rich libraries and support for dynamic memory management and pointers. - A C program structure includes documentation, link, definition, global declaration, main function, and sub-program sections. - Comments are used to explain code and come in single-line and multi-line formats. - Header files contain standard library functions included using #include. - Languages can be high-level, assembly-level, or machine-level. Flowcharts and algorithms are used to design programs and solve problems through logical steps.

Uploaded by

Jigar Dalwadi
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/ 16

PPS – 3110003 Prepared By: Prof.

Jigar Dalvadi

UNIT-2 - Fundamentals of C
Features of C language
Simple and Efficient

 The basic syntax style of implementing C language is very simple and easy to learn.

Fast

 C is a statically typed programming language. Unlike Java and Python, which are
interpreter-based, C is a compiler-based program. This makes the compilation and
execution of codes faster.

Portability

 Another feature of the C language is portability. To put it simply, C programs are


machine-independent which means that you can run the fraction of a code created in C on
various machines with none or some machine-specific changes.

Function-Rich Libraries

 C comes with an extensive set of libraries with several built-in functions that make the
life of a programmer easy. Even a beginner can easily code using these built-in functions.

Dynamic Memory Management

 One of the most significant features of C language is its support for dynamic memory
management (DMA). It means that you can utilize and manage the size of the data
structure in C during runtime.

Pointers

 With the use of pointers in C, you can directly interact with memory. As the name
suggests, pointers point to a specific location in the memory and interact directly with it.

Middle-Level Language

 The C programming language is also referred to as a middle-level language. That‘s


because it offers low-level access to the computer hardware

1|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Structure of C Program

Documentation Section

 The documentation section is the part of the program where the programmer gives the
details associated with the program. He usually gives the name of the program

Link Section

 This part of the code is used to declare all the header files that will be used in the
program.
 This leads to the compiler being told to link the header files to the system libraries.

Example

#include<stdio.h>

Definition Section

 In this section, we define different constants. The keyword define is used in this part.

#define PI=3.14

Global Declaration Section

 This part of the code is the part where the global variables are declared. All the global
variable used are declared in this part.
 The user-defined functions are also declared in this part of the code.

Main Function Section

 Every C-programs needs to have the main function. Each main function contains 2 parts.
2|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

 A declaration part and an Execution part. The declaration part is the part where all the
variables are declared.
 The execution part begins with the curly brackets and ends with the curly close bracket.
Both the declaration and execution part are inside the curly braces.

Sub Program Section

 All the user-defined functions are defined in this section of the program.

The two most important types of software are:

Comments
 In programming, comments are hints that a programmer can add to make their code
easier to read and understand.

Types of Comments

There are two ways to add comments in C:

1. // - Single Line Comment


2. /*...*/ - Multi-line Comment

1. Single-line Comments in C

 In C, a single line comment starts with //. It starts and ends in the same line. For example,

#include <stdio.h>
void main()
{
// create integer variable
int age = 25;

// print the age variable


printf("Age: %d", age);

getch();
}

2. Multi-line Comments in C

 In C programming, there is another type of comment that allows us to comment on


multiple lines at once, they are multi-line comments.
 To write multi-line comments, we use the /*....*/ symbol. For example,

3|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

/* This program takes age input from the user


It stores it in the age variable
And, print the value using printf() */
#include <stdio.h>
Void main()
{
int age;
printf("Enter the age: ");
scanf("%d", &age);
printf("Age = %d", age);
getch();
}

Header files
 In C language, header files contain the set of predefined standard library functions.
 The ―#include‖ preprocessing directive is used to include the header files with ―.h‖
extension in the program.

Sr.No. Header Files & Description

1 stdio.h : - Input/Output functions

2 conio.h : - Console Input/Output functions

3 stdlib.h : - General utility functions

4 math.h : - Mathematics functions

5 string.h : - String functions

6 ctype.h : - Character handling functions

7 time.h : - Date and time functions

8 float.h : - Limits of float types

9 limits.h : - Size of basic types

10 wctype.h : - Functions to determine the type contained in wide character data.

4|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Concepts of Machine level, Assembly level and high level


programming
Low-Level Language

 Low-level languages are referred to as 'low' because they are very close to how different
hardware elements of a computer actually communicate with each other.
 Low-level languages are machine oriented and require extensive knowledge of computer
hardware and its configuration.
 There are two categories of low-level languages: machine language and assembly
language.

Machine language

 Machine language, or machine code, is the only language that is directly understood by
the computer, and it does not need to be translated.

Assembly language

 An assembly language consists of a set of symbols and letters.


 A translator is required to translate the assembly language to machine language. This
translator program is called the 'assembler.'

High-Level Languages

 A high-level language is a programming language that uses English and mathematical


symbols, like +, -, % and many others, in its instructions.
 When using the term 'programming languages,' most people are actually referring to
high-level languages.

Flowcharts and Algorithms


Definition of Flowchart
 A flowchart is the graphical or pictorial representation of a process or a program with the
help of different symbols, shapes, and arrows to demonstrate.
 The main purpose of using a flowchart is to analyze different methods

Several standard symbols are applied in a flowchart:

5|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Advantages of flowchart

 Proper debugging
 Effective analysis
 Efficient coding
 Proper documentation
 Efficient program maintenance

Disadvantages of flowchart

 Time-consuming
 Complex
 Difficult to modify
 It has no standard

Definition of Algorithm
 Writing a logical step-by-step method to solve the problem is called the algorithm
 In other words, an algorithm is a procedure for solving problems. In order to solve a
mathematical or computer problem, this is the first step in the process.
 An algorithm includes calculations, reasoning, and data processing. Algorithms can be
presented by natural languages, pseudo code, and flowcharts, etc.

Advantages of algorithm

 Algorithms are easy to write.

6|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

 There is a use of human-readable techniques to understand logic.


 For big problems, algorithms can be written with moderate steps.

Disadvantages of algorithm

 Algorithms are difficult to debug.


 It is hard to show branches and loops using an algorithm.
 Jumping (or go to statements) makes the algorithm hard to trace the problems.

Hello World Program Flow Chart

Step 1. BEGIN
Step 2. PRINT "Hello World"
Step 3. END

Algorithm and Flowchart for Addition of two numbers

7|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Step 1: Start
Step 2: Read values for Number1, Number2.
Step 3: Add Number1, Number2 and assign the result to a variable Sum.
Step 4: Display Sum
Step 5: Stop

Flowchart to check Odd or Even Number.

Step 1: Start
Step 2: Read a number to Number
Step 3: Perform the Number modulo by 2
Step 4: If it is equal to 0 then go to Step 6 else go to step 5
Step 5: Print ―Number is odd‖ go to step 7
Step 6: Print ―Number is even‖
Step 7: Stop

Draw a flow chart and write an algorithm to find out whether a given number is zero or
positive or negative.

Algorithm:

Step 1. Start
Step 2. Print‖Enter a number‖.
Step 3. Read n.
Step 4. If n>0 then print ―The number is positive‖
8|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Else if n<0 print ―The number is negative‖


Else ―The number is zero‖.
Step 5. Stop

Algorithm and flowchart to find greatest of two numbers

Step 1: Start
Step 2: Read a, b .
Step 3: If a>b then Display ―a is the largest number‖ and goto step 5. Otherwise goto step 4
Step 4: Display ―b is the largest number‖..
Step 5: Stop.

9|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Algorithm and flowchart to find greatest of three numbers

Step 1. Start
Step 2. Input A,B,C
Step 3. If (A>B) then goto step 4 otherwise goto step 5.
Step 4. If (A>C) then Print A otherwise goto step 7 .
Step 5: If (B>C) then Print B otherwise goto step 7.
Step 6: Print C
Step 7. Stop

10 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Flowchart for reverse of a number

Types of Data Types in C

 In C programming, data types are declarations for variables. This determines the type and
size of data associated with variables.
 The C programming language has five primitive or primary data types.
1. Integer (int): Refers to positive and negative whole numbers (without decimal), such
as 10, 12, 65, 3400, etc.

2. Character (char): Refers to all the ASCII character sets within single quotes such as
‗a‘, ‗A‘, etc.

3. Floating-point (float): Refers to all the real number values or decimal points, such as
3.14, 10.09, 5.34, etc.

4. Double (double): Used when the range exceeds the numeric values that do not come
under either floating-point or integer data type.

11 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

5. void
void is an incomplete type. It means "nothing" or "no type". You can think of void as
absent. For example, if a function is not returning anything, its return type should be
void.

 commonly used types in C programming for quick access are as follow

Size of Data Types In C

Type Size (bytes) Format Value range


Specifier
int at least 2, usually 4 %d, %i -32,768 to 32,767
char 1 %c -128 to 127
float 4 %f 1.2E-38 to 3.4E+38
(6 decimal places)
double 8 %lf 2.3E-308 to 1.7E+308
(15 decimal places)
short int 2 usually %hd -32,768 to 32,767
unsigned int at least 2, usually 4 %u 0 to 65,535
long int at least 4, usually 8 %ld, %li -2,147,483,648 to
2,147,483,647
unsigned long int at least 4 %lu 0 to 4,294,967,295
unsigned long long at least 8 %llu
int
signed char 1 %c -128 to 127
unsigned char 1 %c 0 to 255
long double at least 10, usually 12 %Lf 3.4E-4932 to 1.1E+4932
or 16 (19 decimal places)

12 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

 In C, there are various types of operators available to perform various operations.


Following are the list of operators present in C

1. Arithmetic Operators in C

 The purpose of this operator is to perform mathematical operations like addition,


subtraction, division, multiplication etc.
Operator What it does Example
+ Addition between 2 operands. A + B = 10
− Subtraction between 2 operands. A−B=0
* Multiplication between 2 operands. A * B = 25
/ Division between 2 operands. B/A=1
% Modulus Operator and remainder of after an integer division. B%A=0
++ Increases the integer value by one. A++ = 6
— Decreases the integer value by one. B– = 4

2. Increment Operator in C

 Mainly used for incrementing the value of an integer. It is represented by the ‗++‘
operator. ++x will increase the value of the variable x instantly. But if it is placed after
the variable then it gets increased before the execution of the next statement.

3. Decrement Operator in C

 Mainly used for decrementing the value of an integer. It is represented by the ‗--‘
operator. --x will decrease the value of the variable x instantly. But if it is placed after the
variable then it gets decreased before the execution of the next statement.

4. Assignment Operators in C

 The purpose of this operator is to assign value to a variable. The most used assignment
operator is ―=‖.

Operator Example
= a=b or b=a
+= a += b or a = a+b
-= a -=b or a = a-b
*= a *= b or a = a*b
/= a /= b or a = a/b
%= a %= b or a = a%b

13 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

5. Relational Operators in C

 Mainly used for checking relationships between operands. With the help of this operator,
you can check whether one operand is equal to or greater than the other operand or not.

 It returns 1 when the relation is true. And when it is false, it returns 0.

Operator What it does Example


== Equal to 5==5 will be 1
> Greater than 5>6 will be 0
< Less than 6<7 will be 1
>= Greater than 2 >= 1 will be 1
equal to
<= Less than equal to 1 <= 2 will be 1
!= Not equal to 5 != 6 will be 1

6. Logical Operators in C

 In the C programming language, Logical operators are mostly used for decision
making. A logical operator returns either 0 or 1 whether the condition is true or false.

Operator What it does


&& (Logical AND) True only if all conditions satisfy.
|| (Logical OR) True only if either one condition satisfies.
! (Logical Not) True only if the operand is 0.

7. Conditional Operator in C

 Also known as Ternary operator. The main purpose of conditional operators is in


decision making statements. It is similar to an if-else statement.

Syntax of a conditional operator:-

Expression1? statement1: statement2;

 Expression1 is a boolean expression, it can be either true or false.


 If the Expression1 returns true then statement1 will get executed.
 If the Expression1 returns false then statement2 will get executed.

8. Bitwise Operator in C
 C also provides special operators for bit operation between two variables.

Operator Also known as


<< Binary Left Shift Operator
>> Binary Right Shift Operator

14 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

~ Binary Ones Complement Operator


& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator

9. Misc operators in C
 It includes operators like sizeof(), reference operator(&), pointer operator(*), condition
operator(?).

Operator Example What it does


sizeof sizeof(a) Returns the size occupied by the data type of the operand.
& &a Refers to the memory address where operand is stored.
* *a A pointer.

Expression Evaluation in C

 An expression in C is defined as 2 or more operands are connected by one operator and


which can also be said to a formula to perform any operation.
 An operand is a function reference, an array element, a variable, or any constant. An
operator is symbols like ―+‖, ―-―, ―/‖, ―*‖ etc.
 Expression evaluation is nothing but operator precedence and associativity.
 Expression precedence in C tells you which operator is performed first, next, and so on in
an expression with more than one operator with different precedence.
 This plays a crucial role while we are performing day to day arithmetic operations.
 If we get 2 same precedences appear in an expression, then it is said to be
―Associativity‖.
 Now in this case we can calculate this statement either from Left to right or right to left
because this both are having the same precedence.

Types of Expression Evaluation in C


In C there are 4 types of expressions evaluations
 Arithmetic expressions evaluation
 Relational expressions evaluation
 Logical expressions evaluation
 Conditional expressions evaluation

15 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

16 | P a g e

You might also like