An ARRAY is a accumulating of similar elements. These agnate elements could be all ints, or all floats, or all chars, etc. Usually, the arrangement of characters is alleged a 'string', admitting an arrangement of ints or floats is alleged basically an array. Keep in apperception that all elements of any accustomed arrangement accept to be of the aforementioned type. i.e. they cannot accept an arrangement of ten numbers, of which 5 are ints & 5 are floats.For example, we want to align the allotment marks acquired by 100 acceptance in ascendance order. In such a case they accept options to abundance these marks in memory:

  1. Construct 100 variables to abundance allotment marks acquired by 100 altered students, i.e. anniversary capricious absolute student's marks.
  2. Construct capricious (called arrangement or array) able to autumn or captivation all the hundred values.

Obviously, the additional another is better.This is wahat we call array.

ARRAY  INITIALISATION:
Arrays can be initialised as followed:

int a[6] = { 2, 4, 12, 5, 45, 5 } ;
 int b[ ] = { 2, 4, 12, 5, 45, 5 } ;
float c[ ] = { 12.3, 34.2 -23.4, -11.3 } ;


Read more


main( )
{
int m, f ;
printf ( "\nEnter number " ) ;
scanf ( "%d", &m ) ;
f = factorial ( m ) ;
printf ( "Factorial = %d", f ) ;
}
factorial ( int a )
{
int fact = 1, j ;
for ( j = a ; j >= 1 ; j-- )
fact = fact * j ;
return ( fact ) ;
}

OUTPUT:

Enter number 4
Factorial = 24

Read more

In C, it is achievable for the functions to alarm themselves. A Function is alleged 'recursive' if a account aural the physique of a action calls the aforementioned function. Sometimes alleged 'circular definition', recursion is appropriately the action of defining something in agreement of itself.

FACTORIAL OF A NUMBER USING RECURSION:


main( )
{
int m, fact ;
printf ( "\nEnter any number " ) ;
scanf ( "%d", &m ) ;
fact = recr ( m ) ;
printf ( "Factorial  = %d", fact ) ;
}
recr ( int a )
{
int z ;
if ( a == 1 )
return ( 1 ) ;
else
z = a * rec ( a - 1 ) ;
return ( z ) ;
}

OUTPUT:

Enter any number 3
Factorial  = 6

Read more



main( )
{
int x = 20, y = 12 ;
swap ( x, y ) ;
printf ( "\nx = %d y = %d", x, y ) ;
}
swap ( int a, int b )
{
int z ;
z = a ;
a = b ;
b = z ;
printf ( "\na = %d b = %d", a, b ) ;
}


OUTPUT:


a = 12 b = 20
x = 20 y = 12


Read more


main( )
{
int r ;
float a, peri ;
printf ( "\nEnter radius of a circle " ) ;
scanf ( "%d", &r ) ;
aperi ( r, &a, &peri ) ;
printf ( "area = %f", a ) ;
printf ( "\nperimeter = %f", peri ) ;
}
aperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}

OUTPUT:

Enter radius of a circle 10
Area = 314.000000
Perimeter = 62.800000

Read more


main( )
{
int a = 3 ;
int *b ;
b = &a ;
printf ( "\nAddress of a = %u", &a ) ;
printf ( "\nAddress of a = %u", b ) ;
printf ( "\nAddress of b = %u", &b ) ;
printf ( "\nValue of b = %u", b ) ;
printf ( "\nValue of a = %d", a ) ;
printf ( "\nValue of a = %d", *( &a ) ) ;
printf ( "\nValue of a = %d", *b ) ;
}

OUTPUT:
Address of a = 65512
Address of a = 65512
Address of b = 65522
Value of b = 65512
Value of a = 3
Value of a = 3
Value of a = 3



Read more

 Pointer is a user authentic advice blazon which creates appropriate types of variables which can authority the abode of archaic advice blazon like char, int, float, bifold or user authentic advice blazon like function, arrow etc. or acquired advice blazon like array, structure, union, enum.

POINTER DECLARATION:


Declaring pointers can be actual ambagious & difficult sometimes (working with structures & arrow to pointers). To acknowledge arrow capricious they have to accomplish use of * abettor (indirection/dereferencing operator) before the capricious identifier & afterwards ability type. Arrow can alone point to capricious of the aforementioned ability type.
Syntax:

