2 Sequence
2 Sequence
2 Sequence
1. Sequence
2. Selection
3. Repetitions/loops
Sequence
Sequence denotes executing all the statements one by one from Top
to Bottom.
Arithmetic operators -
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (for Remainder)
How to start C
To open an application we have to know the executable file name.
E.g.
Notepad – notepad.exe
Paint – mspaint.exe
MSword – winword.exe
Turboc C IDE – tc.exe
Input/output functions
The function which is used to display output on the screen is
known as Output function.
E.g. printf(“%d”,no);
The function which is used to accept data from the user is known
as Input function.
.C .obj
.exe
.bak
Compiling is the process of converting source code into object code. (Alt+F9).
Linking is the process of combining object code with library functions (F9).
Execution is the process of passing to machine instructions to processor for output
(Ctrl+F9).
For e.g. If the C program is saved with the name Hello, after the compilation it will
create following files -
Hello.c – contains the source code.
Hello.bak – just like a duplicate copy.
Hello.obj – contains the object code (machine code).
Hello.exe – it is the executable file. The program can be executed without C
software (without source code also).
More about translators:
Translators are the programs which converts source code into machine code. The
Different translators are.
Assemblers – for Assembly language
Compilers – for HLL
Interpreter – for 4GL’s
Compiler:
* Whole program is compiled at a time.
* A small change in the program requires whole program
compilation.
* So, the Program can be executed only if it is translated.
* Ex: BASIC, COBOL, C, C++, Pascal, etc...
Interpreter:
* Code is translated line by line.
* Interpreter program and source code program both are loaded in
the memory at a time.
* No copy of translation code exists, if the program is be re-
run, it has to be interpreted all over again.
* Ex: VB, Small Talk, PERL, etc....
Conclusion:
# include <stdio.h>
main()
{
int a,b,c;
a=b=c=0;
clrscr();
printf("Enter a no:");
scanf("%d",&a);
printf("Enter another no:");
scanf("%d",&b);
c=a+b;
printf("The result is:%d",c);
getch();
}
# include <stdio.h>
main()
{
int m,p,c;
float t,a;
clrscr();
t=m+p+c;
a=t/3;
printf("Total is:%.0f",t);
printf("\nAverage is:%.2f",a);
getch();
}
Note: 1. %.2f is known as precision specifier, displays two digits after the decimal
2. %f displays 6 digits after the decimal ( by default )
Program to swap values of two variables A and B
# include <stdio.h>
main()
{
int a,b,temp;
clrscr();
printf("Enter value for A:");
scanf("%d",&a);
printf("Enter value for B:");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("A is :%d",a);
printf("\nB is :%d",b);
getch();
}
WAP to convert the input total minutes into hours and minutes.
Hands on-lab:
1. WAP to calculate amount for the input rate and quantity.
Note: Rate should be in decimals.
2. WAP to swap the values of two variables without using 3rd variable.
3. WAP to convert the input rupees into paises.
4. Write a program to convert Fahrenheit to centigrade.
Formula: c=f-32*5/9.
5. WAP to calculate Electricity bill amount from the input present month and last
month reading.
Note: Rate per unit is 2.50
6. Program to calculate gross salary and net salary from the input basic,
allowances and deductions.
Type casting:
Converting value of one data type to another type during runtime is known as type
casting.
# include <stdio.h>
main()
{
int a=5,b=2;
float c=0;
clrscr();
c=a/b;
printf("%f",c);
getch();
}
So the above problem can be solved by declaring any of the input variables to float, or
type casting....
c=(float)a/b;
printf("%f",c);
getch();
}
Note:
C automatically does the conversion on its own. Still there are cases where we have to
do it explicitly.
# include <stdio.h>
main()
{
char ch;
clrscr();
printf("Enter a character..:");
scanf("%c",&ch);
getch();
}
Note: Only one character is stored in the memory, because char data type allocates
only 1 byte.
# include <stdio.h>
main()
{
char name[50];
clrscr();
getch();
# include <stdio.h>
main()
{
char name[50];
clrscr();
getch();
}
Note: % [ ^ \n ] – keep on reading characters from keyboard buffer till enter key(‘\n’)
encounters.
stop Enter key
Whenever we enter any data from the keyboard, the data in initially stored in
keyboard buffer, when we press enter key then only the data is passed into RAM
ASCII codes:
*** Characters are internally stored as integer (ASCII) values.
All the data (input) is represented in binary format. Each and every data item has an
ASCII code.
Write a program to display the ASCII code for the inputed character.
# include <stdio.h>
main()
{
char ch;
clrscr();
printf("Enter a character....:");
scanf("%c",&ch);
printf("The ASCII Code for (%c) is %d",ch,ch);
getch();
}
# include <stdio.h>
main()
{
int code=0;
clrscr();
printf("Enter a no(0-255):");
scanf("%d",&code);
printf("The ASCII character for %d is %c",code,code);
getch();
}
Errors
There are four types of errors in “C” language.
Compile time errors.
Linking errors.
Logical errors.
Runtime errors.
Linking errors
Linking is a process of combining the machine code versions of our source code
with the machine code versions of the library functions.
If there is any problem during this process, then the linker software reports a
linking error.
E.g. If there is any spelling mistake in the library function.
Detecting and correcting these errors is very easy.
Logical errors
These errors occur if there is any mistake in the logic of the program.
These errors are not displayed on the screen.
We can identify the logical error only if we know the result in advance.
Detecting and correcting these errors is very difficult.
Runtime errors
The errors which occur during the execution of a program are called as run time
errors.
E.g. divide a number with zero / modifying illegal memory location.
Detecting and correcting these errors is very difficult.
A software success depends on how far the runtime errors have been tackled.
Most of the program development time is spent on detecting and correcting runtime
errors.
Garbage value (Unknown value)
main()
{
int n;
clrscr();
printf("%d",n);
getch();
}
main()
{
int n=10; /* initialization */
clrscr();
printf("\n%d",n);
n=100; /* assignment */
printf("\n%d",n);
getch();
}
Const keyword
If the value of a variable is fixed and should not be changed even accidentally, then
such a variable is defined as const.
Example program
# include <stdio.h>
main()
{
const float PI=3.14;
clrscr();
printf(“%f”,PI);
getch();
}
Data types
Classification
C Data types
Character Array
Integer Pointer
Float Structure
Double Union
Void Enum etc…
Modifiers
The modifiers signed, unsigned, short and long are applied to get additional data
types.