You are on page 1of 28

What is C?

C is a structured, high level, machine independent programming language developed by Dennis Ritchie
at AT&T Bell laboratory in 1972.

Who invented C?

Dennis M. Ritchie (Dennis MacAlistair Ritchie) at AT&T Bell laboratory in 1972.

What is the current stable version of C?

C11 (C Standard Revision) C11 (formerly C1X) is an informal name for ISO/IEC 9899:2011.

What is the importance of C?

Importance of ‘C’ language

C language is a famous programming language due to its qualities. Some qualities are:

1. It is robust language whose rich setup of built in functions and operator can be used to write any
complex program.
2. Program written in C are efficient due to several variety of data types and powerful operators.
3. The C compiler combines the capabilities of an assembly language with the feature of high level
language. Therefore it is well suited for writing both system software and business package.
4. There are only 32 keywords; several standard functions are available which can be used for
developing program.
5. C is portable language; this means that C programs written for one computer system can be run
on another system, with little or no modification.
6. C language is well suited for structured programming, this requires user to think of a problems
in terms of function or modules or block. A collection of these modules make a program
debugging and testing easier.
7. C language has its ability to extend itself. A c program is basically a collection of functions that
are supported by the C library. We can continuously add our own functions to the library with
the availability of the large number of functions.

What are tokens?

Smallest Individual Units of a Program are called Tokens. There are five types of tokens in C.

1- Identifiers
2- Keywords
3- Operators
4- Constants/Literals
5- Delimiters /Punctuators/Separators
What is ANSI?

The American National Standards Institute is a private non-profit organization that oversees the
development of voluntary consensus standards for products, services, processes, systems, and
personnel in the United States. The organization also coordinates U.S. standards with international
standards so that American products can be used worldwide.

The first standard for C was published by ANSI. Although this document was subsequently adopted by
International Organization for Standardization (ISO) and subsequent revisions published by ISO have
been adopted by ANSI, the name ANSI C (rather than ISO C) is still more widely used.

What are header files?

A header file is a file with extension .h which contains C function declarations and macro definitions to
be shared between several source files. There are two types of header files: the files that the
programmer writes (user-defined header files) and the files that comes with your compiler (C library).

You request to use a header file in your program by including it with the C preprocessing directive
#include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.

Including a header file is equal to copying the content of the header file but we do not do it because it
will be error-prone and it is not a good idea to copy the content of a header file in the source files,
especially if we have multiple source files in a program.

A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global
variables, and function prototypes in the header files and include that header file wherever it is
required.

What are Preprocessors in C?

The Preprocessors is a program that processes the source code before it passes through the compiler.
It operates under the control of what is known as preprocessor command lines or directives.

Using Preprocessors a program can make program easy to read, easy to modify, portable and more
efficient.

Preprocessors are placed in the source program before the main line. Before source code passes
through the compiler, it is examined the processor for any preprocessor directives. If there are any,
appropriate actions (as per the directives) are taken and then the source program is handed over the
compiler.

Preprocessors directives follow special syntax rules that are different from the normal C syntax. They all
begin with the symbol # in column one and do not require a semicolon at the end.

These directives can be divided into three categories:

1- File Inclusion Directives (Unconditional)


2- Macro Substitution Directives (Unconditional)
3- Compiler Control Directives (Conditional)

The unconditional directives are:

#include - Inserts a particular header from another file


#define - Defines a preprocessor macro
#undef - Undefines a preprocessor macro

The conditional directives are:

#ifdef - If this macro is defined


#ifndef - If this macro is not defined
#if - Test if a compile time condition is true
#else - The alternative for #if
#elif - #else an #if in one statement
#endif - End preprocessor conditional

What is # define & #include?

#define:

A #define preprocessor directive is used to define symbolic constants and macros.

#include:

A #include preprocessor directive is used to include/link the header files stored in the C libraries in the
source programs.

What is the difference between compiler and interpreter?

