PPS Unit-1 (C Basic)
PPS Unit-1 (C Basic)
PROBLEM SOLVING
UNIT 1 CONTINUE..
* 1
STRUCTURE (LAYOUT) OF C PROGRAM
* 2
STRUCTURE (LAYOUT) OF C PROGRAM
Example
// Sample of C Program(documentation Section)
#inclufe<stdio.h> // link section
#include<conio.h> // link section
#define pi 3.14 // definition section
int a=10; // global variable declaration
void disp(); // global function declaration
void main() // main function definition
{
float area,r;
printf(“enter radius”);
scanf(“%f”,&r);
area=pi*r*r;
printf(“area=%f”,area);
disp();
getch();
}
3
STRUCTURE (LAYOUT) OF C PROGRAM
Example
l 4
PROCESS OF COMPILATION AND EXECUTION OF C PROGRAM
System read
Syntax
error
Logical,
Data error data error Logical error
Correct output
Stop
* 5
CHARACTERISTICS OF C
* 6
APPLICATIONS OF C
* 7
Types of error
1.Syntax error
3.Linker error
4.Logical error
5.Semantic error
* 8
ERRORS CONTINUE..
Syntax error:
• The errors which arises due to violation of any rule or regulation of C language during
the development of program those errors are known as Syntax error.
• Syntax errors are also known as the compilation errors as they occurred at the
compilation time, or we can say that the syntax errors are thrown by the compilers. These
errors are mainly occurred due to the mistakes while typing or do not follow the syntax of
the specified programming language.
Examples:-
1.Missing of semicolon.
2.If we miss the parenthesis (}) while writing the code.
3.Displaying the value of a variable without its declaration.
* 9
ERRORS CONTINUE..
* 10
ERRORS CONTINUE..
• Runtime error: The error which occurs during the execution of program those errors
are turn termed as runtime error.
Sometimes the errors exist during the execution-time even after the successful
compilation known as run-time errors. When the program is running, and it is not able
to perform the operation is the main cause of the run-time error. The division by zero
is the common example of the run-time error. These errors are very difficult to find, as
the compiler does not point to these errors.
Example
1.Devision by zero.
* 11
ERRORS CONTINUE..
Linker error
• Linker errors are mainly generated when the executable file of the program is
not created.
• This can be happened either due to the wrong function prototyping or usage of
the wrong header file.
* 12
ERRORS CONTINUE..
Linker error
• Linker errors are mainly generated when the executable file of the program is
not created.
• This can be happened either due to the wrong function prototyping or usage of
the wrong header file.
• The most common linker error that occurs is that we use Main() instead of
main().
• Example using print() instead of printf().
• . These error are identified during linking object code with system library.
#include <stdio.h>
int Main() // Linker error
{ int a=78;
printf("The value of a is : %d", a);
return 0;
}* 13
ERRORS CONTINUE..
Logical error
• The errors which are introduced due to usage of wrong expression formula
for logic in program these errors are called as logical error for example using 2
* 3.14 as area of circle.
• These errors produce the incorrect output, but they are error-free, known as
logical errors.
• If the programmers sound logically good, then there will be fewer chances of
these errors.
* 14
ERRORS CONTINUE..
#include <stdio.h>
int main()
{
int sum=0; // variable initialization
int k=1;
for(int i=1;i<=10;i++); // logical error, as we put the semicolon after loop
{
sum=sum+k;
k++;
}
printf("The value of sum is %d", sum);
return 0;
}
* 15
ERRORS CONTINUE..
Semantic error :
These are valid code the compiler understands, but they do not what you, the
programmer, intended. The following can be the cases for the semantic error:
int i;
i=i+2;
2.Type compatibility
int b = “ABES";
3.Errors in expressions
int a, b;
int a[10];
a[10] = 34;
* 16
FILE BASED STRUCTURE OF C
Linker
Executable Code
Loader
Primary memory
* 17
INTEGRATED DEVELOPMENT ENVIRONMENT
• Editor is a program much like a word processor that is used to write or edit the
source code of any program.
• Preprocessor is a program that process the source code before it passes through
The compiler and convert source code into expanded source code by mean of
preprocessor directives.
• Compiler:
• Linker is a program which combines various pieces of relocatable object code and
data together to form a single executable code that can be loaded in primary
memory.
• Loader is a system program or a part of an operating system that is responsible for
loading the executable code into primary memory for its execution.
* 18
C TOKENS
C tokens
identifier
Special symbol string
•Keyword are also known as reserved words whose meaning is already defined in C
library they all perform some specific purpose. There are mainly 32 keywords some are :
int, char, float, double if , else, break, continue ,case, void etc.
* 19
C TOKENS CONTINUE..
* 20
C TOKENS CONTINUE..
• Constant: It is named location in a memory where entity or value that does not
change during the execution of programs.
const key word is used to make constant.
Exp: const int a=10; const float b=2.5;
Constant
Primary Secondary
Structure
Integer Real Single String
* 21
Rule for Integer Constants in C:
* 23
C TOKENS CONTINUE..
Rules for character constant
• It is a single alphanumeric digit.
• The character is enclosed in single quotes.
• Both single inverted comma point towards left.
• The maximum length of a character constant is one.
• Example ‘a’, ‘1’ , ‘@’
Operator are symbols whose meaning is already known to a C compiler there are total
45 operators used in C language.
String the collection of characters digits or special symbols and closed in double quotes
and terminated by null character is termed as string.
* 24
DATA TYPES
• Data types determine the types of value and the range of values that can be stored
in a variable
C support three classes of data types
1)Primary data type🡪 char, int, float
2) Derived data type🡪 array, pointer
3)User defined data type 🡪 structure, union,enum
* 25
PRIMARY DATATYPE TABLE
• The process of giving something to the computer is known as input. Input is mostly
given through keyboard. The process of getting something from the computer is
known as output. Output is mostly displayed on monitors.
• Types of I/O Statements
printf()
Formatted I/O scanf()
* 27
SCANF FUNCTION
scanf() function
The function used to get input from the user during execution of program and stored in a
variable of specified form is called scanf() function.
Syntax:
scanf(“format string”,& variable name);
format string format specifier is written & address operator
Types:
Single input ex scanf(“%d”,&a);
Multiple input ex scanf(“%d%d”,&a,&b);
* 28
SCANF FUNCTION EXAMPLE
#include<stdio.h>
#include<conio.h>
void main ()
{ int a;
clrscr();
printf("Enter integer value:");
scanf("%d",&a);
printf("The value you enter = %d",a);
getch();
}
Output
Enter integer value:5
The value you enter =5
* 29
PRINTF FUNCTION
printf () function
This function is used to display text, constant or value of variable on screen in specified
format.
Syntax:
printf(“format string”, argument list);
Types:
printf(“hello world”); // Printf()with no argument list
printf(“the value of integar=%d”,a); //Printf() with one argument
printf(“Sum of %d+%d=%d”,a,b,a+b); //Printf()with more than one argument
* 30
GETCHAR FUNCTION
getchar()
• The getchar function is a part of the standard C input/output library. It returns a single
character from a standard input device (typically a keyboard). The function does not
require any arguments, though a pair of empty parentheses must follow the word
getchar.
Syntax
character variable = getchar( );
where character variable refers to some previously declared character variable
* 31
PUTCHAR FUNCTION
putchar()
• The putchar function like getchar is a part of the standard C input/output library. It
transmits a single character to the standard output device (the computer screen).
Syntax
putchar(character variable)
where character variable refers to some previously declared character variable.
* 32
GETCH VS GETCHE
getch() getche()
getch() is used to get a character from getche() is used to get a character from
console but does not echo to the screen. console, and echoes to the screen.
It reads a single character directly from the getche() reads a single character from the
keyboard, without echoing to the screen. keyboard and echoes it to the current text
window, using direct video or BIOS.
* 33
BACKSLASH CHARACTER CONSTANT/EXECUTABLE
CHARACTER SET/ESCAPE SEQUENCES
* 34
Character Escape Result
Sequence
Bell \a Beep Sound
Back Space \b Moves Previous Position
Horizontal Tab \t Moves next horizontal tab
NULL \0 NULL
* 35
STORAGE CLASS USED IN C LANGUAGE
S. No. Keyword Storage place Initial / default value Scope Life Time
Local to
1 auto Memory Garbage value Within the block only.
block
Local to
2 register Register Garbage value Within the block only.
block
Retains the value of
Local to the variable between
3 static memory Zero
block different function
calls.
* 36
1. Blocak means
{
int a;
{
int b;
a and b both used.
}
only a used
}
2. int a; print(“%d”,a);default value
* 37
* 38