C Program Execution Without Main Function
C Program Execution Without Main Function
Interview Question in C
#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf(” hello “);
}
This program runs without main().
how??
Here we are using preprocessor(a program which processes the source code before compilation.) directive #define with arguments
to give an impression that the program runs without main. But in reality it runs with a hidden main function.
The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.
This program runs without main().
how??
Here we are using preprocessor(a program which processes the source code before compilation.) directive #define with arguments
to give an impression that the program runs without main. But in reality it runs with a hidden main function.
The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.
In the 2nd line of the program-
#define decode(s,t,u,m,p,e,d) m##s##u##t
What is the preprocessor doing here. The macro decode(s,t,u,m,p,e,d) is
being expanded as “msut” (The ## operator merges m,s,u & t into msut).
So actually C program can never run without a main() . We are just disguising the
main() with the preprocessor, but actually there exists a hidden main function
in the program.
Okay! Lets see previous program in Simplest
Way
Macro is defined by #define directive.
#include<stdio.h>
#define start main
void start()
{
printf("Hello, World!!!");
}
Interview Questions in C
C:
#include<stdio.h>
void main()
{
if(printf("hello world"))
{}
}
TOPICS FOR TODAY’s SESSION
• C
PREPROCESSOR DIRECTIVES
FORMAT SPECIFIERS
DATA TYPES
KEYWORDS
CONDITIONAL STATEMENTS
LOOPING STATEMENTS
OPERATORS
printf() and scanf() in C
• The printf() and scanf() functions are used for input and output in C language.
Both functions are inbuilt library functions, defined in stdio.h (header file).
printf() function
• The printf() function is used for output. It prints the given statement to the
console.
printf("format string",argument_list);
scanf() function
• The scanf() function is used for input. It reads the input data from the
console.
scanf("format string",argument_list);
Program to print cube of given number
#include<stdio.h>
int main(){
int num;
printf("enter a number:");
scanf("%d",&num);
printf("cube of number is:%d ",num*num*num);
return 0;
}
Output
enter a number:5
cube of number is:125
Program to print cube of given number
Types of Variables in C
Local Variable
• A variable that is declared inside the function or block is called a local
variable.
• It must be declared at the start of the block.
void function1(){
int x=10;//local variable
}
Global Variable
• A variable that is declared outside the function or block is called a
global variable. Any function can change the value of the global
variable. It is available to all the functions.
• It must be declared at the start of the block.
int value=20;//global variable
void function1(){
int x=10;//local variable
}
Variables in C
Static Variable
• A variable that is declared with the static keyword is called static variable.
• It retains its value between multiple function calls.
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
• The local variable will print the same value for each function call, e.g, 11,11,11
and so on. But the static variable will print the incremented value in each
function call, e.g. 11, 12, 13 and so on.
Variables in C
Automatic Variable
• All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable
using auto keyword.
void main(){
int x=10;//
local variable (also automatic)
auto int y=20;//automatic variable
}
External Variable
• We can share a variable in multiple C source files by using an external
variable. To declare
externan
intexternal variable,variable
x=10;//external you need
(alsotoglobal)
use extern
keyword.
Data Types in C
float 4 byte
double 8 byte
#include<stdio.h>
int main() A. 20
{
extern int X; B. 0
printf("%d\n", X);
C. Garbage Value
return 0;
} D. Error
int X=20;
Answer: Option A
Explanation:
extern int a; indicates that the variable a is defined elsewhere, usually in a
separate source code module.
printf("%d\n", a); it prints the value of local variable int a = 20. Because, whenever
there is a conflict between local variable and global variable, local variable gets the
highest priority. So it prints 20.
Declarations and Initializations
#include<stdio.h>
int main() A. 0, 0.000000
{
struct stu { B. Garbage values
char name[20];
C. Error
int age;
float sal; D. None of above
};
struct stu s = {“Smart"};
printf("%d, %f\n", [Link], [Link]);
return 0;
}
Answer: Option A
Explanation:
When an automatic structure is partially initialized remaining elements are
initialized to 0(zero).
Declarations and Initializations
A. 0
B. 1
C. Error
D. None of these
Answer: Option B
Explanation:
Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared and to
be TRUE. The 1 is assigned to i.
Declarations and Initializations
A. Garbage Values
B. 2, 3, 3
C. 3, 2, 2
D. 0, 0, 0
Answer: Option D
Explanation:
When an automatic array is partially initialized, the remaining elements are
initialized to 0.
Declarations and Initializations
#include<stdio.h>
int main()
{ A. 40 40
int a=40;
{ int a=20; B. 20 40
printf("%d ", a;
}
C. 20
printf("%d\n", a); D. Error
return 0;
}
Answer: Option B
Explanation:
In case of a conflict between a local variable and global variable, the local variable
gets priority.
Expressions
Expressions in C
#include<stdio.h>
int main()
A. 2, 2, 0, 1
{
int a=-3, b=2, c=0, d; B. 1, 2, 1, 0
d = ++a|| ++b && ++c;
printf("%d, %d, %d, %d\n", a, b, c, C. -2, 2, 0, 0
d); return 0;
D. -2, 2, 0, 1
}
Answer: Option D
Explanation:
Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type
and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed
because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it
returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of
variable 'i' only increemented by '1'(one). The variable j,k are not increemented.
Hence the output is "-2, 2, 0, 1".
Expressions in C
#include<stdio.h>
int main()
A. 1, 0, 1
{
static int x[20]; B. 1, 1, 1
int i = 0;
x[i] = i ; C. 0, 0, 0
printf("%d, %d, %d\n", x[0], x[1], i);
D. 0, 1, 0
return 0;
}
Answer: Option C
Explanation:
Step 1: 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).
#include<stdio.h>
int main()
A.200
{
int k, num=30; B.30
k = (num>5 ? (num <=10 ? 100 : 200):
500); C.100
printf("%d\n", num);
D.500
return 0;
}
Answer: Option B
Explanation:
Step 1: int k, num=30; here variable k and num are declared as an integer type and
variable num is initialized to '30'.
Step 2: k = (num>5 ? (num <=10 ? 100 : 200): 500); This statement does not affect
the output of the program. Because we are going to print the variable num in the
next statement. So, we skip this statement.
#include<stdio.h>
int main() A. 4
{ int l=2; B. 7
int m = l + (1, 2, 3, 4, 5);
printf("%d\n", m); C. 6
return 0;
} D. 5
Answer: Option B
Explanation:
Because, comma operator used in the expression i (1, 2, 3, 4, 5). The comma
operator has left-right associativity. The left operand is always evaluated first, and
the result of evaluation is discarded before the right operand is evaluated. In this
expression 5 is the right most operand, hence after evaluating expression (1, 2, 3,
4, 5) the result is 5, which on adding to i results into 7.
Expressions in C
#include<stdio.h>
int main()
{
int i=4, j=-1, k=0, a, b, c, d; A. 1, 1, 1, 1
a= i || j || k;
b = i && j && k;
B. 1, 1, 0, 1
c = i || j &&k; C. 1, 0, 0, 1
d = i && j || k;
printf("%d, %d, %d, %d\n", a, b, c, d); D. 1, 0, 1, 1
return 0;
}
Answer: Option D
Explanation:
Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0
Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".
Functions
C Functions
Function Aspects
• Function declaration A function must be declared globally in a c program to tell the compiler
about the function name, function parameters, and return type.
•
Function call Function can be called from anywhere in the program. The parameter list must
not differ in function calling and function declaration. We must pass the same number of
functions as it is declared in the function declaration.
•
Function definition It contains the actual statements which are to be executed. It is the most
important aspect to which the control comes when the function is called. Here, we must notice
that only one value can be returned from the function.
C Functions
Function Aspects
SN C function aspects Syntax
syntax
return_type function_name(data_type parameter...){
//code to be executed
}
C Functions
Types of Functions
Explanation:
Explanation:
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.
Functions in C
Explanation:
Step 1: int i=1; The variable i is declared as an integer type and initialized
to 1(one).
Step 2: if(!i) Here the !(NOT) operator reverts the i value 1 to 0. Hence
the if(0) condition fails. So it goes to else part.
Step 3: else { i=0; In the else part variable i is assigned to value 0(zero).
Step 4: printf("C-Program"); It prints the "C-program".
Step 5: main(); Here we are calling the main() function.
After calling the function, the program repeats from step 1 to step
5 infinitely.
Hence it prints "C-Program" infinitely.
Functions in C
Explanation:
Step 1: int i=0; The variable i is declared as in integer type and initialized
to '0'(zero).
Step 2: i++; Here variable i is increemented by 1. Hence i becomes
'1'(one).
Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and
it enter into if block statements.
Step 4: printf("SMART"); It prints "SMART".
Step 5: exit(1); This exit statement terminates the program execution.
Hence the output is "SMART".
Pointers
C Pointers
• Declaring a pointer
• The pointer in c language can be declared using * (asterisk symbol). It
is also known as indirection pointer used to dereference a pointer.
Pointer Example
C Pointers
• As you can see in the above figure, pointer variable stores the address
of number variable, i.e., fff4. The value of number variable is 50. But the
address of pointer variable p is aaa3.
• By the help of * (indirection operator), we can print the value of
pointer variable p.
C Pointers
A. char a = *malloc(100);
B. char *a = (char) malloc(100);
C. char *a =(char*)malloc(100);
D. char *a = (char*)(malloc*)(100);
Answer: Option C
Explanation:
Prototype of malloc is
#include<stdio.h>
A. 30
int main()
{ B. 27
int i=3, *j, k;
C. 9
j = &i;
printf("%d\n", i**j*i+*j); D. 3
return 0;
}
Answer: Option A
Explanation:
i**j*i+*j
above line executed in following steps
1>i**j
3**3=9
2>i**j*i
9*3=27(9 from the previous step)
3>i**j*i+*j
27+*j=27+*j=27+3=30
C Pointers
# include <stdio.h>
(A) 30
void fun(int a)
{ (B) 20
a= 30;
(C) Compiler Error
}
(D) Runtime Error
int main()
{
int b = 20;
fun(b);
printf("%d", b);
return 0;
}
Answer: (B)
Assume that float takes 4 bytes, predict the output of following program.
#include <stdio.h>
(A) 90.500000
int main()
3
{
(B) 90.500000
float a[5] = {12.5, 10.0, 13.5, 90.5,
12
0.5};
(C) 10.000000
float *p1 = &a[0];
12
float *p2 = p1 + 3;
(D) 0.500000
printf("%f ", *p2);
3
printf("%d", p2 - p1);
return 0;
}
Answer: (A)
While declaring string, size is not mandatory. So we can write the above
code as given below:
char ch[]={‘S', ‘M', ‘A', ‘R', ‘T', '\0'};
C Pointers
#include<stdio.h> #include<string.h>
int main()
{
char s1[20] = "Hello", s2[20] = " World";
printf("%s\n", strcpy(s2, strcat(s1, s2)));
return 0;
}
A. Hello
B. World
C. Hello World
D. WorldHello
Answer: Option C
Explanation:
Step 1: char str1[20] = "Hello", str2[20] = " World"; The
variable str1 and str2 is declared as an array of characters and initialized
with value "Hello" and " World" respectively.
Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be
stored in str1. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the
variable str2.
Hence it prints "Hello World".
C Pointers
#include<stdio.h>
#include<string.h>
int main()
{
printf("%d\n", strlen("123456"));
return 0;
}
A. 6
B. 12
C. 7
D. 2
Answer: Option A
Explanation:
The function strlen returns the number of characters in the given string.
Therefore, strlen("123456") returns 6.
Hence the output of the program is "6".
C Pointers
#include<stdio.h>
#include<string.h>
int main()
{ A. 0
static char str1[] = "dills"; B. 1
static char str2[20]; C. 2
static char str3[] = "Daffo"; D. 4
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)),
"Daffodills");
printf("%d\n", i);
return 0;
}
Answer: Option A
Explanation:
step 1: strcpy(str2,str1) cpoy the str1="dills" into str2.
#include<stdio.h>
#include<string.h> A. 8
int main() B. 0
{ C. 16
static char a[] = "Hello!"; D. Error
printf("%d\n", *(a+strlen(a)));
return 0;
}
Answer: Option B
Explanation:
The expression *(s+strlen(s)) will yeild *(s+6), because s[]="hello!";
contains 6 characters, so the length is 6.
hence the output gives zero value because ascii value of \0 is zero.
Bitwise Operators
What are Bitwise Operators?
• Bitwise operators are used for manipulating a data at the bit level, also
called as bit level programming. Bit-level programming mainly consists
of 0 and 1. They are used in numerical computations to make the
calculation process faster.
Operator Meaning
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise exclusive OR operator
~ Binary One's Complement Operator is a unary operator
<< Left shift operator
>> Right shift operator
C Pointers
#include<stdio.h>
int main() A. 0
{ B. 256
unsigned char ia= 0x80; C. 100
printf("%d\n", a<<1); D. 80
return 0;
}
Answer: Option B
Explanation:
#include<stdio.h>
int main() A. Ffff
{ unsigned int a=0xffff; B. 0000
~a; C. 00ff
printf("%x\n", a); D. ddfd
return 0;
}
Answer: Option A
Explanation:
0xfff = 1111 1111 1111 1111
and ~a is not a=~a
~a dose not affect the value of a
C Pointers
#include<stdio.h>
int main() A. 32
{ unsigned int r; B. 64
r = (64 >>(2+1-2)) & (~(1<<2)); C. 0
printf("%d\n", r); D. 128
return 0;
}
Answer: Option A
Explanation:
(64>>(2+1-2)) & (~(1<<2)) becomes
(64>>1) & (~(1<<2)).
In binary,
(0100 0000>>1) & (~(0000 0001<<2)
(0010 0000) & (~(0000 0100))
0010 0000 & 1111 1011 ( ~ equal to NOT )
0010 0000 = 32 (in decimal)
C Pointers
#include<stdio.h>
int main() A. 4, 8, 0
{ B. 1, 2, 1
int i=4, j=8; C. 12, 1, 12
printf("%d, %d, %d\n", i|j&j|i, i| D. 0, 0, 0
j&&j|i, i^j);
return 0;
}
Answer: Option C
Explanation:
The solution is :
#define P printf("%d\n", -
1^~0); #define M(P) A. 1
int main()\ B. 0
{ C. -1
\ P\ return 0;\ D. 2
}
M(P)
Answer: Option B
Explanation:
10000000 00000000 00000000 00000001
11111111 11111111 11111111 11111111 (after negation or inverting)
for 0^0 = 0
1^1 = 0
#include<stdio.h>
int main() A. 128
{ B. Garbage value
int a=128; C. Error
const int a=b; D. 0
printf("%d\n", a);
return 0;
}
Answer: Option A
Explanation:
Step 1: int y=128; The variable 'y' is declared as an integer type and
initialized to value "128".
Step 2: const int x=y; The constant variable 'x' is declared as an integer
and it is initialized with the variable 'y' value.
Step 3: printf("%d\n", x); It prints the value of variable 'x'.
Hence the output of the program is "128"
Const Qualifier in C
#include<stdio.h>
int main() A. 5
{ B. 10
const int x=5; C. Error
const int *p; p = &x; D. Garbage value
*p = 10;
printf("%d\n", x);
return 0;
}
Answer: Option C
Explanation:
Step 1: const int x=5; The constant variable x is declared as an integer
data type and initialized with value '5'.
Step 2: const int *ptrx; The constant variable ptrx is declared as an integer
pointer.
Step 3: ptrx = &x; The address of the constant variable x is assigned to
integer pointer variable ptrx.
Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the
constant vaiable x. This will result in an error.
To change the value of const variable x we have to use *(int *)&x = 10;
Const Qualifier in C
#include<stdio.h>
int fun(int **ptr);
int main()
{ A. i= FFE2 ptr=12 j=FFE4 ptr=24
int i=10, j=20; B. i = FFE4 ptr=10 j=FFE2 ptr=20
const int *ptr = &i;
printf(" i = %5X", ptr); C. i= FFE0 ptr=20 j=FFE1 ptr=30
printf(" ptr = %d", *ptr); D. Garbage value
ptr = &j;
printf(" j = %5X", ptr);
printf(" ptr = %d", *ptr);
return 0;
}
Answer: Option B
Explanation:
const int *ptr;
int const* ptr;
It means pointer points to const integer value. that means we cant change
the value of that integer but pointer can change its pointing location.
It means that pointer is constant here, it can point to an integer and then it
cant change its pointing location.
Const Qualifier in C
#include<stdio.h>
int get();
int main() A. Garbage value
{ B. Error
const int a = get(); C. 20
printf("%d", a); D. 0
return 0;
}
int get()
{
return 20;
}
Answer: Option C
Explanation:
Step 1: int get(); This is the function prototype for the funtion get(), it tells
the compiler returns an integer value and accept no parameters.
Step 2: const int x = get(); The constant variable x is declared as an
integer data type and initialized with the value "20".
The function get() returns the value "20".
Step 3: printf("%d", x); It prints the value of the variable x.
Hence the output of the program is "20".
Library Functions
Library Functions
Variable arguments
<stdarg.h>
handling functions
Standard Input/Output
<stdio.h>
functions
#include<stdio.h> A. How r u
int main() 7
{ 2
int a; B. How r u
a = printf("How r u\n"); 8
a = printf("%d\n", a); 2
printf("%d\n", a); C. How r u
return 0; 1
} 1
D. Error: cannot assign printf to
variable
Answer: Option B
Explanation:
In the program, printf() returns the number of charecters printed on the consolei
= printf("How r u\n"); This line prints "How r u" with a new line character
and returns the length of string printed then assign it to variable i.
So i = 8 (length of '\n' is 1).
i = printf("%d\n", i); In the previous step the value of i is 8. So it prints "8" with a
new line character and returns the length of string printed then assign it
to variable i. So i = 2 (length of '\n' is 1).
printf("%d\n", i); In the previous step the value of i is 2. So it prints "2".
Library Functions
#include<stdio.h>
A. 2, 3
#include<math.h>
B. 2.000000, 3
int main()
C. 2.000000, 0
{
D. 2, 0
float a = 2.5;
printf("%f, %d", floor(a), ceil(a));
return 0;
}
Answer: Option C
Explanation:
Both ceil() and floor() return the integer found as a double.
floor(2.5) returns the largest integral value(round down) that is not
greater than 2.5. So output is 2.000000.
ceil(2.5) returns 3, while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.
Library Functions
#include<stdio.h>
#include<stdlib.h> A. 55, 55.555
int main() B. 66, 66.666600
{ C. 65, 66.666000
char *i = "55.555"; D. 55, 55
int r1 = 10;
float r2 = 11.111;
r1 = r1+atoi(i); r2 = r2+atof(i);
printf("%d, %f", r1, r2);
return 0;
}
Answer: Option C
Explanation:
Function atoi() converts the string to integer.
Function atof() converts the string to float.
result1 = result1+atoi(i);
Here result1 = 10 + atoi(55.555);
result1 = 10 + 55;
result1 = 65;
result2 = result2+atof(i);
Here result2 = 11.111 + atof(55.555);
result2 = 11.111 + 55.555000;
result2 = 66.666000;
So the output is "65, 66.666000" .
Library Functions
#include<stdio.h>
#include<string.h> A. Missed
int main() B. Got it
{ C. Error in memcmp statement
char dest[] = {97, 97, 0}; D. None of above
char src[] = "aaa";
int i;
if((i = memcmp(dest, src, 2))==0)
printf("Got it");
else
printf("Missed");
return 0;
}
Answer: Option B
Explanation:
memcmp compares the first 2 bytes of the blocks dest and src as unsigned
chars. So, the ASCII value of 97 is 'a'.
• Control statements enable us to specify the flow of program control; ie, the order
in which the instructions in a program must be executed. They make it possible to
make decisions, to perform tasks repeatedly or to jump from one section of code to
another.
#include<stdio.h>
int main() A. b = 300 c = 200
{ B. b = 100 c = garbage
int x= 500, y= 100, z; C. b = 300 c = garbage
if(!x >= 400) D. b = 100 c = 200
y = 300;
z = 200; printf(“y = %d z = %d\n",
y, z);
return 0;
}
Answer: Option D
Explanation:
Initially variables a = 500, b = 100 and c is not assigned.
Step 1: if(!a >= 400)
Step 2: if(!500 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.
Step 5: So, variable c is assigned to a value '200'.
Step 6: printf("b = %d c = %d\n", b, c); It prints value of b and c.
Hence the output is "b = 100 c = 200"
Control statements
#include<stdio.h>
int main() A. Infinite loop
{ B. 0 1 2 ... 65535
unsigned int a = 65535; /* Assume C. 0 1 2 ... 32767 - 32766 -32765 -
2 byte integer*/ 10
while(a++ != 0) [Link] output
printf("%d",++a);
printf("\n");
return 0;
}
Answer: Option A
Explanation:
Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.
Step 1:unsigned int i = 65535;
Step 2:
Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied.
Then the printf("%d", ++i); prints '1'(variable 'i' is already incremented by '1' in while statement and now
incremented by '1' in printf statement) Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence
the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already incremented by
'1' in while statement and now incremented by '1' in printf statement)
....
The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'.
Control statements
#include<stdio.h>
int main() A. y =20
{ B. x = 0
int x = 10, y = 20; C. x = 10
if(!(!x) && x) D. x = 1
printf("x = %d\n", x);
else
printf("y = %d\n", y);
return 0;
}
Answer: Option C
Explanation:
The logical not operator takes expression and evaluates to true if the expression
is false and evaluates to false if the expression is true. In other words it
reverses the value of the expression.
Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
Control statements
What will be the output of the program, if a short int is 2 bytes wide?
#include<stdio.h>
A. 1 ... 65535
int main()
B. Expression syntax error
{
C. No output
short int a = 0;
D. 0, 1, 2, 3, 4, 5
for(a<=5 && a>=-1; ++a; a>0)
printf("%u,", a);
return 0;
}
Answer: Option A
Explanation:
for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop.
expression ++i is the loop condition. expression i>0 is the increment
expression.
In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.
Loop condition always get evaluated to true. Also at this point it increases i by
one.
An increment_expression i>0 has no effect on value of [Link] for loop get executed
till the limit of integer (ie. 65535)
Arrays
Arrays
• An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which
can store the primitive type of data such as int, char, double, float, etc. It also has the
capability to store the collection of derived data types, such as pointers, structure, etc.
• The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
• C array is beneficial if you have to store similar elements.
• For example, if we want to store the marks of a student in 6 subjects, then we don't need
to define different variables for the marks in the different subject. Instead of that, we can
define an array which can store the marks in each subject at the contiguous memory
locations.
• By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
Arrays
Properties of Array
The array contains the following properties.
1. Each element of an array is of same data type and carries the same size, i.e., int
= 4 bytes.
2. Elements of the array are stored at contiguous memory locations where the
first element is stored at the smallest memory location.
3. Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size
of the data element.
Arrays
Declaration of C Array
• We can declare an array in the c language in the following way.
data_type array_name[array_size];
Example
int marks[5];
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We
can initialize each element of the array by using the index. Consider the following
marks[0]=80;//
example.
initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Arrays
#include<stdio.h>
int main() A.2, 1, 15
{
B.1, 2, 5
int a[5] = {5, 1, 15, 20, 25};
int i, j, m; i = ++a[1]; C.3, 2, 15
j = a[1]++;
D.2, 3, 20
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
Answer: Option C
Explanation:
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size
of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++
so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
Arrays
#include<stdio.h>
void fun(int **p);
int main() A. 1
{ B. 2
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9,
0}; C. 3
int *ptr; ptr = &a[0][0]; D. 4
fun(&ptr); return 0;
}
void fun(int **p)
{
printf("%d\n", **p);
}
Answer: Option A
Explanation:
Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an multidimensional
integer array with size of 3 rows 4 columns.
Step 2: int *ptr; The *ptr is a integer pointer variable.
Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer
variable *ptr.
Step 4: fun(&ptr); Now, the &ptr contains the base address of array a.
Step 4: Inside the function fun(&ptr); The printf("%d\n", **p); prints the value '1'.
because the *p contains the base address or the first element memory address of the array a (ie. a[0])
**p contains the value of *p memory location (ie. a[0]=1).
Hence the output of the program is '1'
Arrays
What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>
int main()
{ A.1200, 1202, 1204
int arr[]={2, 3, 4, 1, 6}; B.1200, 1200, 1200
printf("%u, %u, %u\n", arr, &arr[0],
&arr); C.1200, 1204, 1208
return 0; D.1200, 1202, 1200
}
Answer: Option B
Explanation:
Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array
and initialized.
Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here,
The base address of the array is 1200.
=> arr, &arr is pointing to the base address of the array arr.
=> &arr[0] is pointing to the address of the first element array arr. (ie. base
address)
Hence the output of the program is 1200, 1200, 1200
Arrays
#include<stdio.h>
int main()
{ A.5
float arr[] = {12.4, 2.3, 4.5, 6.7}; B.4
printf("%d\n",
sizeof(arr)/sizeof(arr[0])); C.6
return 0; D.7
}
Answer: Option B
Explanation:
The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes
Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point
array and it is initialized with the values.
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0]));
The variable arr has 4 elements. The size of the float variable is 4 bytes.
Hence 4 elements x 4 bytes = 16 bytes
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is '4'.
Arrays
#include<stdio.h>
int main()
{ A.1
int arr[1]={10}; B.10
printf("%d\n", 0[arr]);
return 0; C.0
} D.6
Answer: Option B
Explanation:
Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array
with size '2' and it's first element is initialized to value
'10'(means arr[0]=10)
Step 2: printf("%d\n", 0[arr]); It prints the first element value of the
variable arr.
Hence the output of the program is 10.
Answer: Option B
Explanation:
Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array
with size '2' and it's first element is initialized to value
'10'(means arr[0]=10)
Step 2: printf("%d\n", 0[arr]); It prints the first element value of the
variable arr.
Hence the output of the program is 10.
Structures, Unions, Enums
What is Structure
struct employee
{ int id;
char name[20];
float salary;
};
Union in c
• The enum keyword is also used to define the variables of enum type.
There are two ways to define the variables of enum type as follows.
enum week{sunday, monday, tuesday, wednesday,
thursday, friday, saturday}; enum week day;
Structures, Unions, Enums
#include<stdio.h>
int main()
{ A.1, 2, 13
struct value
{ B.1, 4, 4
int bit1:1; C.-1, 2, -3
int bit3:4;
int bit4:4; D.-1, -2, -13
}
bit={1, 2, 13};
printf("%d, %d, %d\n", bit.bit1,
bit.bit3, bit.bit4);
return 0;
}
Answer: Option C
Explanation:
Bit1: 1 means it will be stored only one bit like (eg: 1 binary stored in 1 bit only).
So in a first bit it will take left as well as right most bit is 1 because here only 1 bit memory available.
Bit2: 4 means it will be stored 4 bit like (eg: 2 binary stored in 4 bit only).
So 4 = 0100, here left most bit is 0 that's why we didn't took 2's complement here and print value 2 as is it.
Bit3: 4 means it will be also stored 4 bit like (eg: 13 binary stored in 4 bit only).
So 13 = 1101, here left most bit is 1 that's why we took 2's complement here and print changeable value.
Structures, Unions, Enums
#include<stdio.h>
int main() {
union a A. 3, 2, 515
{
int i; char ch[2]; B. 515, 2, 3
}; C. 3, 2, 5
union a u;
[Link][0]=3; D. 515, 515, 4
[Link][1]=2;
printf("%d, %d, %d\n", [Link][0],
[Link][1], u.i);
return 0;
}
Answer: Option A
Explanation:
The system will allocate 2 bytes for the union.
The statements [Link][0]=3; [Link][1]=2; store data in memory as given
below.
Answer: Option D
Explanation:
enum days={MON=-1,TUE,WED=6,THU,FRI,SAT}
so the value for monday = -1;
usually enum ill take the values sequentially, dats if enum month={a=0,b};
her the value for b=1(dats next of 1)
like wise the value for TUE=0;
den wed=6(ITS ALREADY GIVEN),therefore next value s 7 hence
THU=7,DEN FRI=8,SAT=9
Structures, Unions, Enums
What will be the output of the program given below in 16-bit platform ?
#include<stdio.h>
int main()
{ A. 1
enum value{
VAL1=0, VAL2, VAL3, VAL4, VAL5 B. 2
} var; C. 4
printf("%d\n", sizeof(var));
return 0; D. 10
}
Answer: Option B
Explanation:The reason is that value of each enum element must lie within the range of int.
For example:
enum status{pass,fail}var;
enum var jhon=pass;
printf("%d",sizeof(jhon));
Will print 2 bcause the values in pass, fail are treated as integer by the compiler like 0, 1,
2, ... and so on.
Thats why the size is 2 under TurboC compiler and 4 in GCC compiler.
Structures, Unions, Enums
#include<stdio.h>
int main()
{ A. 12, 12, 12
int i=4, j=8;
printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, B. 112, 1, 12
i^j); C. 32, 1, 12
return 0;
} D. -64, 1, 12
Answer: Option A
Explanation:Here &, | and ^ are bitwise operators,then operations will be
performed on operands at bit level.