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

PASP Unit 1_2

The document provides an introduction to programming and problem-solving, focusing on the C programming language. It covers programming basics, types of programming languages, the structure of C programs, compilation stages, types of errors, and the use of header files. Additionally, it explains how to create custom header files and includes examples of standard library functions.

Uploaded by

asmit.bhorade24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PASP Unit 1_2

The document provides an introduction to programming and problem-solving, focusing on the C programming language. It covers programming basics, types of programming languages, the structure of C programs, compilation stages, types of errors, and the use of header files. Additionally, it explains how to create custom header files and includes examples of standard library functions.

Uploaded by

asmit.bhorade24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Unit-1_2

Introduction to Problem Solving and Programming

1
Outline
• Programing Basics
• Interface of user Program to hardware
• Programming and its Compilation Stages
• Type of Errors in programming
• C Program Structure
• Header Files in C

2
❖ Programming
•A program is a sequence of instructions that specifies how to perform a computation and
written in a language the computer understands.
•Programming refers to a technological process for telling a computer which tasks to perform
in order to solve problems.
•A programming language is a set of instructions written by a programmer to deliver
instructions to the computer to perform and accomplish a task.

3
Types of Programming Languages
After testing your algorithm, you can code it in any programming language.
• Machine Language – A collection of binary numbers
– Not standardized. There is a different machine language for every processor family.

• Assembly Language - mnemonic codes that corresponds to machine language instructions.


– Low level: Very close to the actual machine language.

• High-level Languages - Combine algebraic expressions and symbols from English


– High Level : Very far away from the actual machine language

– For example: Fortran, Cobol, C, Pascal, C#, Perl, Java.

• Easy to write, easy to understand


• Can not be executed directly
• Complier is used to convert high-level language into the target computer’s machine language

4
Interface of user Program
Operating System - Provides an interface with the
user
– unix, windows, ...

Software Tools
– word processors (MS Word, WordPerfect, ...)
– spreadsheet programs (Excel, Lotus1-2-3, ...)
– mathematical computation tools (MATLAB,
Mathematica, ...)

Computer Languages
– machine language
– binary language
– assembly language
– high level languages (C, C++, Ada, Fortran,
Basic, java etc.)
Abstractions

5
What is C?
• General purpose, machine-independent, high-level programming language
• Developed at Bell Labs in 1972 by Dennis Ritchie
• American National Standards Institute (ANSI) approved ANSI C standard in 1989

Source file

6
Entering, Translating, and Running
a High-Level Language Program

7
Example of Computer Languages
char name[40];
C Source Code: printf("Please enter your name\n");
scanf("%s", name);
printf("Hello %s", name);

push offset string "Please enter your name\n" (41364Ch)


call dword ptr [__imp__printf (415194h)]
add esp,4
lea eax,[name]
push eax
Assembly Code: push offset string "%s" (413648h)
call dword ptr [__imp__scanf (41519Ch)]
add esp,8
lea eax,[name]
push eax
push offset string "Hello %s" (41363Ch)
call dword ptr [__imp__printf (415194h)]
add esp,8

Machine Code: 68 4C 36 41 00 FF 15 94 51 41 00 83 C4 04 8D 45 D8 50 68 48 36 41 00 FF 15 9C 51 41
00 83 C4 08 8D 45 D8 50 68 3C 36 41 00 FF 15 94 51 41 00 83 C4 08

8
Stages of Compilation
o Performed by a program called the compiler
o Translates the preprocessor-modified source code into object code (machine code)
o Checks for syntax errors and warnings
o Saves the object code to a disk file
o If any compiler errors are received, no object code file will be generated.
o An object code file will be generated if only warnings, not errors, are received.

Execution
when the program become don’t has errors, the computer execute it to produce the ……output

Verify that the algorithm works → source of errors


Many errors made in:
- analyzing the problem,
- developing an algorithm, and/or
- coding the algorithm

