You are on page 1of 6

C LANGUAGE MCQS

DECLARATION AND INITIALIZATIONS

1)
By default a real number is treated as a DOUBLE
In computing, 'real number' often refers to non-complex floating-point numbers. It
include both rational numbers, such as 42 and 3/4, and irrational numbers such as
pi = 3.14159265...

When the accuracy of the floating point number is insufficient, we can use the
double to define the number. The double is same as float but with longer precision
and takes double space (8 bytes) than float.

To extend the precision further we can use long double which occupies 10 bytes of
memory space.

2)
Data tyes in C
Primary data types
int
char
float
double
void
Secondary data types (or) User-defined data type
Array
Pointer
Structure
Union
Enum

3)
Declaring is the way a programmer tells the compiler to expect a particular type,
be it a variable, class/struct/union type, a function type (prototype) or a
particular object instance. (ie. extern int i)

Declaration never reserves any space for the variable or instance in the program's
memory; it simply a "hint" to the compiler that a use of the variable or instance
is expected in the program. This hinting is technically called "forward reference".

- During declaration we tell the datatype of the Variable.

- During definition the value is initialized.

4)
A function prototype in C or C++ is a declaration of a function that omits the
function body but does specify the function's name, argument types and return type.

While a function definition specifies what a function does, a function prototype


can be thought of as specifying its interface.

5)
fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating
point divisions.

6) types of linkage
External Linkage-> means global, non-static variables and functions.
Internal Linkage-> means static variables and functions with file scope.
None Linkage-> means Local variables.

7)symbol allowed in a variablr name?


Variable names in C are made up of letters (upper and lower case) and digits. The
underscore character ("_") is also permitted. Names must not begin with a digit.

8) How would you round off a value from 1.66 to 2.0? ceil(1.66)

9)Linker Error : Undefined symbol 'i'


The statement extern int i specifies to the compiler that the memory for 'i' is
allocated in some other program and that address will be given to the current
program at the time of linking. But linker finds that no other variable of name 'i'
is available in any other program with memory space allocated for it. Hence a
linker error has occurred.

10)pointer declaration;-
Any pointer size is 2 bytes. (only 16-bit offset)
So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset
value.

Since C is a compiler dependent language, it may give different output in other


platforms. The above program works fine in Windows (TurboC), but error in Linux
(GCC Compiler).

11)When an automatic structure is partially initialized remaining elements are


initialized to 0(zero).

12) True, we can use long double; if double range is not enough.

13) True, When a function is declared inside the source file, that function(local
function) get a priority than the extern function. So there is no need to declare a
function as extern inside the same source file.

14) True, we can find the size of short integer and long integer using the sizeof()
operator.

\\EXPRESSIONS//
1)* / % + - =
C uses left associativity for evaluating expressions to break a tie between two
operators having same precedence.

2)Simply called as BODMAS (Bracket of Division, Multiplication, Addition and


Subtraction).

How Do I Remember ? BODMAS !

B - Brackets first
O - Orders (ie Powers and Square Roots, etc.)
DM - Division and Multiplication (left-to-right)
AS - Addition and Subtraction (left-to-right)

3)sizeof operator: sizeof is a much used in the C/C++ programming language. It is a


compile time unary operator which can be used to compute the size of its operand.
The result of sizeof is of unsigned integral type which is usually denoted by
size_t. Basically, sizeof operator is used to compute the size of the variable. To
learn about sizeof operator in details you may visit this link.

4)static int a[20]; here variable a is declared as an integer type and static. If a
variable is declared as static and it will be automatically initialized to value
'0'(zero).

\\\ FUNCTION ///


1) A call stack or function stack is used for several related purposes, but the
main reason for having one is to keep track of the point to which each active
subroutine should return control when it finishes executing.

A stack overflow occurs when too much memory is used on the call stack.

Here function main() is called repeatedly and its return address is stored in the
stack. After stack memory is full. It shows stack overflow error.

2) There is an error in this line i>=45 ? return(*p): return(*q);. We cannot use


