Unit 1
Unit 1
Procedural Programming
Object-oriented Programming
Logic programming
Functional Programming
High-level languages include the popular BASIC programming language as well as
other languages that just aren’t that popular any more BASIC reads almost like
English, and all its commands and instructions are English words — or at least
English words missing a few vowels or severely disobeying the laws of spelling.
int main()
{ puts("Hello, World");
return 0;
}
Steps for C program
1. Save source code in a text editor and save it as a text file with the c (single
letter c) extension.
2. Compile
3. Link
4. Run
Compiler,Assembler,Interpreter
Compiler is a computer program that reads a program written in one language,
which is called the source language, and translates it in to another language,
which is called the target language. Most often, the source language is a high level
language and the target language is a low level language.
The Interpreter translates the program written in high level language into machine
language at the time of executing that program, instructions by instructions. That
is, it reads the first instruction written in the program and converts that into
equivalent machine language instructions.
#include<stdio.h> /* For
printfsum() & scanf() */
#include<conio.h> /* For
clrscr() & getch() */
int main() /* Starting point of the
program execution*/
{
int a,b,c,d,e,sum;
float avg; /* Variable Declarations */
clrscr(); /* Clear Screen */
printf("enter five numbers"); /* Request for Input */
scanf("%d %d%d%d%d”,&a,&b,&c…); /* Input from user
*/
sum=a+b+c+d+e; /* Adding
Tokens
The smallest individual unit known as tokens. C recognizes six types of tokens. C
programs are written using these tokens and the syntax of the language. Following
are the C Tokens:
Keywords :- Keywords are the reserved words whose meaning has already been
explained to the C compiler. These words are defined in the language itself. There
are 32 keywords in C.
Identifiers :- Identifiers are the names given to variables. Using this identifier we
can access that variable. All other names in a C program such as array name or a
function name are also known as Identifiers.
● Simple brackets ( ): It is used in function declaration and function calling. For example, printf() is a
predefined function.
● Hash/preprocessor (#): It is used for pre-processor directive. It basically denotes that we are using
the header file.
● Curly braces { }: It is used in the opening and closing of the code. It is used in the opening and
closing of the loops.
FLOWCHART
HEADER FILES
#include <stdio.h>
#include "mydecls.h"
ALGORITHMS
An algorithm is a procedure or step-by-step instruction for solving a problem. They form the foundation of writing a
program.
1. Read A,B.
3. Write SUM.
4. Exit.
PSEUDOCODE
Pseudocode is a simple way of writing programming code in English. Pseudocode is not actual programming language. It
uses short phrases to write code for programs before you actually create it in a specific language.
main()
integer a,b,sum;
read in a and b;
write sum;
}
Storage classes
Program Execution
Sequence logic -- that simply means the statements in the programs are
executed one after the other, from the first to the last.
Selection logic -- they will test some condition and take different courses of
action based on whether the condition is true or false.
Repetition Logic -- The idea is to write statements once and get the computer to
execute them repeatedly as long as some condition is true.
...
satament3
if(){
if(){
if(){
if (){
}else{
... }
}else {
} }
else{
else if() { }
}
Switch Case –
switch(expression)
{ case 1: statement 1
sequence; break;
case 2: statement 2 sequence; break;
case 3: statement 3 sequence; break;
-- -- -- case n: break;
default : default statement sequence;
}
Control Statements (Looping)
Looping is a process of repeating a single statement or a group of statements until some condition for
termination of the loop is satisfied.
Entry control loops: Those loops in which condition is checked before the execution of the statement.
Thus if the condition is false in the beginning the loop will not run even once. (for,while)
Exit control loops: Those loops in which the condition is checked after the execution of the statement.
Thus if the condition is false in the beginning the loop will run at least once. (do-while)
While loop
initialize; #include <stdio.h>
main()
while(condition) {
int i=0;
while( i<100 )
{ {
printf("\ni=%d",i);
statement; i=2i-1;
}
increment or decrement; statement3;
}
} 0
-1
Do - While Loop
initialize; int j = 5;
do printf("start\n");
{ do {
statement1 printf("stop\n");
For Loop
for (initialize; condition; increment or decrement) #include<stdio.h>
{ #include<conio.h>
main()
statement; {
int i;
for(i=1;i<100;i++ )
}
{
printf("\ni=%d",i);
}
}
continue - changes flow of a program. It does break - The break statement is used inside a loop,
to directly come out of the loop. We have already
not terminate the loop however. It just skips the
used break in switch statement. It is used to skip the
rest of current iteration of the loop and returns
execution of the loop any further and transfer the
to starting point of the loop. control of the program to the statement following the
loop.
#include <stdio.h>
int main() {
#include <stdio.h>
int i;
double number, sum = 0.0; int main() {
int i;
for (i = 1; i <= 10; ++i) { double number, sum = 0.0;
printf("Enter a n%d: ", i);
scanf("%lf", &number); for (i = 1; i <= 10; ++i) {
printf("Enter a n%d: ", i);
if (number < 0.0) { scanf("%lf", &number);
continue;
} // if the user enters a negative number, break
the loop
if (number < 0.0) {
sum += number; // sum = sum + number; break;
} }
return 0;
}
goto-It is also used to jump from one part of the program to the other or exit from
the deeply nested loop. But using goto is not a good practice because it makes the
logic complicated and long programs unreadable.
return - Used in a function to return some value and to jump from called function
definition to calling function definition.
return 0; return(0);
} }