Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Showing posts with label C TUTORIAL. Show all posts
Showing posts with label C TUTORIAL. Show all posts

Tuesday, 14 November 2017

What is Recursion in C Language with Example


Recursion in C

Recursion in  C Language

When function calling itself is called recursion in c programming language. In other words, When function is call in the same function is known as recursion in c language.

The function which calls the same function is called recursive function in C.

Recursive functions is quite useful in any mathematical calculation for solving a problem e.g useful in calculate factorial of any number, perform fibonacci series, etc.

After explaining recursion in c, Let's see the syntax of recursion in c.

Syntax:

function()
{
function();//calling itself, known as recursion or recursive function
}

Let's talk about some advantages and disadvantages of recursion in c language.


Advantages of Recursion in C

There are some advantages of using recursion in c language.
  • Recursion will be useful when same kind of work has to be continued for a finite no input or time.
  • We can reduce the length of the code by using recursion in c.
  • Recursion is very useful for solving data structure problems.

Disadvantages of Recursion in C

There are some disadvantages of recursion in c language.
  • It requires more memory because recursive calls along with automatic variables are stored on the stack.
  • Recursive solution is always logical and it is very difficult to trace i.e debug and analyse.

Now let's explain recursion in c with examples.

Factorial using recursion in c

This is the simple factorial program with recursion in c language.

#include<stdio.h>
#include<conio.h>
int fact(int);

void main()
{
int number, a;
clrscr();
printf("Enter number");
scanf("%d", &number);

a = fact(number);
printf("Factorial is %d\n", a);
getch();
}

int fact(int x)
{
int f;
if(x ==1)
return 1;
else
f = x*fact(x - 1);//recursion performed
return f;
}
}

Output: Enter number 5
             Factorial is 120


Fibonacci Series Program in C with Recursion

This is simple fibonacci series program in c by using recursion function.

#include<stdio.h>
int fibonacci(int);

main()
{
int n, c, i = 0;
scanf("%d", &n);
printf("febonacci series \n");

for(c = 1; c <= n; c++)
{
printf("%d\n", febonacci(i));
i++;
}

return 0;
}

int febonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else 
return (febonacci(n-1) + febonacci(n-2));
}

Here we discussed what is recursion in c language with examples and their advantages and disadvantages.

Check other C tutorial articles e.g data types in c language and printf() and scanf() function in c.





Share:

How to Write Comments in C Programming


Comments in C Programming Language

Comments in C Language

We are going to discuss, How to write comments in c programming language with step-by-step with simple examples. Let's start...


What is the purpose of comments in program?

Basically comments are used for description of every line of code which is written so that the programmer and other user can understand the meaning of each line of code in program.

Comments are mostly used for documenting of code.

The particular area of comments where we used are totally ignored by the compiler and its output does not print on the console.


Types of Comments in C Language

There are two types of comments in c language and these are...

  1. Single Line Comments
  2. Multiple Line Comments

Single Line Comments in C Language

We can use double slash ( // ) for representing single line comments in c language.

Syntax:

//this is my first single line comment in c program

Single Line Comments Example

This is single line comments in c program with output.

#include<stdio.h>
#include<conio.h>
void main()
{
//Variables declaration for addition
int a = 40;
int b = 40;
int c;
clrscr();

printf("value of a : %d\n", a);
printf("value of b : %d\n", b);

c = a+b;
printf("value of c : %d", c);
getch();
}

Output: value of a : 40
             value of b : 40
             value of c : 80


Another Example of Single Line Comments in C

This is another single line comment example where we will use more than one single line comments. You can use any number of single line comments at any place.

//single line comments in c
#include<stdio.h>
#include<conio.h>
void main()//main method
{
int a = 30;//variable declaration
clrscr();
printf("value of a : %d", a);
getch();
}

Output: value of a : 30


Multiple Line Comments in C Language

We can use slash asterisk (/*......*/) for representing multiple line comments in c language.

Syntax:

/* This is my 
      first multiple line comments
      in c language. */

            or

/* multiple line comments */

Multiple Lines Comments Example

This is multiple line comments example in c language.

#include<stdio.h>
#include<conio.h>
void main()/* main function */
{
/* variable declaration 
     a variable
     b variable 
     c variable */
int a = 40;
int b = 20;
int c;
clrscr();

c = a+b;
printf("addition of two number : %d ", c);
getch();
}

Output: addition of two number : 60

We cannot define comments into another comments i.e nested comments are not allowed in c.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
/* 
   /* nested comments not allowed */

*/
printf("nested not allowed");
getch();
}

Output: compile time error

Here we discussed, What is comments in c and the advantages of using comments in programming and how to write comments in c programming.

Learn how to run first program in c







Share:

Sunday, 12 November 2017

How to Use Pointers in C

Pointer in C Programming

In this article, We are going to learn what is a pointer and how to use pointers in c language with simple examples so that you can easily understand the pointer concept in c programming language.

Let's start with the definition of the pointer in c programming language and then take a lot of examples of c pointer to understand better.


What is Pointer in C Language?

