0% found this document useful (0 votes)
22 views58 pages

1st Unit

The document provides an introduction to program planning and C programming, covering fundamental concepts such as algorithms, flowcharts, and the C programming language's history and importance. It explains key elements of C, including tokens, keywords, constants, and data types, along with examples of basic C programs. Additionally, it discusses the advantages of using constants and the significance of variable declarations and storage classes in C.

Uploaded by

nayna sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views58 pages

1st Unit

The document provides an introduction to program planning and C programming, covering fundamental concepts such as algorithms, flowcharts, and the C programming language's history and importance. It explains key elements of C, including tokens, keywords, constants, and data types, along with examples of basic C programs. Additionally, it discusses the advantages of using constants and the significance of variable declarations and storage classes in C.

Uploaded by

nayna sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

01/20/2025

UNIT –I Introduction to Program Planning & C Programming

• Overview of C: History and importance C

Fundamentals of Programming languages


• Algorithm :- ” A set of finite rules or instructions to be followed in
calculations or other problem-solving operations. ”
Therefore Algorithm refers to a sequence of finite steps to solve a
particular problem.
A set of steps that generates a finite sequence of elementary
computational operations leading to the solution of a given problem
is called an algorithm.

1
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

Fundamentals of Programming languages


Example of algorithm for addition of two number

Step 1. INPUT TO A, B

Step 2. S ← A+B --(Store the sum of the values in A and B in S)

Step 3. PRINT S --(Show the sum obtained in Step 2)

Step 4. STOP

2
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

Fundamentals of Programming languages


• Flowchart :- A flowchart is a blueprint that pictorially represents the algorithm and its steps.

• Formally speaking, a flowchart is a diagrammatic representation of the steps of an algorithm.

• In a flowchart, boxes of different shapes are used to denote different types of operations.
These boxes are then connected by lines with arrows denoting the flow or direction to which
one should proceed to know the next step.

3
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

Example of flowchart

Fundamentals of Programming languages


4
01/20/2025
C PROGRAM FOR ADDITION OF TWO NUMBER

# include<stdio.h>

Fundamentals of Programming languages


#include<math.h>

Int main ()
{
int a , b , c ;
a = 5;
b = 7;
c = a + b;
printf(“%d”, c )
return 0;
5

}
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Overview of C: History and importance C

Fundamentals of Programming languages


• Overview of C:
• It is a general-purpose, procedure-oriented programming language.

• It is both machine-independent and structured. C is a high-level programming language


developed by Dennis Ritchie in the early 1970s.

• It is now one of the most popular and influential programming languages worldwide.

6
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• History C

Fundamentals of Programming languages


7
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Importance of C :

Fundamentals of Programming languages


• C allows for direct memory manipulation and low-level
Efficiency access to system resources

• C code can be easily ported to different platforms & wide


Portability availability of compilers and libraries.

• C is known for its fast execution speed, making it suitable


Speed for developing performance-critical applications

• C gives programmers fine-grained control over memory


Control management and system resources

8
Compatibility • C code can be easily integrated with code written in other
languages like C++, Java, and Python
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Character Set :

Fundamentals of Programming languages


character set refers to a set of all the valid characters that we can use in the source program
for forming words, expressions, and numbers

9
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

Tokens in C :

Fundamentals of Programming languages


It is a smallest individual element of the C programming language that is meaningful to the compiler.

10
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Keywords :

Fundamentals of Programming languages


These are words that have special meaning to the C compiler.

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 11

volatile while
01/20/2025
• // Example of the if-keyword token in C
• #include <stdio.h>

Fundamentals of Programming languages


int main() {

int num = 10;
• if (num > 0) {
• printf("Positive number\n");}

return 0;
• }

12
01/20/2025
• / Example of identifier tokens in C

Fundamentals of Programming languages



#include <stdio.h>

int main() {
• int age = 25;
• float salary = 5000.50;

printf("Age: %d\n", age);
• printf("Salary: %.2f\n", salary);

return 0; 13
• }
01/20/2025
• // Example of operators in C
int a = 5, b = 10; //simple assignment operator
int sum = a + b; // addition operator

Fundamentals of Programming languages


int product = a * b; // operator for multiplication
int isGreater = a > b; // greater than relational operator
a += 1; // compound assignment operator

14
• Types Of Operator Tokens In C

01/20/2025
• The operator tokens in C can be classified into two types- unary operators and binary operators, depending on
the number of operands they take.
1.Unary Operator Tokesn In C
• A unary operator is an operator that operates on a single operand or variable in an expression. It performs an
operation or transformation on the value of the operand and typically returns a new value. The unary operator

