Professional Documents
Culture Documents
C is a structured, high level, machine independent programming language developed by Dennis Ritchie
at AT&T Bell laboratory in 1972.
Who invented C?
C11 (C Standard Revision) C11 (formerly C1X) is an informal name for ISO/IEC 9899:2011.
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.
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.
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.
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.
#define:
#include:
A #include preprocessor directive is used to include/link the header files stored in the C libraries in the
source programs.
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:
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.
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.
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.
1- Integer ( int)
2- Character (char)
3- Floating Point (float)
4- Double Precision Floating Point (double)
5- Void (void)
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:
+/- 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
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().
implicitly it doesn’t take any argument. Implicitly it doesn’t take any argument.
Type qualifiers or Type Modifiers are keywords available in C which modifies or alters the meaning of
basic or primary data types.
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
Applies to:
Integer, Double
1. integer-2 bytes to long integer-4 bytes
2. double-8 bytes to long double – 10 bytes
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.
1. _Bool 3. _Imaginary
2. _Complex 4. inline
What is the difference between for and while ? (Important does rare)
Difference between i++ and ++i?
What is recursion?
What is typedef?
What is heap?
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.
Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated
memory with 0‟s.
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;
}
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.
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.
Get the two‟s compliment of the same positive integer. Eg: 1101 (-5)
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.
Eg:
main() {
extern int i;
Printf(“%d”,i);
}
int i = 20;
Explain the purpose of the function sprintf().
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?
A pointer initially holding valid address, but later the held address is released or freed. Then such
a pointer is called as dangling pointer.
It is used to alias the existing type. Also used to simplify the complex declaration of the type.
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.
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.
Yes, it can be but cannot be executed, as the execution requires main() function definition.
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.
Every local variable by default being an auto variable is stored in stack memory.
Declaration associates type to the variable whereas definition gives the value to the variable.
A structure containing the same structure pointer variable as its element is called as self-
referential structure.
No, the header file only declares function. The definition is in library which is linked by the
linker.
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.
How can you print a \ (backslash) using any of the printf() family of functions.
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.
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.
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.
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.
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.
It cannot be used on variable which are declared using register storage class.
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?
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.
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 (,).
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?
Which operator is used to continue the definition of macro in the next line?
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
strcat(s1,s2);
Which built-in library function can be used to re-size the allocated dynamic memory?
realloc().
Define an array.
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?
Dennis M Ritchie.
Which operator can be used to determine the size of a data type or variable?
sizeof
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.
Which operator can be used to access union elements if union variable is a pointer variable?
stdin in a pointer variable which is by default opened for standard input device.
fclose().
Define a structure.
Name the predefined macro which be used to determine whether your compiler is ANSI standard
or not?
__STDC__
What is typecasting?
What is recursion?
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?
Can a function return multiple values to the caller using return reserved word?
A pointer which is not allowed to be altered to hold another address after it is holding one.
Void
Which built-in library function can be used to match a patter from the string?
Strstr()
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.
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.
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 <>.
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?
15. What do you mean by high level, middle level and low level languages and give an example
for each?
These level languages provide almost everything that the programmer might need to do as
already build into the language.
Example: Java, Python
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++
These languages provide nothing other than access to the machine’s basic instruction set.
Example: Assembly language
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.
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.
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.
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( );
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.
exit() function terminates the program/process normally and returns the status code to the
parent program/process.
Syntax: void exit(int status)
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.
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
// 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
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.