return keyword in the terenary operators
3) MAIN FUNCTION CAN CALL MAIN

4) return (kk, ll);


Comma (,) operator has left to right associativity, so while returning the
rightmost operator will be returned and no error because of this.
5) Function Pointer in C

\\\ POINTERS ///


1)An integer constant expression with the value 0, or such an expression cast to
type void *, is called a null pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a null pointer, is
guaranteed to compare unequal to a pointer to any object or function.”

2)The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h,


time.h, and wchar.h.

\\\ strings ///


1)Which of the following function sets first n characters of a string to a given
character? STRNSET
2) Declaration: strcmp(const char *s1, const char*s2);

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

if s1 > s2 returns a value > 0

3) Declaration: char *strrchr(const char *s, int c);

It scans a string s in the reverse direction, looking for a specific character c.

4)The function strstr() Finds the first occurrence of a substring in another string

Declaration: char *strstr(const char *s1, const char *s2);


Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points
to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.

5) printf(5+"Good Morning\n"); It skips the 5 characters and prints the given


string.

Hence the output is "Morning"

6) Here str[] has declared as 7 character array and into a 8 character is stored.
This will result in overwriting of the byte beyond 7 byte reserved for '\0'.
~ strlen function gives the length of string before null character means str=bcg\
0hch(3)
~ char *b="sucesss" // it is readonly by default // in c language constt keyword is
added by default

\\\ OPERATORS ///

1) The bracket operator has higher precedence than assignment operator. The
expression within bracket operator is evaluated from left to right
but it is always the result of the last expression which gets assigned.

2)
int i = 1, 2, 3;
printf("%d", i);// Comma acts as a separator here. The compiler creates an
integer variable and initializes it with 1. The compiler fails to create integer
variable 2 because 2 is not a valid identifier.

3)
int i;
i = 1, 2, 3;
printf("%d", i);// Comma acts as an operator. The assignment operator has higher
precedence than comma operator. So, the expression is considered as (i = 1), 2, 3
and 1 gets assigned to variable i.

4)
int i = 3;
printf("%d", (++i)++);
return 0; // In C, prefix and postfix operators need l-value to perform operation
and return r-value. The expression (++i)++ when executed increments the value of
variable i(i is a l-value) and returns r-value. The compiler generates the error(l-
value required) when it tries to post-incremeny the value of a r-value.

5) The main theme of the program lies here: sizeof(k /= i + j). An expression
doesn’t get evaluated inside sizeof operator. sizeof operator returns sizeof(int)
because the result of expression will be an integer. As the expression is not
evaluated, value of k will not be changed.

6) An expression doesn’t get evaluated inside sizeof operator. GeeksQuiz will not
be printed. printf returns the number of characters to be printed i.e. 9 which is
an integer value. sizeof operator returns sizeof(int).

7) The operator ‘+’ doesn’t have a standard defined order of evaluation for its
operands. Either f1() or f2() may be executed first. So a compiler may choose to
output either “GeeksQuiz” or “QuizGeeks”.

8) int a = 1;
int b = 1;
int c = a || --b;
int d = a-- && --b;
printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
return 0; // // Since a is 1, the expression --b is not executed because
// of the short-circuit property of logical or operator
// So c becomes 1, a and b remain 1
int c = a || --b;

// The post decrement operator -- returns the old value in current expression
// and then updates the value. So the value of expression --a is 1. Since the
// first operand of logical and is 1, shortcircuiting doesn't happen here. So
// the expression --b is executed and --b returns 0 because it is pre-increment.
// The values of a and b become 0, and the value of d also becomes 0.
int d = a-- && --b;