Difference between Compiler and Interpreter

No Compiler Interpreter
1 Compiler Takes Entire program as input Interpreter Takes Single instruction as input .
2 Intermediate Object Code is Generated No Intermediate Object Code is Generated
3 Conditional Control Statements are Executes Conditional Control Statements are Executes
faster slower
4 Memory Requirement : More (Since Object Memory Requirement is Less
Code is Generated)
5 Program need not be compiled every time Every time higher level program is converted into
lower level program
6 Errors are displayed after entire program is Errors are displayed for every instruction
checked interpreted (if any)
7 Example : C Compiler/C++ Compiler Example : BASIC/ Visual Basic
What are escape sequences/back slash character constants?

Escape sequences are used to represent certain special characters within string literals and character
literals.

An escape sequence is a sequence of characters that does not represent itself when used inside a
character or string literal, but is translated into another character or a sequence of characters that may
be difficult or impossible to represent directly.

Note that each one of them represents one character, although they consist of two characters.

Because they all are prefixed by backslash character constant they are also called backslash character
constants. Some of them are as below:

Escape Sequence Character Description Representation


\' single quote byte 0x27 in ASCII encoding
\" double quote byte 0x22 in ASCII encoding
\? question mark byte 0x3f in ASCII encoding
\\ backslash byte 0x5c in ASCII encoding
\a audible bell byte 0x07 in ASCII encoding
\b backspace byte 0x08 in ASCII encoding
\f form feed - new page byte 0x0c in ASCII encoding
\n line feed - new line byte 0x0a in ASCII encoding
\r carriage return byte 0x0d in ASCII encoding
\t horizontal tab byte 0x09 in ASCII encoding
\v vertical tab byte 0x0b in ASCII encoding

What are trigraph sequences?

Many Non-English Keyboards do not support all the characters of C Character set. ANSI C introduces the
concept of “trigraph” sequences to provide a way to enter certain characters hat are not available on
some keyboards.

Each trigraph sequence consists of three characters (two question marks followed by another character)
as shown in below table. For example – if a keyboard does not support square brackets, we can still use
them in C program using the trigraph ??( for opening square bracket [ or ??) for closing square
bracket ].

Note that they should be treated as if they were single characters, although they are made up of three
characters.
C supports the following 9 trigraph characters.

Trigraph sequence Equal character


??= #
??( [
??) ]
??/ \
??< {
??> }
??! |
??’ ^
??- ~

What are the rules for naming and identifier?

Rules for Identifiers:

1. First character must be an alphabet (or underscore)


2. Must consist of only letters, digits or underscore.
3. Only first 31 character are significant. Minimum 8 characters are supported by all
compilers.
4. Must not contain white space.
5. Must not contain any special character except underscore(_)
6. They are case sensitive.
7. Cannot use a keyword.

What is ternary operator?

An operator with three operands is called ternary operator. C supports only one ternary operator that is
also called conditional operators as it is used to check condition and make decision accordingly.

Symbol for Conditional Operator is : ?:

Its syntax is : Condition ? Expression1 : Expression 2 ;

Example:

max = (a>b)? a: b;

If a is greater than be then value of a will be assigned to max otherwise b’s value will be assigned to
max.
What are the primary data types in C?

Primary Data Types are those which are in-built data types in C and cannot be decomposed further as
they are not made up by the combination of any other data types.

There are five fundamental data types in C.

1- Integer ( int)
2- Character (char)
3- Floating Point (float)
4- Double Precision Floating Point (double)
5- Void (void)

How many types of data types in C?

ANSI C Supports three classes of data types:

1- Primary (other names – atomic/basic/fundamental/in-bult/built-in)


(Integer,Character, Float, Double, Void)
2- Derived Data Types
(Arrays,Functions, Pointers)
3- User-defined Data types
(Structures, Unions, Enumerations)

Why integer can store only 32767 positive integers?