Read more

Call by reference basically means 'calling function instead of passing values but instead passing the location n umber or address of the variable'.Call by refrence basically use the pointers for address reference.

Read more

Functions in C language can be called by two methods:
  1. Call by value
  2. Call by reference
Call by value:

Call by value basically means passing values between functions when they are called i.e. user provides values to the functions when they are called in the MAIN() function.THese values can either be any constants or variables.
An example illustrating this is given below:

Read more

A function is a independent block of statements that accomplish a articular assignment of some kind. Every C affairs can be anticipation of as a accumulating of these functions. As they acclaimed earlier, application a function is something like hiring a being to do a specific job for you. In the above programs we have used main at the starting, this main() itself is a function.A simple program illustrating the use of function is given below:

Read more


In a difficult programming bearings it seems so simple to accomplish use of a goto to yield the ascendancy area you need. However, always, there is a added affected way of autograph the aforementioned affairs application if, for, while and switch. These constructs are far added analytic and aboveboard to understand.
The huge affair with gotos is that if they do use them they can hardly be abiding how they got to a abiding point in our code. They abstruse the breeze of control. The GOTO keyword is basically used to get over a certain set of instructions instantly.a program illustrating this is shown below:

Read more


main( )
{
int n = 27 ;
switch ( n )
{
case 1239 :
printf ( "i am in case 1239 \n" ) ;
break ;
case 56 :
printf ( "I am in case 56 \n" ) ;
break ;
case 29 :
printf ( "I am in case 29 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}

OUTPUT:

I am in default

Read more

The ascendancy account that lets us accomplish a best from the amount of choices is alleged a switch, or added accurately a switch-case-default, back these keywords go calm to accomplish up the ascendancy statement.
Syntax for switch statement:


switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

Read more

The CONTROL keyword allows us to ignore the statements occurring after the control statement and transfer the control to the beginning of the loop i.e. it refreshes the loop.It is mainly associated with the if statement.It however differs from the break statement i.e. it somehow opposes the break statement.



Read more

The BREAK statement is basically used to get out of the loop. When the break statement is encountered the control is automatically transferred to the statement executing just after the curly braces of the loop i.e. it allows us to get out of the existing loop at any instant of time.



/*Demonstration of break statement*/

main( )
{
int n, count ;
printf ( "Enter a number " ) ;
scanf ( "%d", &n ) ;
count = 2 ;
while ( count <= n - 1 )
{
if ( n % count == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
count++ ;
}
if ( count == n )
printf ( "Prime number" ) ;
}


Read more

There is a accessory aberration amid the alive of while and do-while loops. This aberration is the abode area the action is tested. The while tests the action afore active any of the statements aural the while loop. As adjoin this, the do-while tests the action afterwards accepting accomplished the statements aural the loop.

General syntax for DO-WHILE loop :


do
{
this ;
and this ;
and this ;
and this ;
} while ( this condition is true ) ;



Read more

The loops that they accept acclimated so far accomplished the statements aural them a bound amount of times. However, in absolute activity programming comes beyond a bearings if it is not accepted advanced how affluence of times the statements in the bend are to be executed.


/* Execution of a loop for many number of times */ 

main( )
{
char other ;
int n ;
do
{
printf ( "Enter number " ) ;
scanf ( "%d", &n ) ;
printf ( "square of %d is %d", n, n * n ) ;
printf ( "\nWant to enter other number y/n " ) ;
scanf ( " %c", &other ) ;
} while ( other == 'y' ) ;
}

Read more


/* Demonstration of nested loops */


main( )
{
 int a, b, s;
          for ( a = 1 ; a <= 3 ; a++ ) /* outer loop */
          {
                    for ( b = 1 ; b <= 2 ; b++ ) /* inner loop */
                    {
                    s= a + b ; printf ( "a = %d b = %d sum= %d\n", a, b, s) ;
                    }
           }
}

Read more



/* Calculation of simple interest for 4 sets of pi, t and rate */
main ( )
{
int pi, t, i ;
float rate, si ;
for ( i = 1 ; i <= 4 ; i = i + 1 )
{
printf ( "Enter values of pi, t, and rate " ) ;
scanf ( "%d %d %f", &pi, &t, &rate ) ;
si = pi * t * rate / 100 ;
printf ( "Simple Interest = Rs.%f\t", si ) ;
}
}



Read more


The for bend specify things about a bend in a individual line:
(a) Setting a bend adverse to an basic value.
(b) Testing the bend adverse whether its amount has accomplished the no. of repititions desired.
(c) Increasing the account of bend adverse anniversary time the affairs articulation aural the bend has been executed.

Syntax for FOR loop:

Read more

The general syntax for WHILE loop is given as:

initialise loop counter ;
while ( test loop counter using a condition )
{
do this ;
and this;
increment loop counter;
}

Read more

The versatility of the computer lies in its adeptness to accomplish a set of instructions repeatedly. This involves repeating some allocation of the affairs either a defined amount of times or until a specific action is getting satisfied.
Various loops availble in C are as follows:

  • a for statement 
  • a while statement 
  • a do-while statement

Read more

The codicillary or conditional operators ? and : are sometimes alleged ternary operators back they yield arguments. In fact, they anatomy a affectionate of foreshortened if-then-else. Their accepted anatomy is :


expression 1 ? expression 2 : expression 3


If announcement one is accurate (that is, if its amount is non-zero), again the account alternate will be announcement two, contrarily the account alternate will be announcement 3. Example showing conditional operator:


main( )
{
int a = -4, b, n ;
b = ( n < 0 ? 0 : n * n ) ;
printf ( "\n%d",b ) ;
}

Read more

Various logical and arithmetic operators useful in c are give below as:



      Operators                                                    
       Type
        !
     Logical NOT
        * / %
     Arithmetic and modulus
        + -
     Arithmetic
        < > <= >=
     Relational
       == !=
     Relational
       &&
     Logical AND
       ||
     Logical OR
      =
     Assignment

Read more

It is nothing different from if else. It is jst rearranging the else with the if followed by it. the else if program is given below which proves the above statement:

/* else if  demo */
main( )
{
int a,b,c,d,e, pr ;
pr = (a+b+c+d+e) / pr ;
if ( pr >= 60 )
printf ( "First division" ) ;
else if ( pr >= 50 )
printf ( "Second division" ) ;
else if ( pr >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}

Read more


#include <stdio.h>
#include <math.h>

main() 
 {
  float a, b, c, x1, x2, q, d, x1r, x2r, x1i, x2i;

  printf("Input coefficients of square equation a,b,c:");
  scanf("%f %f %f", &a, &amp;b, &c);
 
  d=b*b -4.0*a*c; 

  if (d > 0)
 {
                                          /* results are real numbers */
          q = pow (d, 1./2);
          x1 = (-b + q)/(2*a);
          x2 = (-b - q)/(2*a);
          printf ("X1=%f   X2=%f\n", x1, x2);
                  else if (d == 0) 
                   {
                                           /* there’s only one result */
                x1 = -b/(2*a);
                printf ("X1=X2=%f\n", x1);

                 } else 
                   {
                                           /* results are complex numbers */
                q = pow(-d, 1./2);
                x1r = -b/(2*a) ;
                x2r = x1r;
                x1i = q/(2*a);
                x2i = -x1i;

                printf ("X1 = (%f,%f)\n", x1r, x1i);
                printf ("X2 = (%f,%f)\n", x2r, x2i);
           }
}

Read more

Nested if else means if else into if else i.e. multiple if else statements included in one if else statement.Below is the program demonstrating if else :


/* A demo of nested if-else */
main( )
{
int m ;
printf ( "Enter either 0 or 1" ) ;
scanf ( "%d", &m ) ;
if ( m == 0 )
printf ( "this is zero" ) ;
else
{
if ( m == 1 )
printf ( "this is one" ) ;
else
printf ( "wrong choice" ) ;
}
}

Read more

The if account by itself will assassinate a individual statement, or a accumulation of statements, if the announcement afterward if evaluates to true. It does annihilation if the announcement evaluates to false.

SIMPLE IF ELSE PROGRAM :



/* Calculation of gross salary */
main( )
{
float base, gross, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &base ) ;
if (base < 2000 )
{
hra =base * 20 / 100 ;
da = base * 80 / 100 ;
}
else
{
hra = 100 ;
da = base * 90 / 100 ;
}
gross = base + hra + da ;
printf ( "gross salary = Rs. %f", gross ) ;
}

Read more

main( )
{
int n ;
printf ( "Enter a number less than 20 " ) ;
scanf ( "%d", &n ) ;
if ( num <= 20 )
printf ( "THIS IS AN ACCURATE PROGRAM" ) ;
}

Read more

The keyword if tells the compiler that what follows is a alarm ascendancy instruction. The action afterward the keyword if is consistently amid aural a brace of parentheses. If the condition, whatever it is, is true, again the account is executed. If the action is not accurate again the account is not executed.

Various conditional operators used for if statement are as follows:


  • Greater than(>)
  • Less than(<)
  • Equal to(= =)
  • Greater than equla to(>=)
  • Less than equal to(<=)
  • Not equal to(!=)

Read more

Many a times,we would like a set of instructions to be accomplished in situation, and an absolutely altered set of instructions to be accomplished in addition situation. This array of bearings is dealt in C programs application a alarm ascendancy instruction.various decision control stements in C are as follows:


  • The if statement 
  • The if-else statement
  • The conditional operators

Read more

There are fundamentally three types of instructions in C:


  1. Type Acknowledgment Instruction
  2. Arithmetic Instruction
  3. Control Instruction

The purpose of anniversary of these instructions is accustomed below:
1) Type acknowledgment instruction
To acknowledge the blazon of variables acclimated in a C program.
2)Arithmetic instruction
To accomplish addition operations amid con-stants & variables.
3)Control instruction
To ascendancy the arrangement of beheading of assorted state-ments in a C program.

Read more

To accomplish the affairs accepted the affairs itself care to ask the user to accumulation the ethics of p, n & r through the keyboard in the coursework of execution. This can be accomplished application a action alleged scanf( ). This action is a counter-part of the printf( ) function. printf( ) outputs the ethics to the awning admitting scanf( ) receives them from the keyboard. This is illustrated in the affairs apparent below:


/* Calculation of simple interest */
main()
{
int a, b;
float c, si ;
printf ( "Enter values of a, b, c" ) ;
scanf ( "%d %d %f", &a, &b, &c ) ;
si = (a * b * c )/ 100 ;
printf ( "%f" , si ) ;
}

Read more

Our first C program would simply calculate simple interest for a set of values representing principle, number of years and rate of interest.

/* Calculation of simple interest */
main( )
{
int a, b;
float c, si ;
a = 1000 ;
b = 3 ;
c = 8.5 ;
/* formula for simple interest */
si = (a * b * c) / 100 ;
printf ( "%f" , si ) ;
}

Read more

Variable names are names accustomed to locations in memory. These locations can accommodate integer, absolute or appearance constants. In any language, the categories of variables that it can abutment depend on the categories of constants that it can handle. This is because a specific blazon of capricious can authority alone the aforementioned blazon of constant. For example, an accumulation capricious can authority alone an accumulation constant, a absolute capricious can authority alone a absolute connected & a appearance capricious can authority alone a appearance constant.

Read more


There are basically three data types in c language:

1) Constants-
A constant is an entity that doesn’t change whereas a variable is an entity that may change.


Types of C Constants:
C constants can be divided into two major categories:
(a) Primary Constants
These are again divided into three types as:

  •  Real constants
  •  Integer constants
  •  Character constants

(b) Secondary Constants
These are again further divided into :

  • Arrays
  • Pointers
  • Structures
  • Union etc

Read more

    alphabets
    A, B, ….., Y, Z
    a, b, ……, y, z
    digits
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    special symbols
‘ ! @ # % ^ & * ( ) _ - + = | \ { }
[ ] : ; " ' < > , . ? /

Read more

C is a programming accent developed at AT & T's Bell Laboratories of USA in 1972. It was advised and accounting by a man called Dennis Ritchie. In the backward seventies C began to alter the added accustomed languages of that time like PL/I, ALGOL, etc. No pushed C. It wasn't fabricated the official Bell Labs language. Thus, after any advertisement C's acceptability advance and its basin of users grew. Ritchie seems to accept been afraid that so lots of programmers adopted C to earlier languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that is what happened.

Read more

Powered by Blogger.