9) Similar to operator ‘+’, most of the other similar operators like ‘-‘, ‘/’, ‘*’,
Bitwise AND &, Bitwise OR |, .. etc don’t have a standard defined order for
evaluation for its operands.
~In general, a program is converted to its executable code using the following
steps.
1. Pre-processing
2. Source code to object code conversion
3. Linking
~ Which file is generated after pre-processing of a C program?// .i
~ undefined macros are initialise to zero. macro func nhi hota wo expand hota hai//
#str matlb str #s matlb s//## means concatination//#ifdef macro checks whter the
macro is defined or not it doesnt matter if it is defined with zero aur anything
else.// main k pahele aur baad me agar koi func excuete krna hai to we can do that
with help of "pragmma".
~ cannot writr printf gobally.
~ redeclaration of a varible is allowed , whereas redefination of avarible is not
allowed.but in macros it is allowed.
~ extern is a declaration it will loook for a global value .if global value doesnt
exist then it will produce a linkage error. if there two global values then also it
is a linkage error.
~ same static varible can be declaared many times but we can initialise them only
once.
~ strcmp returns the ascii value of first mismatch character.
~ studyyyyy lecture 37;
~ padding/memory holes in structer.
~ static keyword ka extent gobal hota tabhi wo ek baari hi bnata hai.
~ jab koi varible function me define na ho matlab uski value na pta ho wo free
varible hota aur uss free varible ki value define krta hai scoping rule.do type k
scoping hoti hai static and dynamic // c c++ java static folooow krta hai.
~printf ko globally nhi likh skte usko func me hi likh skte ho .
~ pre increament aur post incre/drec krte time compiler me depend krta hai wo eary
binding kreyega ya late binding . COMPILERRRR.
~ switch statement ka kaam hi yahi hai wo direct case pe jump krre beech me kch bhi
ho.except cases no statement will execute in switch.// break na ho case me to wo
chalta rahega.// continue switch k sath kam nhi krta// compiler maintains jump
table.
~for loop me initialisation sirf ek nbaar hota hai
~stack overflow recursion k sath hota hai main k sath nhi.
~break if else se bahar nhi loop se bahar se jata hai.
~while loop me condition compulsory hai while ko khaali nhi rakh skte.
~for loop me do semi colon compulsory hai khaali bhi rakh skte ho // for loop k
last me agar ; ho to wo ek statement hui wo baar nhi bas jab condition galat hogi
loop tab next line ko exceute karega. see video.
~In C, parameters are always// pass by value
~ function cannot return function and array.
~ In C, three dots is known as an ellipsis. It accepts a variable number of
parameters when used in a function definition. But, we cannot find the total number
of arguments passed. Hence in the given program, only the first value gets assigned
to the parameter 's'. In the case of the first function call, the value 2 gets
assigned to 's' and the same gets printed. In the second function call, the value 3
gets assigned and printed.
~ fun declaration is necessary when fun call is before fun def.// a fun cant be
define inside another fun// default return typr is int not void.// funcant return
more than one value// redeclaration of afunction is not an error whereas
redefination is an error.
~pointer are the only data type in c which can hold address. // c compiler can
support 12 level of pointer// addition and subtracton of two pointers depends upon
type of pointer.
~void pointer has no associative data type// airthematic operations are not allowed
with pointer.
~In the concept of a pointer, the operator * is used for dereferencing and the
operator & is used to get the address of a variable. We can use them alternatively
for n number of times. These operators cancel out the effect of each other when
they used one after another. In the given program, 'ptr' is a pointer variable
pointing to the first character of the string "PROcoder". Hence, *ptr prints P.
~a[i]=i[a] same hota hai.
~in partially initialise array the remaining elements are initialise with null.
~in enum we can initialise varible with numeric value in incresing fashion //
diffrent varibles can have same values//two varible cannt have same value within a
single scope.//enum is a user data type//
~typedef//
~constt keyword with integer make the value of integer read only , if we want to
change the value we can do that by using pointers.
~sizeof//
~malloc allocates memory inside heap//return type is void pointer
~segmentation error is when you put value and memory is not allocated.
~calloc two type k arugment leta hai//calloc sari memory ko 0 se initialise krke
deta ,alloc garbage dedeta hai thats y calloc is slower tha malloc//
~ All number primitives in Java are signed. Hence the Compilation will fails.

You might also like