In C language, int takes 2 bytes. Now 2 bytes mean 16 bits.

One bit out of these 16 bits is for sign (+ or -) of integer.

So, remaining 15 bits will store data in form of 0 and 1. The maximum value in binary number system
using 15 bits can be 32767.

Signed Integer:

32767 Decimal = 111111111111111 (Binary) – 15 Digits

+/- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
               
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

So an integer can store value from -32768 to +32767.

And this becomes from 0 to 65536 for unsigned integers, due to the reason that total usable bits
become 16.
What is the difference between void main() and int main().

void main() int main()


Explicitly defined that main doen’t return using Explicitly defined that main return an integer type
void thus doesn’t require return 0 statement at of value thus at the end of the main return 0
the end of main. statement required.

implicitly it doesn’t take any argument. Implicitly it doesn’t take any argument.

What are the type qualifiers/modifiers?

Type qualifiers or Type Modifiers are keywords available in C which modifies or alters the meaning of
basic or primary data types.

These are four:

1- Signed (Default)

Applies to:
Integer, Character, Float, Double
Not Applies to:
Void
2- Unsigned (Negative range becomes positives thus double the range without increasing memory)

Applies to:
Integer, Character, Float, Double
Not Applies to:
Void
3- Short

Applies to:
Integer only
Not Applies to:
Character, Float, Double, Void

4- Long (Increases memory thus also increases range )

Applies to:
Integer, Double
1. integer-2 bytes to long integer-4 bytes
2. double-8 bytes to long double – 10 bytes

Not Applies to: Character, Float, Void


What are the keywords? How many keywords are there in C? List them.

Keywords are special words because they have predefined and fixed meanings to the language
compilers and cannot be changed.

They serve as basic building blocks for program statements and used in syntaxes and grammar of the
language.

ANSI C or C89 supports 32 keywords in C:

ANSI C (C89)/ISO C (C90) keywords:

1. auto 9. double 17. int 25. struct


2. break 10. else 18. long 26. switch
3. case 11. enum 19. register 27. typedef
4. char 12. extern 20. return 28. union
5. const 13. float 21. short 29. unsigned
6. continue 14. for 22. signed 30. void
7. default 15. goto 23. sizeof 31. volatile
8. do 16. if 24. static 32. while

Keywords added to ISO C99 (supported in most new compilers):

1. _Bool 3. _Imaginary
2. _Complex 4. inline

Keywords added to ISO C11 (supported only in some newer compilers):

1. alignof 4. _Generic 7. _Thread_local


2. _Alignas 5. _Noreturn
3. _Atomic 6. _Static_assert

What do you mean by storage classes?

What is dangling else problem?

What is sentinel loop?

How many types of loops are there in C?

What is the difference between do while and while ?

What is the difference between for and while ? (Important does rare)
Difference between i++ and ++i?

Why arrays are also called contiguous memory allocation units?

What is the difference between malloc() and calloc()

What is sequential file handling?

What are pointers?

What are the usage of void keywords in C?

Can we write a program in C without using semicolon?

Write a program to print semicolon without using semicolon anywhere in a program.

What is the difference between break and continue?

What is type casting?

What is integral promotion?

What is the purpose of comments and indentation in C?

What are bitwise operators?

What is recursion?

Define goto statement?

What is meant by scope of a variable?

What is the difference between structures and unions?

Why C is called structured programming language?

Why C is also called procedure programming languages?

What is the importance/advantages of C?

What are the disadvantages of C?

How will you create your own header file?

How many types of error are there in C?

What is the difference between Pass by Value and Pass By Reference?

What is Call by Pointers?


What is the use of SizeOf() operator?

What is operators precedence and associativity?

Differentiate between “a” and ‘a’?

Differentiate between && and &?

What are the expressions?

What are symbolic constants in C?

What are enums?

What is typedef?

Why the range of integer differs on 16 bit and 32 bit machines?

What are the difference between 16 bit and 32 bit machines?

