C-Viva Q&A

Why is C known as a mother language?-1

Most of the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust, java script, etc. It introduces new core concepts like arrays, functions, file handling which are used in these languages.

C is a mid-level programming language?-2

C is called a mid-level programming language because it binds the low level and high -level programming language. We can use C language as a System programming to develop the operating system as well as an Application programming to generate menu driven customer driven billing system.

founder of C language?-3

Dennis Ritchie.

When C language developed?-4

Developed in 1972 at bell laboratories of AT&T.

Who is the main contributor in designing the C language after Dennis Ritchie?-5

Brain Kernighan.

What is a compiler?-6

Compile is a software program that transfer program developed in high-level language into executable object code.

What is syntax error VS LOGICAL ERROR?-7

Syntax Error

Syntax Errors occur when we violate the rules of writing the statements of the programming language.

Program fails to compile and execute.

Syntax Errors are caught by the compiler.

Logical Error

Logical Errors occur due to our mistakes in programming logic.

Program compiles and executes but doesn't give the desired output.

Logical errors need to be found and corrected by people working on the program.

IDE-8

Integrated Development Environment (IDE) The process of editing, compiling, running and debugging is managed by a single integrated application known as IDE

Algorithm-9

It is a method of representing the step by step process for solving a problem. Each step is called an instruction

FLOWCHART-10

A flowchart is a pictorial representation of an algorithm in which the steps are drawn, in the form of different shapes of boxes and the logical flow indicated by inter connecting arrows.

Meaning of a Flowchart

A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program

ALGORITHM VS FLOWCHART-11

ALGORITHM

Step by step procedure for solving a problem

Written in English-like language with words

Explains how a certain process is followed or a problem solved


FLOW CHART

Pictorial representation of an algorithm

Steps of algorithm are drawn in the form of shapes of boxes and logical flow with arrows

May not have detailed instructions about how the tasks are done.

Characteristics of algorithm-12

Finiteness:- It terminates with finite number of steps.

Definiteness:- Each step of algorithm is exactly defined.

Effectiveness:- All the operations used in the algorithm can be performed exactly in a fixed duration of time.

Input:- An algorithm must have an input before the execution of program begins.

Output:- An algorithm has one or more outputs after the execution of the program.

Structure of C PROGRAM-13

C program contains Documentation Section, Link Section, Definition Section, Global declaration Section, Main function and User-defined function. under main main function 1. Declaration Part, 2. Execution Part.

header file and its usage in C programming?-14

The file containing the definitions and prototypes of the functions being used in the program are called a header file. It is also known as a library file.

Example: The header file contains commands like printf and scanf is from the stdio.h library file

difference when the header file is included in double-quotes (“”) and angular braces (<>)?-15

The Header file is included within double quotes (“ ”), compiler search first in the working directory for the particular header file. If not found, then it searches the file in the include path.

The Header file is included within angular braces (<>), the compiler only searches in the working directory for the particular header file.

Can we compile a program without main() function?-16

Yes, we can compile, but it can’t be executed.

But, if we use #define, we can compile and run a C program without using the main() function.

For example:

#include<stdio.h>

#define start main

void start() {

printf(“Hello”);

}

KEYWORD?-17

Keywords are building blocks for program statements and have fixed meaning and these meaning cannot be changed.

What is a token?-18

The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:

  1. Identifiers: Identifiers refer to the name of the variables.

  2. Keywords: Keywords are the predefined words that are explained by the compiler.

  3. Constants: Constants are the fixed values that cannot be changed during the execution of a program.

  4. Operators: An operator is a symbol that performs the particular operation.

  5. Special characters: All the characters except alphabets and digits are treated as special characters.

What is an identifer?-19

Identifiers are user-defined names given to variables, functions and arrays.

maximum length of an identifier-20

It is 32 characters ideally but implementation specific.

basic data types associated with C?-21

Int – Represent the number (integer)

Float – Number with a fraction part.

Double – Double-precision floating-point value

Char – Single character

Void – Special purpose type without any value.

Why variable names beginning with underscore is not encouraged?-22

To avoid conflicts since library routines use such names

use of scanf() functions?-23

scanf () function can be used to get input into a program and it requires two arguments. First a format specifier defines the type of data to be entered, then the name of the variable in which the input will be stored. This scanf () function is responsible for giving input into the program.

use of printf() functions?-24

printf() function is used to display/output values of variable in the monitor.

The printf function has general form:

printf (“format specifiers”,variables)

difference between the local variable and global variable in C?-25

Declaration

Local variable

A variable which is declared inside function or block is known as a local variable.

Global variable

A variable which is declared outside function or block is known as a global variable.


Scope

Local variable

The scope of a variable is available within a function in which they are declared.

Global variable

The scope of a variable is available throughout the program.


Access

Local variable

Variables can be accessed only by those statements inside a function in which they are declared.

Global variable

Any statement in the entire program can access variables.


Life

Local variable

Life of a variable is created when the function block is entered and destroyed on its exit.

