Showing posts with label C Basic. Show all posts

Errors in C

Errors

#Error is a abnormal condition whenever it occurs execution of the program is stopped these are mainly classified into following types.
  • Compile time error
  • Run time error

Compile time error

If any error is generated at the time of compilation is known as compile time error, in general these are raised while break down the rules and regulation of programming language.
Example:
Missing semicolon, writing keyword in upper case, writing variable declaration, initialization after calling clrscr() function.
Compile time errors also known as syntax errors.
Compile time errorCompile time error

Run time error

If any error is generated at run time is known as runtime error, in general these are raised because of writing wrong logics in the program.

Example

Calling function without existence, divide by zero.
Int a=10,b;
B=a/0; --> infinite
Here out of range of int data type.
In general it is very difficult to identify logical error in C language, to overcome this problem exception handling was introduced in object oriented programming language.
error in c

Warning

Warning is also an abnormal condition but whenever it occurred execution of program will never be stopped.
Note: In C language warning can be neglected but error can not be neglected.
Warning in cWarning in c

Storage Classes in C

Storage Classes in C

#Storage class specifiers in C language tells to the compiler where to store a variable (Storage area of variable), how to store the variable, Scope of variable, Default value of a variable (if it is not initialized it), what is the initial value of the variable and life time of the variable.
Storage classes of C will provides following information to compiler.
  • Storage area of variable
  • Scope of variable that is in which block the variable is visible.
  • Life time of a variable that is how long the variable will be there in active mode.
  • Default value of a variable if it is not initialized it.

Type of Storage Class

Storage classes in mainly divided into four types,
  • auto
  • extern
  • static
  • register

Properties of All storage class

TypeStorage placeScopeLifeDefault Value
autoCPU MemorybodyWithin the FunctionGarbage value
staticCPU Memoryfunctionprogram0 (zero)
externCPU MemoryprogramTill the end of the main program.0 (zero)
registerRegister memorybodyWithin the FunctionGarbage value

auto Storage Class

The auto storage class is the default storage class for all local variables. The scope auto variable is within the function. It is equivalent to local variable.

Syntax

{
   int roll;
   auto int roll;
}
In above example define two variable with same storage class auto and their scope is within the function.

Example of auto storage class

#include<stdio.h>
#include<conio.h>

void increment();
void main()
{
increment();
increment();
increment();
increment();
getch();
}
void increment()
{
auto int i = 0 ;
printf ( "%d", i ) ;
i++;
}

Output

Output:
0 0 0 0

static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.

Example of static storage class

#include<stdio.h>
#include<conio.h>

void increment();
void main()
{
increment();
increment();
increment();
increment();
getch();
}
void increment()
{
static int i = 0 ;
printf ("%d", i ) ;
i++;
}

Output

Output:
0 1 2 3

extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. It is equivalent to global variable.

Example of extern storage class

Example

#include<stdio.h>
#include<conio.h>

int x = 20 ;
void main( )
{
extern int y;
printf("The value of x is %d \n",x);
printf("The value of y is %d",y);
getch();
}
int y=30;

Output

The value of x is 20
The value of y is 30

Register variable

Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory.
Advantages: The register variables are faster than remaining variables, because register variable are stored in register memory not in main memory..
Limitation: But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits).
  • In TC-3.0 we can't access the address of register variables.
  • Pointer are ptr related concepts are can't applied to register variable.

Example

void main()
{
register int a=10;
++a;
printf("\n value of a: %d",a);
printf("Enter a value:");
scanf("%d",&a);
--a;
printf("\n value of a: %d",a);
getch();
}

Output

Input data is 50.
Error, must take address of a memory location.

Explanation

  • In scanf() function if address is provided for the register variable then it will give error, if addition is not provided it normally work.
  • Register storage class specifier just recommended to the compiler to hold the variable in CPU register if the memory is available or else stored in stack area of data segment.

Expression evaluation in C

Expression evaluation

#In c language expression evaluation is mainly depends on priority and associativity.

Priority

This represents the evaluation of expression starts from "what" operator.

Associativity

It represents which operator should be evaluated first if an expression is containing more than one operator with same priority.
OperatorPriorityAssociativity
{}, (), []1Left to right
++, --, !2Right to left
*, /, %3Left to right
+, -4Left to right
<, <=, >, >=, ==, !=5Left to right
&&6Left to right
||7Left to right
?:8Right to left
=, +=, -=, *=, /=, %=9Right to left

