Introduction to C

C is a general-purpose programming language that was developed in the early 1970s by Dennis Ritchie at Bell Labs. It is widely used for system programming, developing operating systems, and embedded systems due to its efficiency and control over system resources.

C is a procedural programming language, meaning it follows a step-by-step approach to solve problems, where the program is structured as a sequence of procedures or functions.

C is case-sensitive language, meaning that uppercase and lowercase letters are treated as distinct characters.

C follows top-down approach, where the program is broken down into smaller functions.

C programs are typically compiled into machine code, which makes them very fast and efficient.

Structure of C Program

Documentation Section
Files Inclusion Section
Definition Section
Global Declaration Section
int main() {
    // Local Declaration Section
    // Executable Statements
    return 0;
}
Sub-Functions Section
Documentation Section

This section contains comments and documentation about the program. For comments, use // for single-line comments and /* */ for multi-line comments. Comments are not executed by compiler.

Files Inclusion Section

This section includes header files that provide necessary declarations and functions for the program. For example, #include <stdio.h> is used to include the standard input-output library.

Here, # is called the preprocessor.

Use #include, also import existing C-programs. For example, create following files in a same working directory:

variables.c
int num1=20;
int num2=10;

add.c
#include<stdio.h>
#include "variables.c"
int main(){
    printf("%d", num1+num2);
    return 0;
}

multiply.c
#include<stdio.h>
#include "variables.c"
int main(){
    printf("%d", num1*num2);
    return 0;
}
Definition Section

This section is used to define constants and macros using #define directive.

Syntax:
#define MACRO_NAME(or)CONSTANT_NAME expression(or)value

Where, MACRO_NAME or CONSTANT_NAME is the name of the macro or constant being defined, and expression or value is the value or expression to be substituted than only you can evaluate.

For Example:
#include<stdio.h>
#define SQUARE(x) x*x
int main(){
    printf("%d", SQUARE(2+3));
    return 0;
}
Output:
11

Explanation: In the example, the macro SQUARE(x) expands to x*x. When SQUARE(2+3), x is 2+3, is called, it becomes 2+3*2+3, which evaluates to 11.

Global Declaration Section

This section is used to declare global variables and function prototypes that can be accessed throughout the program.

Main Function

This is the entry point of the program where execution begins. The main function typically returns an integer value to indicate the program's success or failure.

  • Program executes successfully, return 0;
  • Program encounters an error, return non-zero value;

We can use the following arguments for main, if needed:

  • int argc: argc - argument count. It represents the number of command-line arguments passed to the program.
  • char *argv[]: argv - argument vector. It is an array of strings that holds the actual command-line arguments.
  • char *envp[]: envp - environment pointer. It is an array of strings that holds the environment variables.
Local Declaration Section

This section is used to declare local variables that are only accessible within the main function or other functions.

Executable Statements

This section contains the main logic of the program, where the actual operations and computations are performed.

Sub-Functions Section

This section contains additional functions that are defined outside the main function to perform specific tasks.


Common Interview Questions

  1. What is the difference between #include<filename> and #include "filename"?

    #include<filename> - It searches directories at pre-designated by the compiler.
    #include "filename" - It searches in the same directory as the file containing the directive.

  2. What is the default argument of main() in C?

    void

  3. What is the use of return statement in main() function?

    A return statement in main() function tells to compiler about type of termination. The return value is 0(zero) means normal termination and return value is non-zero means abnormal termination.

  4. Is it possible to create a C program without main() function?

    Yes, it is possible to create C program without main() function but you can't execute it.

  5. Is it possible to execute user defined functions before and after main() function in C?

    #include<stdio.h>
    void before_main();
    void after_main();
    #pragma startup before_main
    #pragma exit after_main
    void before_main(){
        printf("Before main() function.\n");
    }
    void after_main(){
        printf("After main() function.\n");
    }
    int main(){
        printf("Inside main() function.\n");
        return 0;
    }
    Note:The above code executes in Turbo C compiler, doesn't in GCC compiler.