What are source code?

What is native language?

What are the benefits of using pointers?

What is Dangling Pointer?

What is Null Pointer?

What is chain of pointer?

What is heap?

What are free store functions?

Can we compare two objects of the same structure? If No Why?

What is Scale Factor?

Is Function Prototyping is Mandatory in C?


What is a pointer on pointer?

It‟s a pointer variable which can hold the address of another pointer variable. It de-refers twice to
point to the data held by the designated pointer variable.

Eg: int x = 5, *p=&x, **q=&p;

Therefore „x‟ can be accessed by **q.

Distinguish between malloc() & calloc() memory allocation.

Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated
memory with 0‟s.

What is keyword auto for?

By default every local variable of the function is automatic (auto). In the below function both the
variables „i‟ and „j‟ are automatic variables.

void f() {
int i;
auto int j;
}

NOTE − A global variable can‟t be an automatic variable.

What are the valid places for the keyword break to appear.

Break can appear only with in the looping control and switch statement. The purpose of the
break is to bring the control out from the said blocks.

Explain the syntax for for loop.


for(expression-1;expression-2;expression-3) {
//set of statements
}

When control reaches for expression-1 is executed first. Then following expression-2, and if
expression-2 evaluates to non-zero „set of statements‟ and expression-3 is executed, follows
expression-2.

What is difference between including the header file with-in angular braces < > and double
quotes “ “
If a header file is included with in < > then the compiler searches for the particular header file
only with in the built in include path. If a header file is included with in “ “, then the compiler
searches for the particular header file first in the current working directory, if not found then in
the built in include path.

How a negative integer is stored.

Get the two‟s compliment of the same positive integer. Eg: 1101 (-5)

Step-1 − One‟s compliment of 5 : 1010

Step-2 − Add 1 to above, giving 1011, which is -5

What is a static variable?

A static local variables retains its value between the function call and the default value is 0. The
following function will print 1 2 3 if called thrice.

void f() {
static int i;
++i;
printf(“%d “,i);
}

If a global variable is static then its visibility is limited to the same source code.

What is a NULL pointer?

A pointer pointing to nothing is called so. Eg: char *p=NULL;

What is the purpose of extern storage specifier?

Used to resolve the scope of global symbol.

Eg:
main() {
extern int i;
Printf(“%d”,i);
}

int i = 20;
Explain the purpose of the function sprintf().

Prints the formatted output onto the character array.

What is the meaning of base address of the array?

The starting address of the array is called as the base address of the array.
When should we use the register storage specifier?

If a variable is used most frequently then it should be declared using register storage specifier,
then possibly the compiler gives CPU register for its storage to speed up the look up of the
variable.

S++ or S = S+1, which can be recommended to increment the value by 1 and why?

S++, as it is single machine instruction (INC) internally.

What is a dangling pointer?

A pointer initially holding valid address, but later the held address is released or freed. Then such
a pointer is called as dangling pointer.

What is the purpose of the keyword typedef?

It is used to alias the existing type. Also used to simplify the complex declaration of the type.

What is lvalue and rvalue?

The expression appearing on right side of the assignment operator is called as rvalue. Rvalue is
assigned to lvalue, which appears on left side of the assignment operator. The lvalue should
designate to a variable not a constant.

What is the difference between actual and formal parameters?

The parameters sent to the function at calling end are called as actual parameters while at the
receiving of the function definition called as formal parameters.

Can a program be compiled without main() function?

Yes, it can be but cannot be executed, as the execution requires main() function definition.

What is the advantage of declaring void pointers?

When we do not know what type of the memory address the pointer variable is going to hold,
then we declare a void pointer for such.

Where an automatic variable is stored?

Every local variable by default being an auto variable is stored in stack memory.

What is a nested structure?

A structure containing an element of another structure as its member is referred so.


What is the difference between variable declaration and variable definition?

Declaration associates type to the variable whereas definition gives the value to the variable.

