LearnFrenzy
Sign in Take a free test

C Programming · Topic 1 of 23

Declarations and Initializations

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

Coding Techniques

Declarations and Initializations · Study material

Designated initializers for lookup tables

Indexing a table by enum constants ties each entry to its value, so reordering or adding enum members cannot silently shift the table.

#include <stdio.h>

enum color { RED, GREEN, BLUE, COLOR_COUNT };

static const char *const names[COLOR_COUNT] = {   /* each entry is tied to its enum value */
    [BLUE] = "blue",
    [RED] = "red",
    [GREEN] = "green",
};

int main(void) {
    for (int c = 0; c < COLOR_COUNT; c++)
        printf("%d=%s ", c, names[c]);
    printf("\n");
    return 0;
}

Output

0=red 1=green 2=blue

Declare in headers, define in one source file

/* config.h – included everywhere */
extern int max_connections;          /* declaration */
int connect_all(int limit);          /* prototype   */

/* config.c – exactly one definition */
#include "config.h"
int max_connections = 64;
int connect_all(int limit) { /* … */ return limit; }

Putting int max_connections = 64; in the header would define it in every file that includes the header – a "multiple definition" link error.

One declarator per line, initialised at once

int *head = NULL;        /* clear: one pointer, already valid */
int *tail = NULL;
size_t count = 0;

int* a, b;               /* misleading: only a is a pointer */

Declaring each variable where it is first used, with a value, prevents both "garbage" bugs and pointer-declarator surprises.

Good declaration habits

  • Initialise every variable in its declaration.
  • Use designated initializers for structs with more than two members.
  • Use {0} (or C23 {}) to zero an aggregate.
  • Mark read-only data const and file-private globals static.
  • Use volatile only for hardware registers and signal-handler flags – it is not a threading tool.
  • Prototype every function; write (void) for no parameters.

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