Fundamentals of Programming languages


tokens in C include negation (-), increment (++), decrement (--), logical NOT (!), and bitwise one's complement
(~).

Example// Unary negation operator



#include <stdio.h>

int main() {

int x = 5;
• int y = -x;
• printf("y: %d\n", y); // Output: y: -5
• return 0; 15
• }
• Output:
• y: -5
01/20/2025
• Binary Operators
• A binary operator operates on two operands or variables in an expression. It takes two
values, performs an operation on them, and returns a result. Binary operators are used to
perform arithmetic, logical, and comparison operations.

Fundamentals of Programming languages


• Examples of binary operators include addition (+), subtraction (-), multiplication (*), division
(/), and equality (==). Binary operators are commonly used in programming to perform
calculations or compare two values. The C code example below illustrates the use of the
addition binary operator token.
Example:
• #include <stdio.h>
• int main() {
• int a = 5;
• int b = 3;
• int result = a + b; // Using the binary addition operator (+)
• printf("The result of %d + %d is %d\n", a, b, result);
• return 0; 16
• }
• The result of 5 + 3 is 8
01/20/2025
• Program Demonstarting C Tokens(Keywords,Identifiers,Constants,Operators)
• #include<stdio.h>
• Int main(){

Fundamentals of Programming languages


• //Keywords : int ,return
• //Identifiers: main,num,result
• //Operators:=,+,*
• Int num=10://Declaration and assignment
• Const int multiplier=2;//Constant
• int result;
• result= num*multiplier;//Assignment and arithmetic operation
• printf(“The result is %d\n”,result);
• Return 0;
• } 17
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Identifiers :

Fundamentals of Programming languages


These are unique names that are assigned to variables, structs,
functions, and other entities.
Sr.
Identifier Keyword
No
1 An identifier is a unique name created Keywords are restricted words with a definite,
by the programmer to define a predetermined meaning. Any programme
variable, structure, class, or function. statement may be defined with the aid of
keywords.

2 The initial character in the identification can be


A keyword always starts in lowercase. capitalized, lowercase, or start with an
underscore.

3 It can only have alphabetical It can have numbers, alphabetical characters,


characters. and underscores.

4 Examples of keywords are double, int, Examples of IDs are test, count1, high speed,
18
auto, char, break, and many others. etc.
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Constants :

Fundamentals of Programming languages


It is a value that remains constant throughout the program and
cannot be changed during program execution..

Integer
Real
Primary
Character
Constants

Array
Pointer
Secondary
Structure
19
Union
Enum , etc
C constants
• C Constants is a most fundamental and essential part of C programming language.
Constants in C are the fixed values that are used in a program, and its value remains
the same during the entire execution of the program.
• Constants are also called literals.
• Constants can be any of the data types.
• A constant is a value or variable that can't be changed in the program, for example:
10, 20, 'a', 3.4, "c programming" etc.
C constants
• Constants can be of any of the basic data types like an integer constant, a floating
constant, a character constant, or a string literal. There are enumeration constants
as well.
• Syntax
• const type constant_name;
• const keyword defines a constant in C.
Fundamentals of Programming languages 01/20/2025
22
Example of constant
• #include<stdio.h>
• int main()
• {
• const float PI=3.14;
• printf("The value of PI is: %f",PI);
• return 0;
• }
Cont…
• If you try to change the value of PI, it will render compile time error.
• #include<stdio.h>
• int main(){
• const float PI=3.14;
• PI=4.5;
• printf("The value of PI is: %f",PI);
• return 0;
• }
Integer constant
• It's referring to a sequence of digits. Integers are of three types:
• Decimal Integer
• Octal Integer
• Hexadecimal Integer
• Example:
• 15, -265, 0, 99818, +25, 045, 0X6
• Real constant
Real constant
• The numbers containing fractional parts like 99.25 are called real or floating points constant.

• Single character constant


• It simply contains a single character enclosed within ' and ' (a pair of single quote). It is to be
noted that the character '8' is not the same as 8. Character constants have a specific set of
integer values known as ASCII values (American Standard Code for Information Interchange).

• Example:
• 'X', '5', ';'
String constant
• These are a sequence of characters enclosed in double quotes, and they may include
letters, digits, special characters, and blank spaces. It is again to be noted that "G"
and 'G' are different - because "G" represents a string as it is enclosed within a pair of
double quotes whereas 'G' represents a single character.
• Example:
• "Hello!", "2015", "2+1"
Backslash character constant
• C supports some character constants having a backslash in front of it. The lists of
backslash characters have a specific meaning which is known to the compiler. They
are also termed as "Escape Sequence".
• For Example:
• \t is used to give a tab
• \n is used to give a new line
• // C program to illustrate constant variable definition