Pointer is a special type of variable which stores or hold the address of another variable. Every variable has a particular address in a memory. We can know the address of variable and pointer easily by using some specifiers.


Advantages of Pointers in C

There are some advantages of pointer concept in c language.
  • Pointer reduces the length of the code and improves the performance because it can access address of a variable directly.
  • Pointer increases the processing speed.
  • Pointer saves memory.
  • Using pointer, We can return multiple values from any function.
  • Using pointer, You can access any memory location.

Symbols in C Pointer

There are some symbols or operator which is used in c pointer concept.

(1) Symbol - &(ampersand sign)

The name of this symbol is "address of operator" which is used to display the address of a variable.

(2) Symbol - *(asterisk sign)

The name of this symbol "indirection operator" which accesses the value at the address.


Address of Operator - &(ampersand sign)

In this example, We will print the address of variable 'x' by using the address of operator i.e & and there we will use %u format specifier for displaying the address of a variable.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 50;
clrscr();
printf("Value of variable x is : %d\n ", x);//print value of x i.e 50
printf("Address of variable x is : %u ", &x);//print address of variable x
getch();
}

Output: Value of variable x is : 50
             Address of variable x is : 65524

                                                                 OR

You can also use %p format specifier for display variable's address in hex format.

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 50;
clrscr();
printf("The value of variable x is : %d\n ", x);//print 50
printf("Address of variable x is : %p", &x);//print address in hex format
getch();
}

When you will convert 65524 (decimal) to hex number, it will give you FFF4 in result. 

Now we are able to know the address of any variable and now it is time to know how to hold or store these variable's address into another variable, for this we will use the pointer in c programming language.


How to declare pointers in C?

By using *(asterisk sign) we can declare pointers in c language.

For example:

int *a;//pointer to integer type

char *b;//pointer to character type

double *c;//pointer to double type

float *d;//pointer to float type

Let's understand pointer in c language with example.


Example of Pointer in C

In this example, We will store the address of a variable 'x' into pointer variable '*p" and then print the address and value of a variable by using pointer variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 400;//variable x
int *p;//pointer variable
clrscr();
p = &x;//store the address of x variable 

printf("Value of variable x : %d\n ", x);//print 400
printf("Address of variable x : %p\n ", &x);//print address of x
printf("Address of variable p : %p\n ", p);//print address of p
printf("Value of variable p : %d ", *p);//print value of p

getch();

}
}

Output: Value of variable x : 400
             Address of variable x : FFF4
             Address of variable p : FFF4
             Value of variable p : 400

Note: The data type of both variable(x) and pointer(*p) variable must be same. 

For example: 

int x = 50;//integer data type 
int *p;//integer data type


Another example of Pointer in C

Pointer in C programming

In the above image, Here 'a' is a simple variable with value 40 and with memory address fff4 and 'p' is a pointer variable which stores and holds the address of variable 'a' i.e fff4. Here fff2 is the memory address of pointer variable.

By using the address of a variable 'a' into pointer p we can print the address and value of variable 'a'.

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 40;
int *p;
clrscr();
p = &a;
printf("value of a : %d\n ", a);
printf("address of a : %x\n", &a);
printf("address of p : %x\n", p);
printf("address of p : %x\n", &p);
printf("value of p : %d ", *p);

getch();
}
}

Output: value of a : 40
             address of a : fff4
             address of p : fff4
             address of p : fff2
             value of p : 40

You can use %x format specifier for display address of the variable.


Null Pointer in C

A pointer that is not assigned any value but NULL is known as a Null pointer in c programming language and this done at the time of variable declaration.

For example:

int *p = NULL;


In this article, We discussed what is pointer in c programming language and how to use pointers in c and what is null pointers with simple and easy examples. 

Share:

Tuesday, 7 November 2017

Data Types in C Language with Examples

Data Types in C Language

Now, Here we are going to discuss data types in c language with examples. 

Data type tell us which type of data or value we can store in a memory by the help of variables e.g integer type value, character type value, float type value, double type value, etc.

In other words, We can say that the data type is a keyword which identified the type of data and store the input of a program into a memory(Ram) by the help of variables.

For example: Declaration of data type

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 50;
}

Here 'int' is a data type and 'a' is variable with value 50 of integer type. 


Data Types in C

There are different types of data types in c language which are given below.


Data Types in C Language

(1) Primitive or Primary Data Type

Some primitive data types which is int, char, float, double, and void.

(2) Derived Data Types

Some derived data types in c which is pointer, array, references, function.

(3) User - defined Data Types

Some user - defined data types in c which are structure, union, enumeration.


Primitive Data Types - Size and Range

The primitive data type contains integer and floating based data types. C language supports both signed and unsigned literals.

Let's understand first signed and unsigned then size and range of primitive data types.

Signed  : Signed keyword allows both negative and positive values and this is the default properties or data type modifier for every data type.

For example: 

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 20;
int b = - 30;
signed int c = 40;
signed int d = - 50
}

Above all the data types declaration are valid declaration.

Unsigned : Unsigned keyword allows only positive value, It does not supports negative values.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int b = 30;//valid declaration
unsigned int c = - 30;//invalid declaration 
}