What is a self-referential structure?

A structure containing the same structure pointer variable as its element is called as self-
referential structure.

Does a built-in header file contains built-in function definition?

No, the header file only declares function. The definition is in library which is linked by the
linker.

Explain modular programming.

Dividing the program in to sub programs (modules/function) to achieve the given task is modular
approach. More generic functions definition gives the ability to re-use the functions, such as
built-in library functions.

What is a token?

A C program consists of various tokens and a token is either a keyword, an identifier, a constant,
a string literal, or a symbol.

What is a preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation
process begins.

Explain the use of %i format specifier w.r.t scanf().

Can be used to input integer in all the supported format.

How can you print a \ (backslash) using any of the printf() family of functions.

Escape it using \ (backslash).

Does a break is required by default case in switch statement?

Yes, if it is not appearing as the last case and if we do not want the control to flow to the
following case after default if any.

When to user -> (arrow) operator.


If the structure/union variable is a pointer variable, to access structure/union elements the arrow
operator is used.

What are bit fields?

We can create integer structure members of differing size apart from non-standard size using bit
fields. Such structure size is automatically adjusted with the multiple of integer size of the
machine.

What are command line arguments?

The arguments which we pass to the main() function while executing the program are called as
command line arguments. The parameters are always strings held in the second argument (below
in args) of the function which is array of character pointers. First argument represents the count
of arguments (below in count) and updated automatically by operating system.

main( int count, char *args[]) {


}
What are the different ways of passing parameters to the functions? Which to use when?

Call by value − We send only values to the function as parameters. We choose this if we
do not want the actual parameters to be modified with formal parameters but just used.
Call by reference − We send address of the actual parameters instead of values. We
choose this if we do want the actual parameters to be modified with formal parameters.

What is the purpose of built-in stricmp() function.

It compares two strings by ignoring the case.

Describe the file opening mode “w+”.

Opens a file both for reading and writing. If a file is not existing it creates one, else if the file is
existing it will be over written.

Where the address of operator (&) cannot be used?

It cannot be used on constants.

It cannot be used on variable which are declared using register storage class.

Is FILE a built-in data type?

No, it is a structure defined in stdio.h.

What is reminder for 5.0 % 2?

Error, It is invalid that either of the operands for the modulus operator (%) is a real number.
How many operators are there under the category of ternary operators?

There is only one operator and is conditional operator (? : ).

Which key word is used to perform unconditional branching?

goto

What is a pointer to a function? Give the general syntax for the same.

A pointer holding the reference of the function is called pointer to a function. In general it is
declared as follows.

T (*fun_ptr) (T1,T2…); Where T is any date type.

Once fun_ptr refers a function the same can be invoked using the pointer as follows.

fun_ptr();
[Or]
(*fun_ptr)();
Explain the use of comma operator (,).

Comma operator can be used to separate two or more expressions.

Eg: printf(“hi”) , printf(“Hello”);


What is a NULL statement?

A null statement is no executable statements such as ; (semicolon).

Eg: int count = 0;


while( ++count<=10 ) ;

Above does nothing 10 times.

What is a static function?

A function‟s definition prefixed with static keyword is called as a static function. You would
make a function static if it should be called only within the same source code.

Which compiler switch to be used for compiling the programs using math library with gcc
compiler?

Opiton –lm to be used as > gcc –lm <file.c>

Which operator is used to continue the definition of macro in the next line?

Backward slash (\) is used.


E.g. #define MESSAGE "Hi, \

Welcome to C"

ome to C"
Which operator is used to receive the variable number of arguments for a function?

Ellipses (…) is used for the same. A general function definition looks as follows

void f(int k,…) {


}
What is the problem with the following coding snippet?
char *s1 = "hello",*s2 = "welcome";

strcat(s1,s2);

s1 points to a string constant and cannot be altered.

Which built-in library function can be used to re-size the allocated dynamic memory?

realloc().

Define an array.