01/20/2025
• #include <stdio.h>
• int main()
• { // defining integer constant using const keyword

Fundamentals of Programming languages


• const int int_const = 25;
• // defining character constant using const keyword
• const char char_const = 'A'; /
• / defining float constant using const keyword
• const float float_const = 15.66;
• printf("Printing value of Integer Constant: %d\n", int_const);
• printf("Printing value of Character Constant: %c\n", char_const);
• printf("Printing value of Float Constant: %f", float_const);
• return 0;
• }
• Output-Printing value of Integer Constant: 25 29
• Printing value of Character Constant: A
• Printing value of Float Constant: 15.660000
01/20/2025
• Properties of Constant in C
• The important properties of constant variables in C defined using the const keyword
are as follows:
• 1. Initialization with Declaration

Fundamentals of Programming languages


• We can only initialize the constant variable in C at the time of its declaration.
Otherwise, it will store the garbage value.
• 2. Immutability
• The constant variables in c are immutable after its definition, i.e., they can be
initialized only once in the whole program. After that, we cannot modify the value
stored inside that variable.

30
01/20/2025
• Advantages of Using Constants in C
• Using constants in C has several advantages over using variables. Some of the
advantages are:

Fundamentals of Programming languages


• Readability: The constants in C make the code more readable(since their values
cannot be changed) and self-explanatory.
• Maintenance: The usage of the constants in C makes the code more maintainable.
When a constant is used at multiple places, changing its value at one place will
change its value at all other places.
• Safety: Constants provide safety to the code. When a variable is used in multiple
places in the code, it is possible to accidentally modify the value of the variable. This
can cause unexpected behavior in the code. However, when a constant is used, the
value cannot be modified, providing safety to the code.
• Optimization: It helps in the optimization of the code. When we used the Constants
in C, it replaces the constant with the values. This helps in reducing the overhead of
accessing the values. 31
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Variables :

Fundamentals of Programming languages


It is the name associated with some memory location to store data
of different types.

C Variable Syntax
Data type variable name = value;
or
data type variable_name1, variable_name2;
Example
Int var; // integer variable
char a; // character variable
float fff; // float variables

32
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Data types :

Fundamentals of Programming languages


• It specifies the type of data that the variable can store like integer, character, floating,
double, etc.

33
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Declaration of variables:

Fundamentals of Programming languages


• The main purpose of variable declaration is to store the required data in the memory
location in the form of variables so that we can use them in our program to perform any
operation or task.

• E xample:

34
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Storage Classes in C

Fundamentals of Programming languages


• C Storage Classes are used to describe the features of a variable/function.

• These features basically include the scope, visibility, and lifetime which help us to trace the existence
of a particular variable during the runtime of a program.

35
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Assigning Values to variables:

Fundamentals of Programming languages


 “Assigning a value to a variable” means writing a value to the variable.

This process involves four entities:


• A data type
• A variable
• The simple assignment operator (=)
• The value that will be copied to the variable.

36
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Assigning Values to variables:

Fundamentals of Programming languages


“ int a = 4 ; ”

• Here:
• “ int ” is the data type.
• “ a ” is the variable.
• “ = " is the operator.
• “ 4 ” is the value.
37
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Defining Symbolic Constants:

Fundamentals of Programming languages


• Labels/names that represent fixed values that never change during the course of a
program.

• Syntax for Creating Symbolic Constants


#define symbolic_constant_name value_of_the_constant

• Symbolic Constants Examples

38
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• declaring a Variable as Constant:

Fundamentals of Programming languages


• To create a constant in C, use the const keyword followed by a data type and a variable
name.

• example:
const int MAX_VALUE = 100;

This will create a constant variable named MAX_VALUE with a value of 100.

39
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

• Comparison between constants and variables :

Fundamentals of Programming languages


Sr.
Constants Variables
No
1 A constant does not change its A variable, on the other hand, changes
value as the equation is solved. its value depending on the equation.

2 Constants are usually written in Variables are written as letters or


numbers(whether fractions, symbols.
integers, decimals or real
numbers).

3 Constants usually represent the Variables, on the other hand,


known values in an equation or represent unknown values.
expression.

4 40
Constants have fixed face Variables do not have a fixed face
values. value..
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

Fundamentals of Programming languages


Declaring a Variable as Volatile:

• When a variable is declared as volatile, it indicates to the compiler


that the variable’s value may change at any time without any
action being taken by the code the compiler finds nearby.

