c-language
It is a high-level programming language. It was invented by Dennis Ritchie in 1972. C language is a portable language that means a program written on one computer can be run or another computer can be run.
Rules for C
- Every C program must contain header files
stdio.h(standard input output header files)
conio.h (console input output header files)
#include<stdio.h>
#include<conio.h> - Every c program must contain one main( ) function
main( )
{
clrscr( );
printf(“Hello”);
getch( );
}
Syntax and Logical Error
I) Syntax Error: It is an error that does not conform to the grammar of the programming language.Ex missing semicolon, using an uppercase letter instead of a lowercase letter.
II) Logical Error: It is an error that occurs when the program doesn’t want the programmer except it to do so.
This type occurs due to a poor understanding of a problem.
Ex: Months in year=13
average = Total/months in a year
= 50/13
The above example shows a logical error
Pseudocode
Pseudocode is an informal high-level description of the operating principle of a computer program or an algorithm. It is an informal description to write an algorithm. It is a textbook-based design tool. Pseudocode does not use any programming language in its representation instead it uses the simple English language text as it is intended for human understanding rather than machine reading.
Ex: If a student is secured more than 60% marks then print”passed” otherwise print “failed”
Object and Executable code
I) Object code: It is the product of compiler. It is a code in which sequences of instructions are written in machine language. It is code is a machine language that is directly understood by a computer system exact without further simplification needed. Object code is generated from source code after going through a compiler or other translator.
II) Executable code: It is also called the Binary. It is the output of a linker after it processes the object code. A machine code file can be immediately executable (i.e., runnable as a program), or it might require linking with other object code files (e.g. libraries) to produce a complete executable program.
C Tokens
Tokens are the basic building blocks of the program. Any individual word, punctuation, mark, or special symbol is knowns as a token. C support six types of tokens
- Keywords: Keywords are reserved words that have fixed meanings. It supports 32 keywords. Ex:- auto, break, char, double, float, for, int, long, while, switch, structure, union, etc
- Constant: Constants are expressions that have a fixed value.
i)Integer constant:- 10,-32, etc
ii) string constant:- “Hello”, “Bye”, Etc - Identifiers: These refer to the variable name.
Ex:- int time; //here time is an identifier used to indicate a variable. - Special symbol: Symbols other than the alphabet, digits, and underscore(-) are known as special symbols.
Ex:- #, @, $,& etc - String: The sequences of characters are known as strings.
Ex:- “Good”, “Hello” etc - Operator: Symbols used to perform mathematical and non-mathematical operations are known as operators.
Ex:-(+ , Addition),(-, subtraction), (* ,Multiplication),(/, Division), (Modulus, %), >,>=,<,<=,==, etc.
Variable
It refers to the named memory location which is used to store the value of data .
Rules for declaring variables
- a variable cannot be keywords
- the first letter cannot be a digit
- the first letter can be an alphabet or underscore(-). No other special symbols are allowed as the first letter except underscore (-).
Ex:- int 1d; //wrong statement
int -d: //correct
int d1-; // correct
int @d; //incorrect
Data Types
It specifies the type of data that variable used to store the value. There are two types of data types available in ‘C’
I) Primitive data types: int, float, char, double, etc
II) Derived data types: Structure, union, etc
Data Type | Format specifier | Range | Size |
int | %d | -32768 to 32767 | 2 bytes |
float | %f | 3.4e-338 to 3.4e+38 | 4 bytes |
char | %c | -128+127 | 1 bytes |
double | %ld | 1.7e-308 to 1.7e+308 | 8 bytes |
Note: If we want an accurate value of the integer then use long (%ld)
operators
operators are symbols used to perform mathematical or non-mathematical operations. There are three types of operators .
- Unitary operators
- Binary operators
- Ternary operators
- Unitary operators: There are also three types of unitary operators:
I) Increment operators(++) :
a) pre-increment operators: They first increment the value of an operand by one then assign the updated value of a variable
Ex:-
main( )
{
int i = 4,J ;
j= ++i;
printf (“The value of j is %d”,J);
printf(“The value of i is %d”, i);
getch( );
}
output: The value of b is 5 and the value of a is 5
b) post-increment operators: It first assigns the old value to the variable and then increment.
main( )
{
int i = 9, J ;
J= i++;
printf(“The value of J is %d”,J);
printf(“The value of i is %d”, i);
getch( );
}
output: The value of b is 4 and the value of a is 5
II)Decrement operators ( – – ): It is used to decrement the value of an operand by 1
a) Pre-decrement: It first decrements the value of an operand by one then assign the updated value of a variable
Ex:-
main( )
{
int i = 4,J ;
j= – – i;
printf (“The value of j is %d”,J);
printf(“The value of i is %d”, i);
getch( );
}
b) post-decrement: It first assigns the old value to the variable and then decrement.
Ex:-
main( )
{
int i = 4,J ;
j= i– ;
printf (“The value of j is %d”,J);
printf(“The value of i is %d”, i);
getch( );
}
III)Negation operator(!): It is also called not operator.
Ex:-
main( )
{
int i = 10 ,J ;
j= ! i;
printf (“The value of j is %d”,J);
printf(“The value of i is %d”, i);
getch( );
}
Ans: The value of a is 5
The value of b is 0 - Binary operators: It requires to operand to perform the operation. There are four types of binary operations.
They are:
I)Arithmetic operations: These operators are used to perform mathematical operations like addition, subtraction, multiplication, Division, Modulus
Ex:-
main( )
{
int i = -10,j= -3, k;
K= I/j;
printf (“The value of k is %d”,k);
getch( );
}
output: The value of K is 3
II) Relational operators: It is used to check the relation between two operations. If the relation is true then it will be written one otherwise it will be written Zero.
1) > (greater than)
2) >= (greater than or equal to)
3) < (less than)
4) <= (less than or equal to)
5) == equal to )
6) != (Not equal to)
Ex:-
main( )
{
int i = 10,j= 15, k;
K= (I==J);
printf (“The value of k is %d”,k);
getch( );
}
output: The value of K is 0
III) Logical operators: It is used to combine the result of more than conditions. These operators are used in decisions lately. There are three types of logical operators.
a)Logical AND (&&)
b)Logical OR (//)
c) Logical NOT(!)
Ex:-
main( )
{
int a = 20,b= 30 , c= -32, d= 0,e;
e= (a>b)&&(c>d);
printf (“The value of e is %d”,e);
getch( );
}
output: The value of e is 0.
IV) Bitwise operators: bitwise operators are used to perform operations on a bit level.There are also six types
a)bitwise AND(&)
b)bitwise OR(/)
c)XOR(^)
d)Complement(~)
e) Right shift (>>)
f) Left shift (<<)
Ex .1:-
main( )
{
int a = 20,b= 30 , c;
c= a&b;
printf (“The value of c is %d”,c);
getch( );
}
output:The value of c is 1.
Ex .2:-
main( )
{
int a = 7,b= 2 , c;
c= a<<b
printf (“The value of c is %d”,c);
getch( );
}
output:The value of c is 28.
Ex .3:-
main( )
{
int a = 4,b= 7 , c;
c= a^b;
printf (“The value of c is %d”,c);
getch( );
}
output: The value of c is 3.
V)Ternary operators: It takes three operands to perform.
If the condition is true then Experiment 1 will be executed otherwise Experiment 2 will be executed.
Ex .1:-
main( )
{
int a = 5,b= 3 ;
(a>b)?;
printf (“Hello”);
printf(“bye”);
getch( );
}
output: Hello
Decision-Making Statement
It is used to define a group of statements that are executed based on the condition. If the condition is true then some blocks of statement are executed. If the condition is false then again some block of the statement will be executed.
There are five types of decision-making statements.
I) If statement: when the expression evaluates to true then the if block statement will be executed.
syntax:-
if (condition)
{
………………;
……………….;
}
Ex:-
main( )
{
int i = 10;
if(i>5);
{
printf (“Hello”);
}
printf(“\n Bye”);
getch( );
}
output:- Hello
Bye
II) If else statement: When the expression evaluates to true then if block statement will be executed and if it is false then the else block of the statement will be executed.
syntax:-
if (condition)
{
………………;
……………….;
}
else
{
………………;
……………….;
}
Ex:-
main( )
{
int i = 10;
if(i>5);
{
printf (“Hello”);
}
else
{
printf(“\n Bye”);
}
getch( );
}
output:- Hello
III) Nested if else statement:- Within if else there is another if else statement such statement are known as nested if else statement.
syntax:-
if (condition 1 )
{
(if condition 2)
{
…………….;
}
}
else
{
…………….;
}
else
{
if (condition 3)
{
……………;
}
else
{
…………;
}
}
Ex:- wap in c to print maximum among three number using nested if else
main ( )
int a=20, b=15,c=12;
if (a>b)
{
if (a>c)
{
printf(“a is maximum”);
}
else
{
printf(“c is maximum”);
}
}
else
{
if (b>c)
{
printf(“b is maximum”);
}
else
{
printf(“c is maximum”);
}
getch( );
}
output:- a is maximum
IV) Ladder if statement: Ladder if takes more than one expression. Each expression is evaluated one by one .when the expression evaluates to true then the statement written below it will be executed and also else if up to else part will be skipped.
syntax:-
if (expression 1 && expression 2)
{
………………..;
}
else if (expression 3 && expression 4)
{
………………………..;
}
else if (expression 5 && expression 6)
{
………………..;
}
………………………;
…………………………:
else
{
…………………….;
}
Ex:- wap in ‘C’ to print maximum among three numbers using a ladder if statement.
main ( )
int a=18, b=20,c=25;
{
if ( (a>b)&&(a>c));
{
printf(“a is maximum”);
}
else if (b>c);
{
printf(“b is maximum”);
}
else
{
printf(“c is maximum”);
}
getch( );
}
V) Switch case statement: It is a multi-way branch statement. It compares variables among several integral values.
syntax:-
switch case (choice)
{
case 1;
{
………………..;
}
case 2 ;
{
………………………..;
}
case 3
{
………………..;
}
………………………;
…………………………:
default
{
……………………;
}
}
Ex:- Wap in ‘c’ to perform the following operation based on the choice given by users
multiplication
Division
#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,mul,div,choice;
clrscr( );
printf(“Enter the value of a and b\n”);
scanf(“%d %d”, &a,&b);
printf(“enter the choice\n”);
scanf(“%d”,&choice):
switch (choice)
{
case 1
{
mult= a*b;
printf(“The multiplication is %d,”mul);
break;
}
case 2
{
div=a/b
printf(“The division is %d”,div);
break;
}
default
{
printf(” wrong choice…please try again”);
}
}
getch( );
}
Scanf( )
It is a function that is used to take input from the user at run time
syntax:-
scanf(format.specifier, & variable —-name)
where & (ampersand operator ) denotes the address of a variable.
Loop
A loop is a sequence of instructions that is repeated until a certain condition is reached.
There are two types of loops
I) Entry-controlled Loop: In an entry-controlled loop first condition will be checked then a block of statements will be executed. while and for loops are examples of entry-controlled loops.
a) While loop: while is an entry-controlled loop means the condition will be checked first then a block of the statement will be executed.
syntax :-
while (condition)
{
…………;
}
Ex:-
#include<stdio.h>
#include<conio.h>
main ( )
{
int x=1;
clrscr( );
while (x<=10)
{
printf(“%d\n”,X);
x=x+1;
}
getch( );
}
b)For Loop:- It is also an entry-controlled loop. It means the first condition is checked then the body loop will be executed.
syntax:
for (initialization; condition; increment/decrement)
{
……………..;
}
ex:- wap in ‘C’ to print factorial of a number
main ( )
{
int n,,lf=1,i;
clrscr( );
printf(“Enter a number \n”);
scanf(“%d”,&n);
for(i=1; i<=n;i++);
{
f=f*i
}
printf(“The factorial of %d is %ld”,nf);
getch( );
}
II) Exit-controlled loop:- In the exit-controlled loop first block statement is executed then the condition will be checked.
a)Do while loop: Do while loop is similar to a while loop except for the fact that it is granted to execute. Do a while loop at least once even if the condition is false.
syntax:
do
{
………..;
………….;
} while condition
Ex:-
main
{
int i=1;
do
{
printf (“%d”, i);
} while (i>5);
getch( );
}
Break
It stops the loop immediately when it is encountered.
Ex:-
main( )
{
int i;
for ( i=1; i<=5;i++)
{
if (i,==4)
{
break;
}
printf(“%d\t”,i);
}
getch( );
}
output:- 123
continue:-
It skips the current iteration of a loop and jumps control to the next iteration of the loop.
Ex:-
main( )
{
int i;
for (i=1;i<=5;i++)
{
if(i==40
}
printf(“%d\t”,i0;
getch( );
}