Array is collection of similar data items under a common name.

What are enumerations?

Enumerations are list of integer constants with name. Enumerators are defined with the keyword
enum.

Which built-in function can be used to move the file pointer internally?

fseek()

What is a variable?

A variable is the name storage.

Who designed C programming language?

Dennis M Ritchie.

C is successor of which programming language?

What is the full form of ANSI?


American National Standards Institute.

Which operator can be used to determine the size of a data type or variable?

sizeof

Can we assign a float variable to a long integer variable?

Yes, with loss of fractional part.

Is 068 a valid octal number?

No, it contains invalid octal digits.

What it the return value of a relational operator if it returns any?

Return a value 1 if the relation between the expressions is true, else 0.

How does bitwise operator XOR works.

If both the corresponding bits are same it gives 0 else 1.

What is an infinite loop?

A loop executing repeatedly as the loop-expression always evaluates to true such as

while(0 == 0) {
}
Can variables belonging to different scope have same name? If so show an example.

Variables belonging to different scope can have same name as in the following code snippet.

int var;

void f() {
int var;
}

main() {
int var;
}
What is the default value of local and global variables?

Local variables get garbage value and global variables get a value 0 by default.

Can a pointer access the array?

Pointer by holding array‟s base address can access the array.


What are valid operations on pointers?

The only two permitted operations on pointers are

Comparision ii) Addition/Substraction (excluding void pointers)

What is a string length?

It is the count of character excluding the „\0‟ character.

What is the built-in function to append one string to another?

strcat() form the header string.h

Which operator can be used to access union elements if union variable is a pointer variable?

Arrow (->) operator.

Explain about „stdin‟.

stdin in a pointer variable which is by default opened for standard input device.

Name a function which can be used to close the file stream.

fclose().

What is the purpose of #undef preprocessor?

It be used to undefine an existing macro definition.

Define a structure.

A structure can be defined of collection of heterogeneous data items.

Name the predefined macro which be used to determine whether your compiler is ANSI standard
or not?

__STDC__

What is typecasting?

Typecasting is a way to convert a variable/constant from one type to another type.

What is recursion?

Function calling itself is called as recursion.


Which function can be used to release the dynamic allocated memory?

free().

What is the first string in the argument vector w.r.t command line arguments?

Program name.

How can we determine whether a file is successfully opened or not using fopen() function?

On failure fopen() returns NULL, otherwise opened successfully.

What is the output file generated by the linker.

Linker generates the executable file.

What is the maximum length of an identifier?

Ideally it is 32 characters and also implementation dependent.

What is the default function call method?

By default the functions are called by value.

Functions must and should be declared. Comment on this.

Function declaration is optional if the same is invoked after its definition.

When the macros gets expanded?

At the time of preprocessing.

Can a function return multiple values to the caller using return reserved word?

No, only one value can be returned to the caller.

What is a constant pointer?

A pointer which is not allowed to be altered to hold another address after it is holding one.

To make pointer generic for which date type it need to be declared?

Void

Can the structure variable be initialized as soon as it is declared?


Yes, w.r.t the order of structure elements only.

Is there a way to compare two structure variables?

There is no such. We need to compare element by element of the structure variables.

Which built-in library function can be used to match a patter from the string?

Strstr()

What is difference between far and near pointers?

In first place they are non-standard keywords. A near pointer can access only 2^15 memory
space and far pointer can access 2^32 memory space. Both the keywords are implementation
specific and are non-standard.

Can we nest comments in a C code?

No, we cannot.

Which control loop is recommended if you have to execute set of statements for fixed number of
times?

for – Loop.

What is a constant?

A value which cannot be modified is called so. Such variables are qualified with the keyword
const.

Can we use just the tag name of structures to declare the variables for the same?

No, we need to use both the keyword „struct‟ and the tag name.

Can the main() function left empty?

Yes, possibly the program doing nothing.

Can one function call another?

Yes, any user defined function can call any function.

