Wednesday, 31 July 2019

How to write a program to find the factorial of a given number?

#include<stdio.h>
#include<conio.h>
void main()
{
  int i, fact=1, num;
  printf("Enter the number/n");
  scanf("%d", &num);
    if(num=0)
           fact=1
    else
        {
            for(i=1;i<=num;i++)
     {
         fact*i
}
}
   printf("factorial of number%d=%5 d/n",num, fact);
getch();
}

What is conditional compilation? How does it help a programmer?

               Conditional Compilation
There may be situations when we went to compile some parts of the code based on some condition. We know that before compilation the source code passes through the preprocessor to supply only some parts of the code to the compiler for compilation.
These conditions are checked during the preprocessing phase. The directive used in conditional compilation are:
#ifdef
#ifdef
#if
#else

Saturday, 27 July 2019

ANSI Standard Libraries


1.   stdio.h: I/O functions:


a.    getchar() returns the next character typed on the keyboard.


b.    putchar() outputs a single character to the screen.


c.    printf() as previously described


d.    scanf() as previously described


2.    string.h: String functions


a.    strcat() concatenates a copy of str2 to str1


b.    strcmp() compares two strings


c.    strcpy() copys contents of str2 to str1


3.    ctype.h: Character functions


a.    isdigit() returns non-0 if arg is digit 0 to 9


b.    isalpha() returns non-0 if arg is a letter of the alphabet


c.    isalnum() returns non-0 if arg is a letter or digit


d.    islower() returns non-0 if arg is lowercase letter


e.    isupper() returns non-0 if arg is uppercase letter


4.    math.h: Mathematics functions


a.    acos() returns arc cosine of arg


b.    asin() returns arc sine of arg


c.    atan() returns arc tangent of arg


d.    cos() returns cosine of arg


e.    exp() returns natural logarithim e


f.     fabs() returns absolute value of num


g.    sqrt() returns square root of num


5.    time.h: Time and Date functions


a.    time() returns current calender time of system


b.    difftime() returns difference in secs between two times


c.    clock() returns number of system clock cycles since program execution


6.    stdlib.h:Miscellaneous functions


a.    malloc() provides dynamic memory allocation, covered in future sections


b.    rand() as already described previously


c.    srand() used to set the starting point for rand()


How to write a program in c to check whether the given number is odd or even?

#include<stdio.h>
#include<conio.h>
void main()
{
   int num;
  clrscr();
  printf("Enter any number ");
  scanf("%d",&num);
   if(num%2==0)
      printf("%d is even.",num);
  else
       printf("%d is odd.",num);
    getch();
}

Output
  Enter any number: 5
     5 is odd.

What is preprocessor?

The preprocessor is not a part of c compiler. It is an unique feature of c language. Preprocessor is a program that processes the code before it passes through the compiler. Preprocessor makes the program easy to read, modify, portable and more efficient by providing their tools.

Preprocessor directives-

Directives                       Function
#define          defines a macro substitution
#undef           undefines a macro
#include        specifies a file to be included
#ifdef            tests for macro definition
#endef          specifies the end of #if
#ifendef        tests weather the macro                               
                     is not def
#if              tests a compile time condition
#else       specify alternatives when # if  

                 test fail

Thursday, 25 July 2019

What is header files?

                        Header File

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.

Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.

A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required.

What is function?

                         Function

The group of instructions and statement
which perform any task togather.
Every c program has at least one function, which is main(),  all the most trivial programs can define additional function.

Defining aFunction

The general form of a function definition in C programming language is as follows:

return_type function_name( parameter list )


{

body of the function

}

A function definition in C programming consists of a function header and a function body. Here are all the parts of a function:

 

o   Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

 

o   Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.

 

 

o   Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

 

o   Function Body: The function body contains a collection of statements that define what the function does.

 

 

Example

Given below is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum value between the two:

 

/* function returning the max between two numbers */


int max(int num1, int num2)

{

/* local variable declaration */

int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

Saturday, 13 July 2019

What is array?

Arrays a type of data structure that can store a fixed-size sequential collection of elements of the same type.
We can't take any array with a half of data types.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

The bracket [  ] denotes to the compiler that for an array.

The declaration of array -

Data_type <array name>[expression]
Ex-
         Int array[10]

Intialization of array
 
Int array[10]={1,5,8,9,6,0,3,4,2,7}

Types of array -

Arrays are important to C and should need a lot more attention. The following important concepts related to array should be clear to a C programmes.

 

Multidimensional arrays

C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.

Twodimensional array

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows:

type arrayName [ x ][ y ];


 

Where type can be any valid C data type and arrayName will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns.

What is pointers?

A pointer is a variable which denotes the address in the memory of another variable. 
When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it. 

#include <stdio.h>

int main ()

{

int var1;

char var2[10];

printf("Address of var1 variable: %x\n", &var1 );

printf("Address of var2 variable: %x\n", &var2 );

return 0;

}

 

include <stdio.h>

in the above code is compiled and executed, it produces the following result:

Address of var1 variable: bff5a400

Address of var2 variable: bff5a3f6




Application of pointer-

Accessing data by their address for example
Parameters are argument of function main. 

Null Pointer-

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program:

 

#include <stdio.h>

int main ()

{

int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;

}

 

When the above code is compiled and executed, it produces the following result:

 

The value of ptr is 0