9
Types of Errors
Syntax errors: detected by the C compiler
. A program can only be executed if it is syntactically correct; otherwise, the process fails and returns an
error message
. syntax refers to the structure of a program and the rules about that structure
. examples of syntax errors: undeclared variable, …

Run-time errors: detected and displayed by computer during execution.


- So called because the error does not appear until you run the program.
- Occur when program directs computer to perform illegal operation. Example: int x=y/0;
- These errors are also called exceptions because they usually indicate that something exceptional (and
bad) has happened.
Semantic errors: Related to Meaning
▪ If there is a semantic error in the program, it will run successfully, in the sense that the computer will not
generate any error messages, but it will not do the right thing. It will do something else. Specifically, it will
do what the programmer told it to do.
▪ But the written program does not solve the original problem. The meaning of the program (its semantics) is
wrong.

Programming errors are called bugs and the process of tracking them down and correcting them is called
debugging
10
Preprocessor Directives Declarations
• Begin with # • Global
• Instruct compiler to perform some – visible throughout program
transformation to file before compiling – describes data used throughout program
• Example: #include <stdio.h> • Local
– add the header file stdio.h to this file – visible within function
– .h for header file – describes data used only in function
– stdio.h defines useful input/output functions

Functions
• Consists of header and body
– header: int main ()
– body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
11
Main Function Comments
• Every program has one function main • Text between /* and */
• Header for main: int main () • Used to “document” the code for the
• Program is the sequence of statements human reader
between the { } following main • Ignored by compiler (not part of program)
• Statements are executed one at a time • Have to be careful
from the one immediately following to
– comments may cover multiple lines
main to the one before the }
– ends as soon as */ encountered (so no internal
comments - /* An /* internal */ comment */)
#include <stdio.h>

/* This comment covers


* multiple lines
* in the program. */ Comment Example
int main () /* The main header */
{
/* No local declarations */

printf(“Too many comments\n”);


} /* end of main */
12
C Program Structure

Preprocessor Directives • Program


defined by: #include <stdio.h> Preprocessor Directive
Global Declarations – global
declarations int x; Global Declaration
– function int main () {
Function Definitions definitions int y; Local Declaration
Function
int main () { printf("Enter x and y: ");
scanf(&x,&y); Statements
Local Declarations • Always has printf("Sum is %d\n",x+y);
one function }
named main,
Statements may contain
others
}

13
Structure of C Program #include <stdio.h> //preprocessor Run Window
directive

int main() //main function