Apart from Dennis Ritchie who the other person who contributed in design of C language.

Brain Kernighan
What is difference between including the header file with-in angular braces < > and double
quotes “ “

If a header file is included with in < > then the compiler searches for the particular header file
only with in the built in include path. If a header file is included with in “ “, then the compiler
searches for the particular header file first in the current working directory, if not found then in
the built in include path.

The difference depends on the compiler you are using. Some compilers treat them the same, while
others don't. With vc++ the <> tells the compiler to only look in its standard include directories (there
are options to add to the list of directories), while the "" tells the compiler to first look in the program's
source directory, it the header file is not there then the compiler will look in its standard directories just
as if you had used <>.

What is volatile keyword?


The volatile keyword is intended to prevent the compiler from applying any optimizations on
objects that can change in ways that cannot be determined by the compiler.
Objects declared as volatile are omitted from optimization because their values can be changed
by code outside the scope of current code at any time.

Can a variable be both const and volatile?


yes, the const means that the variable cannot be assigned a new value. The value can be changed
by other code or pointer. For example the following program works fine.

int main(void)
{
const volatile int local = 10;
int *ptr = (int*) &local;
printf("Initial value of local : %d \n", local);
*ptr = 100;
printf("Modified value of local: %d \n", local);
return 0;
}
13. What is embedded C?

Embedded C is the extension of C programming language.


Embedded C is used to develop micro controller based applications.
Embedded C includes features not available in normal C like fixed-point arithmetic, named
address spaces, and basic I/O hardware addressing.
Cell phones, MP3 players are some example for embedded systems in which embedded C is
used to program and control these devices.

14. Which level is C language belonging to?

C language is belonging to middle level language. C language behaves as a bridge between


machine level (low level) languages and high level languages.
C language is more user friendly than machine level languages. And, C language does not
support all the concepts that high level languages offer. So, C programming language is called as
middle level language.

15. What do you mean by high level, middle level and low level languages and give an example
for each?

High level languages –

These level languages provide almost everything that the programmer might need to do as
already build into the language.
Example: Java, Python

Middle level languages –

These languages don’t provide all the built-in functions found in high level languages, but
provide all building blocks that we need to produce the result we want.
Example: C, C++

Low level languages –

These languages provide nothing other than access to the machine’s basic instruction set.
Example: Assembly language

What is wild pointer in C?

Uninitialized pointers are called as wild pointers in C which points to arbitrary (random) memory
location. This wild pointer may lead a program to behave wrongly or to crash.
98. What is file pointer in C?

File pointer is a pointer which is used to handle and keep track on the files being accessed. A
new data type called “FILE” is used to declare file pointer. This data type is defined in stdio.h file.
File pointer is declared as FILE *fp. Where, ‘fp’ is a file pointer.
fopen() function is used to open a file that returns a FILE pointer. Once file is opened, file pointer
can be used to perform I/O operations on the file. fclose() function is used to close the file.

99. When can void pointer and null pointer be used in C?

Void pointer is a generic pointer that can be used to point another variable of any data type. Null
pointer is a pointer which is pointing to nothing.

100. What is const pointer in C?

Const pointer is a pointer that can‟t change the address of the variable that is pointing to.
Once const pointer is made to point one variable, we can‟t change this pointer to point to any
other variable. This pointer is called const pointer.

Can array subscripts have negative value in C?

No. Array subscripts should not have negative value. Always, it should be positive.

What is pragma in C? Or how will you execute functions before and after main function in C
program?

#pragma is a pre-processor macro in C. It is used to call a function before and after main
function in a C program.

Example program:

#include <stdio.h>

void function1( );
void function2( );

#pragma startup function1


#pragma exit function2
int main( )
{
printf ( "\n Now we are in main function" ) ;
return 0;
}
void function1( )
{
printf("\nFunction1 is called before main function call");
}
void function2( )
{
printf ( "\nFunction2 is called just before end of " \
"main function" ) ;"
}