Global variable

Life of a variable exists until the program is executing.


Storage

Local variable

Variables are stored in a stack unless specified.

Global variable

The compiler decides the storage location of a variable.

auto keyword in C?-26

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.

Operators in C?-27

  1. Arithmetic Operator:

(+, -, *, /, % )

  1. Relational Operator:

(<, <=, >, >=, != )

  1. Logical Operator:

( !, &&, || )

  1. Assignment operator :

(+=, - =, *=, /=, %=, &=, |=, ^=, <<=, >>= )

  1. Bitwise Operator:

(~, &, |, ^ <<, >>)

  1. Conditional Operator:

(? :)

  1. Increment and decrement:

(++ and - - )

  1. Special Operator:

( ., ->, sizeof, &, *)

Types of Unary Operators-28

1. Unary minus(-)

2. Unary plus(+)

3. Increment(++)

  • Pre increment(++variable)

  • Post increment(variable++)

4. Decrement(– -)

  • Pre decrement(– -variable)

  • Post decrement(variable– -)

5. Logical Negation(!)

6. Address Operator(&)

7. sizeof() Operator

What is the difference between ++a and a++?-29

"++a” is called prefixed increment and the increment will happen first on a variable.

"a++is called postfix increment and the increment happens after the value of a variable used for the operations.


difference between = and == symbols in C programming?-30

‘==’ is the comparison operator which is used to compare the value or expression on the left-hand side with the value or expression on the right-hand side.


‘=’ is the assignment operator which is used to assign the value of the right-hand side to the variable on the left-hand side.


What is typecasting?-31

The typecasting is a process of converting one data type into another is known as typecasting. If we want to store the floating type value to an int type, then we will convert the data type into another data type explicitly.

Syntax

(type_name) expression;


implicit VS explicit-32

An implicit type conversion is automatically performed by the compiler when differing data types are intermixed in an expression.


An implicit type conversion is performed without programmer's intervention.


Example:

a, b = 5, 25.5

c = a + b

********************************************************************************************************************************************

An explicit type conversion is user-defined conversion that forces an expression to be of specific type.


An explicit type conversion is specified explicitly by the programmer.

Example:

a, b = 5, 25.5

c = int(a + b)

Flow Control statements in C-33

Control statements are heart of any programming language. It consists of 3 Types:

  • Decision statement is condition based statement. It define single or set of conditions that must be satisfied before statement/s can execute. Decision statements are also known as conditional or branching statement.

    1. Simple if statement

    2. if...else and if...else...if statement

    3. Nested if...else statement

    4. Switch...case

  • Looping statement defines a set of repetitive statements . These statements are repeated, with same or different parameters for a number of times. Looping statements are also known as iterative or repetitive statement. Looping statements are also known as iterative or repetitive statement. three types of looping statement in C.

  1. for loop

  2. while loop

  3. do...while loop


  • Jump statement provides unconditional way to transfer control from one part of program to other. C supports three jump statements.

  1. break

  2. continue

  3. goto

Entry controlled and Exit controlled loop-34

In Entry Controlled Loop, loop body is checked after checking the test condition i.e. condition is checked first after that loop body will execute.

Entry Controlled Loops are : for, while


**************************************************************************************

In Exit Controlled Loop, loop body will be executed first after that loop’s test condition is checked.

Exit Controlled Loop is : do while

Which loop is good for programming?-35

It depends on the programming need, we can not say which is best to use. Where condition checking is required before body execution, just use for or while. And when condition is not required to check before body execution, just use do while.

jumping statements in C Language?-36

In C Programming, some special statements are used to transfer program control to one location to other location. There are following jumping statements are used in c:

  1. goto

  2. break

  3. continue

  4. return


  1. goto

goto statement is used to jump program's control from one location to define label.


  1. break

break statement is used in switch and loop statements, it is used to break the execution of the switch and loop's body and transfer control after the loop/switch statement.


  1. continue

continue is used in looping statement, it transfer program's control in the starting of the loop's body.


  1. return

Generally return statement is used in function's body, it transfers program's control from called to calling function

continue statement without using loop?-37

No, continue statement can only be used within the loops only, it can be any loop while, do while or for. If we use continue statement without using loop , there will be a compiler error "misplaced continue".

Is nested loop possible?-38

Yes, it is possible. We can use loop with in the loop any number of times.

infinite loop?-39

A loop which is never finished is known as infinite loop, it means the looping condition is always true, so that loop never terminate. Infinite loop is very useful in embedded systems.

an array ?-40

Array is a data structure that hold finite sequential collection of homogeneous data.

  • Array is a collection - Array is a container that can hold a collection of data.

  • Array is finite - The collection of data in array is always finite, which is determined prior to its use.

  • Array is sequential - Array stores collection of data sequentially in memory.

  • Array contains homogeneous data - The collection of data in array must share a same data type.

Syntax:

data type array_ name[size];

How to initialize an array?-41