• The syntax for using the volatile keyword in C is as follows:

volatile data_type variable_name;

41
01/20/2025
UNIT –I Introduction to Program Planning & C Programming

Example of Declaring a Variable as Volatile:

Fundamentals of Programming languages


42
• Escape Sequence in C

01/20/2025
• Escape sequence is a character followed by a blackslash(\)
• They are used especially to perform some special operations like going to new
line,providing a horizontal tab,vertical tab etc….

Fundamentals of Programming languages


43
01/20/2025
• // C program to illustrate \a escape sequence
• #include <stdio.h>

Fundamentals of Programming languages


• int main(void)
• {
• // output may depend upon the compiler
• printf("My mobile number " "is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");
• return (0);
• }

Output-My mobile number is 7873923408

44
01/20/2025
• Example to demonstrate how to use \b escape sequence in C

• // C program to illustrate \b escape sequence

Fundamentals of Programming languages


• #include <stdio.h>
• int main(void)
• {
• // \b - backspace character transfers
• // the cursor one character back with
• // or without deleting on different
• // compilers.
• printf("Hello \b\b\b\b\b\bHi Geeks");
• return (0);
• }

45
Output
• Hello Hi Geeks
01/20/2025
Errors in Program
• There are 5 types of error in C:

Fundamentals of Programming languages


• Syntax Errors
• Runtime Errors
• Logical Errors
• Linked Errors
• Semantic Errors

46
1. Syntax Errors

01/20/2025
• These are also referred to as compile-time errors. These errors have occurred when the rule of C
writing techniques or syntaxes has been broken. These types of errors are typically flagged by the
compiler prior to compilation.

Fundamentals of Programming languages


• Example 1: In the below program we are getting an error because of a missing semicolon at the
end of the output statement (printf()) called syntax error

• / C program to demonstrate
• // a syntax error due to
• // missing semi colon
• #include <stdio.h>

• // Driver code
• int main()
• {
• // missing semicolon
47
• printf("Geeks for geeks!")
• return 0;
• }
Fundamentals of Programming languages 01/20/2025
48
Semantic Errors

01/20/2025
• When a sentence is syntactically correct but has no meaning, semantic errors occur.
This is similar to grammatical errors. If an expression is entered on the left side of the
assignment operator, a semantic error may occur.

Fundamentals of Programming languages


Example: Below is the C program to show semantic error.

// C program to demonstrate
• // a semantic error
• #include <stdio.h>

• // Driver code
• int main()
• {
• int x = 10;
• b = 20, c;
• x + y = c; 49
• printf("%d", c);
• return 0;
• }
Fundamentals of Programming languages 01/20/2025
50
Structure of a C Program

Fundamentals of Programming languages 01/20/2025


51
Enumeration (or enum) in C

01/20/2025
• Enumeration (or enum) is a user defined data type in C. It is mainly used to assign
names to integral constants, the names make a program easy to read and maintain.

Fundamentals of Programming languages


52
01/20/2025
// An example program to demonstrate working
// of enum in C
#include<stdio.h>

Fundamentals of Programming languages


enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}

Output:
53
2
01/20/2025
/ Another example program to demonstrate working
// of enum in C
#include<stdio.h>

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,

Fundamentals of Programming languages


Aug, Sep, Oct, Nov, Dec};

int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);

return 0;
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11 54
01/20/2025
Integrated Development Environment(IDE):

• An integrated development environment (IDE) is a

Fundamentals of Programming languages


software application that helps programmers develop
software code efficiently. It increases developer
productivity by combining capabilities such as software
editing, building, testing, and packaging in an easy-to-use
application.

55
01/20/2025
This IDE Contains 5
Tools:Editor,Preprocessor,Compiler,Linker and Loader

Fundamentals of Programming languages


1)Editor:It is udes to type and edit the program
2)Preprocessor:It is responsible for performing certain operations
before the compiling ,like including header files,declaring
constants ,etc.It always begins with the ‘#’ Sign.
For eg.
#include<stdio.h>
#define pi 3.14;//#define is a preprocessor directive that is used to define macros.
The macros are the identifiers defined by #define which are replaced by their value
before compilation. We can define constants and functions like macros using #define . 56
01/20/2025
• 3)Compiler:it is used to convert the c program to machine language.As
the processor cannot understand the language of C,We need to convert it into
a machine understandable language.i.e machine language.

Fundamentals of Programming languages


• 4)Linker:It is responsible for linking the different functions in a program
• 5)Loader:It loads the binary file from the memory and gets is executed by
the processor.

57
Fundamentals of Programming languages 01/20/2025
58

You might also like