If you want to execute C program even after main function is terminated, which function can be
used?

“atexit()” function can be used if we want to execute any function after program is terminated
normally.
Syntax: int atexit (void (*func)(void));
The function “func” will be called automatically without any arguments once program is
terminated normally.

77. What is exit() function in C?

exit() function terminates the program/process normally and returns the status code to the
parent program/process.
Syntax: void exit(int status)

78. Is it possible to call atexit() function more than once in a C program?

Yes. We can call atexit() function more than once. But, they will be executed in reverse order as
a stack.

Example program:
What is memory leak? Why it should be avoided
Ans: Memory leak occurs when programmers create a memory in heap and forget to delete it.
Memory leaks are particularly serious issues for programs like daemons and servers which by
definition never terminate.

/* Function with memory leak */


#include <stdlib.h>

void f()
{
int *ptr = (int *) malloc(sizeof(int));

/* Do some work */

return; /* Return without freeing ptr*/


}

What is scope of a variable? How are variables scoped in C?


Ans: Scope of a variable is the part of the program where the variable may directly be accessible. In C, all
identifiers are lexically (or statically) scoped. See this for more details.

What is NULL pointer?


Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should
initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make
a pointer NULL when memory pointed by it is deallocated in the middle of a program.

What is Dangling pointer?


Ans: Dangling Pointer is a pointer that doesn‟t point to a valid memory location. Dangling
pointers arise when an object is deleted or deallocated, without modifying the value of the
pointer, so that the pointer still points to the memory location of the deallocated memory.
Following are examples.

// EXAMPLE 1
int *ptr = (int *)malloc(sizeof(int));
.............
.............
free(ptr);

// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10; // or printf("%d", *ptr);
// EXAMPLE 2
int *ptr = NULL
{
int x = 10;
ptr = &x;
}
// x goes out of scope and memory allocated to x is free now.
// So ptr is a dangling pointer now.
What are local static variables? What is their use?
Ans:A local static variable is a variable whose lifetime doesn‟t end with a function call where it
is declared. It extends for the lifetime of complete program. All calls to the function share the
same copy of local static variables. Static variables can be used to count the number of times a
function is called. Also, static variables get the default value as 0. For example, the following
program prints “0 1″

#include <stdio.h>
void fun()
{
// static variables get the default value as 0.
static int x;
printf("%d ", x);
x = x + 1;
}

int main()
{
fun();
fun();
return 0;
}
// Output: 0 1

What are static functions? What is their use?

Ans:In C, functions are global by default. The “static” keyword before a function name makes it static.
Unlike global functions in C, access to static functions is restricted to the file where they are declared.
Therefore, when we want to restrict access to functions, we make them static. Another reason for
making functions static can be reuse of the same function name in other files. See this for examples and
more details.
Include Syntax

Both user and system header files are included using the preprocessing directive „#include‟. It
has two variants:

#include <file>
This variant is used for system header files. It searches for a file named file in a standard
list of system directories. You can prepend directories to this list with the -I option (see
Invocation).
#include "file"
This variant is used for header files of your own program. It searches for a file named file
first in the directory containing the current file, then in the quote directories and then the
same directories used for <file>. You can prepend directories to the list of quote
directories with the -iquote option.

The argument of „#include‟, whether delimited with quote marks or angle brackets, behaves like
a string constant in that comments are not recognized, and macro names are not expanded. Thus,
#include <x/*y> specifies inclusion of a system header file named x/*y.

However, if backslashes occur within file, they are considered ordinary text characters, not
escape characters. None of the character escape sequences appropriate to string constants in C
are processed. Thus, #include "x\n\\y" specifies a filename containing three backslashes.
(Some systems interpret „\‟ as a pathname separator. All of these also interpret „/‟ the same way.
It is most portable to use only „/‟.)

It is an error if there is anything (other than comments) on the line after the file name.

You might also like