PASP Unit 1_2
PASP Unit 1_2
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.
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);
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
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, …
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>
13
Structure of C Program #include <stdio.h> //preprocessor Run Window
directive
return 0;
}
14
Structure of C Program
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.
16
Header Files in C
17
Different header files in C:-
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:
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.
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.
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; }
}
27
THANK YOU
28