LearnFrenzy
Sign in Take a free test

C Programming · Topic 4 of 23

Functions

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Programs & Examples

Functions · Study material

Fast power (exponentiation by squaring)

Square the base and halve the exponent each step: about log₂(exp) multiplications instead of exp.

#include <stdio.h>

long long power(long long base, unsigned exp) {     /* exponentiation by squaring: O(log exp) */
    long long result = 1;
    while (exp > 0) {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        if (exp > 0)
            base *= base;
    }
    return result;
}

int main(void) {
    printf("%lld %lld %lld\n", power(2, 10), power(3, 13), power(7, 0));
    return 0;
}

Output

1024 1594323 1

Tower of Hanoi

The classic recursion: move n − 1 disks aside, move the largest, move the n − 1 disks back. n disks need 2ⁿ − 1 moves.

#include <stdio.h>

void hanoi(int n, char from, char to, char via, int *moves) {
    if (n == 0)
        return;
    hanoi(n - 1, from, via, to, moves);      /* move n-1 disks out of the way */
    printf("move disk %d %c -> %c\n", n, from, to);
    (*moves)++;
    hanoi(n - 1, via, to, from, moves);      /* put them back on top */
}

int main(void) {
    int moves = 0;
    hanoi(3, 'A', 'C', 'B', &moves);
    printf("total moves: %d\n", moves);
    return 0;
}

Output

move disk 1 A -> C
move disk 2 A -> B
move disk 1 C -> B
move disk 3 A -> C
move disk 1 B -> A
move disk 2 B -> C
move disk 1 A -> C
total moves: 7

Variadic function with stdarg.h

Functions like printf() take a variable number of arguments. The function must learn how many (here from count) and of which type.

#include <stdarg.h>
#include <stdio.h>

double average(int count, ...) {             /* "..." = any number of extra arguments */
    va_list ap;
    va_start(ap, count);
    double sum = 0;
    for (int i = 0; i < count; i++)
        sum += va_arg(ap, int);              /* you must know each argument's type */
    va_end(ap);
    return count > 0 ? sum / count : 0.0;
}

int main(void) {
    printf("%.2f %.2f\n", average(3, 70, 85, 90), average(4, 1, 2, 3, 4));
    return 0;
}

Output

81.67 2.50

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links