The memory size of primitive data types can be change according to 16 or 32 or 64 bit operating system.

Data Type                         Memory Size               Range
char                                  1 byte                          - 128 to 127
signed char                      1 byte                          - 128 to 127
unsigned char                  1 byte                          0 to 255
short                                 2 byte                          - 32,768 to 32,767
signed short                     2 byte                          - 32,768 to 32,767
unsigned short                 2 byte                          0 to 65,535
int                                     2 byte                           32,768 to 32,767
signed int                         2 byte                           32,768 to 32,767
unsigned int                     2 byte                           0 to 65,535
short int                           2 byte                            32,768 to 32,767
signed short                     2 byte                            32,768 to 32,767           
unsigned short int           2 byte                             0 to 65,535
long int                             4 byte           - 2,147,483,648 to 2,147,483,647
signed long int                 4 byte           - 2,147,483,648 to 2,147,483,647
unsigned long int             4 byte           0 to 4,294,967,295
float                                  4 byte
double                               8 byte
long double                       10 byte

This is the table of data types in c with size and range.


Declaration of some data types with variables

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 50;//positive integer data type
int aa = -48;//negative integer data type
char c = 'b'
float f = 3.1;
float ff = - 5.8
long l = 56850;
long ll = - 452658;
short s = 569;
short ss = - 569;
double d = 3.247895233
}


Data Types in Programming

This is simple c program where we will declare integer data type with variable and store value and print that value to console.

#include<stdio.h>
#include<conio.h>
void main()
{
int i = 7009;//integer data type with i variable
clrscr();
printf("Number is : %d ", i);
getch();
}

Output: 7009


Usage of sizeof Operator 

By using sizeof operator you can get size of type e.g int, char, float, etc.

For example:

#include<stdio.h>
#include<limits.h>
int main()
{
printf("Storage size for int : %d ", sizeof(int));

return 0;
}


Here we have learned data types in c language with examples.

Share:

Monday, 6 November 2017

Printf and Scanf Function in C Language

What is printf and scanf in C Language

printf and scanf in c language

After learning the concept of types of variables in c language, Here we are going to discuss printf and scanf function in c language with simple examples.

Let's understand what is use of printf and scanf in c language with simple and easy examples.


What is printf() in c ?

In c programming language, printf() is a function which is defined in "stdio.h" header file. 

C printf() function is used  to display output on the console. By using printf() function we can print any massage or data on the console. 

Syntax 1: For printing massage only on the console

printf("hello world");//It will print same massage whatever you write in double quotes(" ").

Syntax 2: For printing massage and data(value) on the console

printf("Format specifiers", Value1, Value2...);

Here format specifiers can be %d(integer), %c(character), %s(string), %f(float), %d(double) and printf() function can take any number of arguments i.e Value1, Value2, Value3, but separated with comma(,).

For example1:

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 30;
clrscr();
printf("Hello World\n");
printf("Number is %d ", a);
getch();
}

Output: Hello World
             Number is 30

In the above example, We have printed simple "Hello world" massage and print the data of "a" by using format specifier %d in printf() function.

For example 2:

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

void main()
{
int a = 30;
float f = 5.2;
clrscr();
printf(" C programming \n ");
printf(" Value of integer only : %d  \n" , a);
printf(" Value of float only : %f  \n" , f);
printf("Both Values : %d %f " , a, f);
getch();
}

Output: C programming
             Value of integer only : 30
             Value of float only : 5.200000
             Both Values : 30 5.200000


What is scanf() in c ?

In c programming language, scanf() is also a function which is defined in "stdio.h" header file.

C scanf() function is used to read the input data from console or keyboard

Syntax: 

scanf("Format specifiers", &Value1, &Value2...);

For example 1:

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("What is your age :");//print massage on the console
scanf(" %d ", &age);//taking number from console and store in age variable 
printf("My age is : %d ", age);//print the age's value on console
getch();
}

Output: What is your age : 50
              My age is : 50



For example 2:

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

void main()
{
int a;
float f;
clrscr();
printf("Enter your numbers: ");
scanf("%d %f " , &a, &f);
printf("Your numbers : %d %f ", a, f);
getch();
}

Output: Enter your numbers: 4 5.2
             Your numbers : 4 5.200000


Printf and Scanf Example : Sum of 2 Numbers

This is the simple example of using printf() and scanf() function for addition of two number. We will take input or number from console and add them.

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

void main()
{
int a;
int b;
int sum;
clrscr();
printf("Enter your first number : ");
scanf("%d", &a);
printf("Enter you second number : ");
scanf("%d", &b);

sum = a+b;
printf("addition of 2 number is : %d ", sum);
getch();
}

Output: Enter your first number : 6
             Enter your second number : 5
             addition of 2 number is : 11


Difference between printf and scanf in C language

The difference between printf and scanf in c language is printf() function is used to display the output on the console e.g massage or data values and scanf() function is used to read the input data from console or keyboard.

This your printf and scanf in c language with simple examples. I hope it will be useful to you.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Link

Translate