#include <stdio.h> //preprocessor
directive { //variable declaration
int sum; int x; int y;
int main() //main function
Enter the values to add up:
printf(“Enter the values to add 117 30
{ up:");
//Program Body scanf(“%d %d", &x, &y);

return 0; sum = x + y; //processing


}
printf("The value of the sum is
The value of the sum is
%d", sum); 147
//output

return 0;
}

14
Structure of C Program

Can you guess what this


program does ?
15
Header Files in C
In C language, header files contain a set of predefined standard library functions. The .h is the
extension of the header files in C and we request to use a header file in our program by including it
with the C preprocessing directive “#include”.

C Header files offer the features like library functions, data types, macros, etc. by importing them
into the program with the help of a preprocessor directive “#include”.

Standard Header Files in C Standard header files contain the libraries defined in the ISO standard of
the C programming language. They are stored in the default directory of the compiler and are present in
all the C compilers from any vendor.
There are 31 standard header files in the latest version of C language.

Non-Standard /User Defined Header Files in C


Non-standard header files are not part of the language’s ISO standard. They are generally all the header
files defined by the programmers for purposes like containing custom library functions etc. They are
manually installed by the user or maybe part of the compiler by some specific vendor.

16
Header Files in C

17
Different header files in C:-

Header Files Description Functions/macros/variables


stdio.h Input/Output functions scanf(), printf(), fopen(),
Conio.h console input and output operations. getch(), putch()
stdlib.h General Utility functions atoi(), atof(), malloc()
math.h Mathematics functions sin(), cos(), pow(), sqrt()
string.h String functions strcpy(), strlen(), strcat()
ctype.h Character handling functions isalpha(), isupper(), ispunct()
time.h Date and time functions asctime(), gmtime(), mktime()

float.h Limits of float types FLT_ROUNDS, FLT_RADIX,

limits.h Size of basic types CHAR_BIT, CHAR_MIN, CHAR_MAX

18
Standard Header File In C & Their Uses
The <stdio.h> Header File In C
The name stands for standard input/output header, and it contains functions for standard input and
output operations
Some standard functions that form a part of this header file in C are:

printf(): Used to print formatted output to the console or a file.


scanf(): Used to read formatted input from the console or a file.
fopen(): Used to open a file.
fclose(): Used to close a file.
fseek(): Used to set the file position indicator for a file.
fread(): Used to read data from a file.
fwrite(): Used to write data to a file.

19
Standard Header File In C & Their Uses
The <stdlib.h> Header File In C
The term stdlib.h stands for standard library header, which contains functions for general-purpose
tasks such as memory allocation, process control, type conversions, and searching.

malloc(): Used to dynamically allocate memory.


free(): Used to free dynamically allocated memory.
atoi(): Used to convert a string to an integer.
atof(): Used to convert a string to a floating-point number.
rand(): Used to generate a random number.
qsort(): Used to sort an array.

20
Standard Header File In C & Their Uses
The <string.h> Header File In C
This header contains functions for string manipulation and memory manipulation.

strlen(): Used to get the length of a string.


strcpy(): Used to copy one string to another.
strcat(): Used to concatenate two strings.
strstr(): Used to find a substring in a string.
memset(): Used to set the value of a block of memory to a specific value.
memcpy(): Used to copy a block of memory from one location to another.

21
Standard Header File In C & Their Uses
The <math.h> Header File In C
This header file contains mathematical functions such as trigonometric functions, logarithmic
functions, and exponential functions.
sin(): Used to calculate the sine of an angle.
cos(): Used to calculate the cosine of an angle.
tan(): Used to calculate the tangent of an angle.
sqrt(): Used to calculate the square root of a number.
pow(): Used to raise a number to a power.
ceil(): Used to round a number up to the nearest integer.
floor(): Used to round a number down to the nearest integer.

22
Standard Header File In C & Their Uses
The <time.h> Header File In C
As the name suggests, this header file contains functions for time manipulation and conversion

time(): Used to get the current time in seconds since the Epoch.
localtime(): Used to convert a time value to a local time.
gmtime(): Used to convert a time value to a UTC time.
mktime(): Used to convert a local time to a time value.
strftime(): Used to format a time value as a string.

23
Standard Header File In C & Their Uses
The <float.h> Header File In C
The float.h file contains constants representing the minimum and maximum values that can be
stored in floating-point data types
FLT_MAX: The maximum value that can be stored in a float.
FLT_MIN: The minimum value that can be stored in a float.
DBL_MAX: The maximum value that can be stored in a double.
DBL_MIN: The minimum value that can be stored in a double.

24
How to Create your own Header File?
Apart from the pre-existing header files in C, you can create your own header file too. With
the help of header files, you can avoid writing long and complex codes in your program. It
also enhances the readability and functionality of the program code.

25
Let us take an example on how to create your own header file through steps.
Step:-1
Write your own code in the .h extension file. Step:-3
Because you are creating a header file which Then compile and run the program code.
you will use in your program code.
#include <stdio.h>
int fact(int num) #include"fact.h"
{ int main()
int count, fact=1; {
for(count=1; count<=num; count++) printf(“VIT Tutorial: Create your own header file!\n");
{ int n=6;
fact=fact*count; printf("The factorial of %d is: %d!",n,fact(6))
} return 0;
return fact; }
}

<Name the above code as fact.h>


Output:-
Step:-2 VIT Tutorial: Create your own header file!
Include the fact.h header file in your The factorial of 6 is: 720!
program code.
#include<fact.h> or #include “fact.h”
26
How The Include Operation Work With Header Files In C?

27
THANK YOU
28

Vishwakarma Institute of Technology, Pune

You might also like