typedef
  • We can define data type names by using the typedef keyword.
  • A typedef does not create a new data type but rather defines a new name for an existing type.
  • This process can help to make machine-dependent programs more portable.

Syntax:

                typedef existing_type new_name;

                where existing_type is any valid data type, and new_name is the new name for this particular data type.

  • The new name we define here is in addition to, not a replacement for, the already existing data type name.

          Example:

                               typedef float amount;

                               Here, typedef is creating a new name for float, and this statement tells the compiler to recognize the amount as another name for float.

  • Next, we can create a float variable using amount:

          Example:

                               amount over_draft;

                               Here, over_draft is a floating-point variable of the type amount, another word for data type float.

  • Now this defined amount can be used in another typedef.

           Example:

                           typedef amount over_due;

Program:

#include<stdio.h>
int main()
{
    typedef int price;
    price i,j;
    i=100;
    j=500;
    printf("Value of i is: %d",i);
    printf("\nValue of j is: %d",j);
    return 0;
}
Output:
Value of i is: 100
Value of j is: 500
Console I/O

         In C language, the Console Input and Output functions are classified into two categories:

  • Formatted console I/O functions
  • Unformatted console I/O functions

           1)Formatted console I/O functions:

                      Formatted console I/O functions are:

                                (a) printf()

                                (b) scanf()

  • These functions perform formatted output and input— that is, they can read and write data in various formats that are under your control.
  • printf( ) writes data to the console.
  • scanf( ) is the complement of printf(). It reads the data from the keyboard.
  • We can operate both functions on any of the built-in data types, plus null-terminated character strings.

           2)Unformatted console I/O functions:

                      Unformatted console I/O functions are:

  • getchar( ):

                               It reads a character from the keyboard; usually waits for carriage return.

  • getche( ):

                               It reads a character with echo; it does not wait for carriage return; it is not defined by Standard C but a common extension.

  • getch( ):

                               It reads a character without an echo; it does not wait for carriage return; it is not defined by Standard C but a common extension.

  • putchar( ):

                               It writes a character to the screen.

  • gets( ):

                               It reads a string from the keyboard.

  • puts( ):

                               It writes a string to the screen.

Typecasting
  • Typecasting enables us to convert one data type into another.
  • It is also known as Type-conversion.
  • We use the cast operator to typecast, which is denoted by (type).
  • Type conversion can be automatically done by the compiler implicitly or specified explicitly by using the cast operator.
  • Using the cast operator is considered a good practice whenever type conversion is necessary.

          Syntax:

                           (type_name) expression;

Example:

#include<stdio.h>
int main()
{
    int x=5, y=10;
    float sum;
    sum=(float)x + y;
    printf("Sum of x and y is: %f\n", sum);
}
Output:
Sum of x and y is: 15.000000

In the above program, the sum of two integers is typecast and stored in a floating type.

Recursion
  • In C language, a function can call itself. In this case, the function is called recursive.
  • Recursion is defining something in terms of itself and is sometimes called circular definition.
  • It reduces unnecessary calling of function.
  • Recursive functions are helpful while solving many mathematical problems like calculating the Factorial of a number, generating the Fibonacci series, etc.

Example:

Calculating the Factorial of a number:

#include<stdio.h>
int factorial(int i)
{
    if(i <= 1)
    {
        return 1;
    }
    return i * factorial(i-1);
}
int main()
{
    int i = 7;
    printf("Factorial of %d is %d\n", i, factorial(i));
    return 0;
}
Output:
Factorial of 7 is 5040
Generating Fibonacci series:
#include<stdio.h>
int fibonacci(int i)
{
    if(i==0)
    {
        return 0;
    }
    if(i==1)
    {
        return 1;
    }
    return fibonacci(i-1) + fibonacci(i-2);
}
int main()
{
    int i;
    for(i=0; i<6; i++)
    {
        printf("%d\t\n", fibonacci(i));
    }
    return 0;
}

Output:

0
1
1
2
3
5

Who is the course for?

Prerequisites before starting this course:

Questions 🙵 Answers.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

More Courses

You might also be interested in these courses