Example 1:

images

Example 2:

images

sizeof operator

sizeof operator

#The sizeof operator is used to calculate the size of data type or variables. This operator returns the size of its variable in bytes.
For example: sizeof(a), where a is interger, will return 4.

Syntax

sizeof(variable)

Example

#include<stdio.h>
#include<conio.h>

void main()
{
int a;
float b;
double c;
char d;
printf("Size of Integer: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of character: %d byte\n",sizeof(d));
getch();
}

Output

Size of Integer: 2
Size of float: 4
Size of double: 8
Size of character: 1

Ternary operator

Ternary Operator in C

#If any operator is used on three operands or variable is known as Ternary Operator. It can be represented with ? : . It is also called as conditional operator

Advantage of Ternary Operator

Using ?: reduce the number of line codes and improve the performance of application.

Syntax

expression-1 ? expression-2 : expression-3
In the above symbol expression-1 is condition and expression-2 and expression-3 will be either value or variable or statement or any mathematical expression. If condition will be true expression-2 will be execute otherwise expression-3 will be executed.

Syntax

a<b ? printf("a is less") : printf("a is greater");

Flow Diagram


Find largest number among 3 numbers using ternary operator

Example

#include<stdio.h>
#include<conio.h>

void main()
{
int a, b, c, large;
clrscr();
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d",large);
getch();
}

Output

Enter any three number: 5 7 2
Largest number is 7

First C program

First Program

#Programming in C language is very simple and it is easy to learn here I will show you how to write your first program. For writing C program you need Turbo C Editor. First you open TC and write code.

Example

#include<stdio.h>
#include<conio.h>

void main()
{
printf("This is my first program");
getch();
}

Output

This is my first program
  • After writing complete code save the program using F2
  • Save your code with .c extension for example: hello.c
  • After save the code you need to compile your code using alt+f9
  • Finally Run the program using clt+f9

Save C program

Save any C program using .c Extension with file name. For example your program name is sum, then it save with sum.c.

Syntax

filename.c

Compile and Run C Program

for compile any C program you just press alt+f9 , after compilation of your c program you press clt+f9 for run your C program.

Syntax

Compile -> alt+f9

Run -> clt+f9

Increment and Decrement operators

Increment and Decrement Operator in C

#Increment Operators are used to increased the value of the variable by one and Decrement Operators are used to decrease the value of the variable by one in C programs.
Both increment and decrement operator are used on a single operand or variable, so it is called as a unary operator. Unary operators are having higher priority than the other operators it means unary operators are executed before other operators.

Syntax

 ++  // increment operator
 --   // decrement operator
Note: Increment and decrement operators are can not apply on constant.

Example

x= 4++;  // gives error, because 4 is constant

Type of Increment Operator

  • pre-increment
  • post-increment

pre-increment (++ variable)

In pre-increment first increment the value of variable and then used inside the expression (initialize into another variable).

Syntax

++ variable;

Example pre-increment

#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=++i;
printf("x: %d",x);
printf("i: %d",i);
getch();
}

Output

x: 11
i: 11
In above program first increase the value of i and then used value of i into expression.

post-increment (variable ++)

In post-increment first value of variable is used in the expression (initialize into another variable) and then increment the value of variable.

Syntax

variable ++;

Example post-increment

#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i++;
printf("x: %d",x);
printf("i: %d",i);
getch();
}

Output

x: 10
i: 11
In above program first used the value of i into expression then increase value of i by 1.

Type of Decrement Operator

  • pre-decrement
  • post-decrement

Pre-decrement (-- variable)

In pre-decrement first decrement the value of variable and then used inside the expression (initialize into another variable).

Syntax

-- variable;

Example pre-decrement

#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
getch();
}

Output

x: 9
i: 9
In above program first decrease the value of i and then value of i used in expression.

post-decrement (variable --)

In Post-decrement first value of variable is used in the expression (initialize into another variable) and then decrement the value of variable.

Syntax

variable --;

Example post-decrement

#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
getch();
}

Output

x: 10
i: 9
In above program first used the value of x in expression then decrease value of i by 1.

Example of increment and decrement operator


Example

#include<stdio.h>
#include<conio.h>

void main()
{
int x,a,b,c;
a = 2;
b = 4;
c = 5;
x = a-- + b++ - ++c;
printf("x: %d",x);
getch();
}

Output

x: 0