There are two ways to initialize an array.

  1. Static array initialization - Initializes all elements of array during its declaration.

Example: int marks[5] = {90, 86, 89, 76, 91};

Note: Size of array is optional when declaring and initializing array at once. The C compiler automatically determines array size using number of array elements. Hence, you can write above array initialization as.


  1. Dynamic array initialization - The declared array is initialized some time later during execution of program.

#include <stdio.h>

#define SIZE 10 // Size of the array

int main()

{

int index, marks[SIZE];


for(index = 0; index < SIZE; index++)

{

scanf("%d", &marks[index]);

}

String?-42

In C programming, a string is a sequence of characters terminated with a null character \0.

For example:

char c[] = "c string";


When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.

String handing function?-43

Many library functions are defined under header file <string.h>



FUNCTION NAME DESCRIPTION

strlen (string) It returns length of string


strlwr (string) It converts string into lowercase


strupr (string) It converts string into uppercase


strcat (string_1, string_2) It concatenates string_1 to string_2 and then store into string_1.


strncat (string_1, string_2, size) It concatenates string_1 to specific no. of

characters of string_2 and then store into string_1.


strcmp (string_1, string_2) It compares string_1 with string_2.

If both are equal then it returns 0.

If string_1 < string_2 then it returns –ve.

If string_1 > string_2 then it returns +ve.


strncmp (string_1, string_2, size) It compares string_1 with specific no. of characters of string_2.

If both are equal then it returns 0.

If string_1 < string_2 then it returns –ve.

If string_1 > string_2 then it returns +ve.


strcpy (string_1, string_2) It copies string_2 into string_1.


strncpy (string_1, string_2, size) It copies specific no. of characters of string_2 into string_1.


strchr (string, character) It searches first position of character in the string and prints the string from that character to the end.


strrchr (string, character) It searches last position of character in the string and prints the string from that character to the end.


strrev (string) It returns reverse of string.

function?-44

A function is a module or block of program code which deals with a particular task. Each function has a name or identifier by is used to refer to it in a program. A function can accept a number of parameter or values which pass information from outside.

Modular Programming?-45

If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms, if a complex problem is solved using more modules, to simply an application.

pointer?-46

A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. Whenever a variable is declared inside a program, then the system allocates some memory to a variable. The memory contains some address number. The variables that hold this address number is known as the pointer variable.


For example:


Data_type *p;


The above syntax tells that p is a pointer variable that holds the address number of a given data type value.

USES OF A pointer?-47

Pointer is used in the following cases

i) It is used to access array elements

ii) It is used for dynamic memory allocation.

iii) It is used in Call by reference

iv) It is used in data structures like trees, graph, linked list etc.

ARRAYS VS pointer?-48

Array

An array is a single, pre allocated chunk of contiguous element, fixed in size and location

Expression a[4] refers to the 5th element of the array a

Example int num[]={2,4,5}

They are static in nature. Once memory is allocated, it cannot be resized or freed dynamically.

****************************************************************************************************************************************************

Pointer

A pointer is a place in memory that keeps address of another place inside

Allows us to indirectly access variables. In other words,

Pointer can’t be initialized at definition array can be definition.

Pointer is dynamic in nature. The memory allocation can be resized or freed late

STRUCTURE?-49

Structure constitutes a super data type which represents several different data types in a single unit. A structure can be initialized if it is static or global.

ARRAY vs structure?-50

Array is a collection of homogeneous data types.

Array

  1. It is a collection of data items of same data type.

  2. It has declaration only

  3. There is no keyword.

  4. array name represent the address of the starting element.

********************************************************************************************************************************************************

Structure is a collection of heterogeneous data type

Structure

  1. It is a collection of data items of different data type.

  2. It has declaration and definition

  3. keyword struct is used

  4. Structure name is known as tag it is the short hand notation of the declaration.

UNION?-51

Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by allocating enough memory to hold the largest member. Here a single area of memory contains values of different types at different time. A union can never be initialized.

STRUCTURE VS UNION?-52

A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.

*********************************************************************************************************************************************************

A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.

difference among getch(),getche(),getchar()?-53

These all functions are used to get a single character from keyboard, but there are following differences.

getch() -It takes input, but not echo the character on console, but requires enter key.

getche() -It takes input, but not echo character and does not requires enter key.

getchar() -It takes input, echo character and requires enter key.

sprintf() function?-54

The sprintf() stands for “string print.” The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.

Syntax

int sprintf ( char * str, const char * format, … );

Let’s see a simple example

#include<stdio.h>

int main()

{

char a[20];

int n=sprintf(a,“javaToint”);

printf(“value of n is %d”,n);

return 0;

}


Output:

value of n is 9

sprintf() function?-54

The sprintf() stands for “string print.” The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.

Syntax

int sprintf ( char * str, const char * format, … );

Let’s see a simple example

#include<stdio.h>

int main()

{

char a[20];

int n=sprintf(a,“javaToint”);

printf(“value of n is %d”,n);

return 0;

}


Output:

value of n is 9