0% found this document useful (0 votes)
5 views164 pages

C Program Execution Without Main Function

r22 btech c programming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views164 pages

C Program Execution Without Main Function

r22 btech c programming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C Programming Language

Interview Question in C

 Can we run a program without a main function in C?


 Yes
 No
Interview Question in C

 Yes! we can run a program without a main function in C


The C preprocessor is a micro processor that is used by compiler
to transform your code before compilation. It is called micro
preprocessor because it allows us to add macros.

Macro is defined by #define directive.


It shows how a programmer can defy the very important rule of having a
main() in c program and still make the program run. This illustrates the
concept on a simple program though it can be scaled to much bigger and
more complex programs.

#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).

The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the


4th,1st,3rd & the 2nd characters(tokens)
Now look at the third line of the program –
#define begin decode(a,n,i,m,a,t,e)
Here the preprocessor replaces the macro “begin” with the expansion
decode(a,n,i,m,a,t,e). According to the macro definition in the previous line the
argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be
merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’, ’a’, ’i’
& ‘n’.

So the third line “int begin” is replaced by “in


t main” by the preprocessor before the program is passed on for the compiler.
That’s it…

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

Yes! we can run a program without a semicolon in C & Java?

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

•The scanf("%d",&number) statement reads integer number from


the console and stores the given value in number variable.

•The printf("cube of number is:%d


",number*number*number) statement prints the cube of
number on the console.
Variables in C

• A variable is a name of the memory location. It is used to store data.


Its value can be changed, and it can be reused many times.
• It is a way to represent memory location through symbol so that it can
be easily identified.

syntax to declare a variable:


type variable_list;

• example of declaring the variable


•int a;
•float b;
•char c;
Here, a, b, c are variables. The int, float, char are the
data types.
Variables in C

Rules for defining variables

• A variable can have alphabets, digits, and underscore.


• A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int,
float, etc.
Variables in C

Types of Variables in C

There are many types of variables in c:


1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
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
}

• You must have to initialize the local variable before it is used.


Variables in C

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

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


Data Types in C
Data Types Memory Size Range

char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

unsigned short 2 byte 0 to 65,535

int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

float 4 byte

double 8 byte

long double 10 byte


Keywords in C

• A keyword is a reserved word. You cannot use it as a variable name,


constant name, etc. There are only 32 reserved words (keywords) in
the C language.
• A list of 32 keywords in the c language is given below:

auto break case char const continue default do

double else enum extern float for goto if


int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while


C Identifiers

• C identifiers represent the name in the C program, for example, variables,


functions, arrays, structures, unions, labels, etc.
• An identifier can be composed of letters such as uppercase, lowercase
letters, underscore, digits, but the starting letter should be either an
alphabet or an underscore.
Internal identifier:
• If the identifier is not used in the external linkage, then it is called as an
internal identifier.
External identifier:
• If the identifier is used in the external linkage, then it is called as an external
identifier. total, sum, average, _m _, sum_1, etc.
• Example
Topics
 Declarations and Initializations  Control Instructions
 Expressions  Arrays
 Functions  Structures, Unions, Enums
 Pointers
 Strings
 Bitwise Operators
 Const Qualifier
 Library Functions
Declarations and Initializations
Declaration & Initialization

• C variables are names used for storing a data value to locations in


memory. The value stored in the c variables may be changed during
program execution.
Declaration of Variable
Declaration of variable in c can be done using following syntax:
data_type variable_name;
or
data_type variable1, variable2,…,variablen;
• Example,
int a;
float variable;
float a, b;
Declaration & Initialization
Initialization of Variable
• C variables declared can be initialized with the help of assignment operator ‘=’.
Syntax
data_type variable_name=constant/literal/expression;
or
variable_name=constant/literal/expression;

Examples, int a=10;


int a=b+c;
a=10;
a=b+c;

Multiple variables can be initialized in a single statement by single value, for


example, a=b=c=d=e=10;
Declarations and Initializations

What is the output of the program?

#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

What is the output of the program?

#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

What is the output of the program?


#include<stdio.h>
int main()
{
int a= 10, b = 20, c = 5, d; d = a < b < c;
printf("%d\n", d);
return 0;
}

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

What is the output of the program?


#include<stdio.h>
int main() {
int x[5] = {2, 3};
printf("%d, %d, %d\n", x[2], x[3],
x[4]); return 0;
}

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

What is the output of the program?

#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

• An expression is a formula in which operands are linked to each other


by the use of operators to compute a value. An operand can be a
function reference, a variable, an array element or a constant.
• Example: a-b

• In the above expression, minus character (-) is an operator, and a, and


b are the two operands.
Expressions in C

There are four types of expressions exist in C:


1. Arithmetic expressions
2. Relational expressions
3. Logical expressions
4. Conditional expressions
Expressions in C

What is the output of the program?

#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

What is the output of the program?

#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).

Step 2: int i = 0; here vaiable i is declared as an integer type and initialized to


'0'(zero).

Step 3: a[i] = i ; becomes a[0] = 0;

Step 4: printf("%d, %d, %d\n", a[0], a[1], i);


Here a[0] = 0, a[1] = 0(because all staic variables are initialized to '0') and i = 0.

Step 4: Hence the output is "0, 0, 0".


Expressions in C

What is the output of the program?

#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.

Step 3: printf("%d\n", num); It prints the value of variable num '30‘

Hence the output of the program is '30'


Expressions in C

What is the output of the program?

#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

What is the output of the program?

#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 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an


integer type and the variable i, j, k are initialized to 4, -1, 0 respectively.

Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1

Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0

Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1

Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.

Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".
Functions
C Functions

• In c, we can divide a large program into the basic building blocks


known as function.
• The function contains the set of programming statements enclosed by
{}.
• A function can be called multiple times to provide reusability and
modularity to the C program.
• In other words, we can say that the collection of functions creates a
program. The function is also known as procedureor subroutinein
other programming languages.
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

1 Function declaration return_type function_name


(argument list);
2 Function call function_name
(argument_list)
3 Function definition return_type function_name
(argument list) {function
body;}

syntax
return_type function_name(data_type parameter...){
//code to be executed
}
C Functions

Types of Functions

There are two types of functions in C programming:


• Library Functions: are the functions which are declared in the C
header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

• User-defined functions: are the functions which are created by the C


programmer, so that he/she can use it many times. It reduces the
complexity of a big program and optimizes the code.
Functions in C

What is the notation for following functions?

1. int f(int x, float y) { /* Some A.1. KR Notation


code */ }
2. ANSI Notation
2. int f(x, y) int x; float y; { /* Some
code */ } B.1. Pre ANSI C Notation
2. KR Notation
C.1. ANSI Notation
2. KR Notation
D.1. ANSI Notation
2. Pre ANSI Notation
Answer: Option C

Explanation:

KR Notation means Kernighan and Ritche Notation.


In KR notation first we need to give prototype declaration then function
call then comes function definition where as in ASCII notation 1st we need
to define the the function defination and then function call.
Functions in C

How many times the program will print “SMART" ?

#include<stdio.h> A. Infinite times


int main()
{ B. 32767 times
printf(“SMART"); C. 65535 times
main();
return 0; D. Till stack overflows
}
Answer: Option D

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

What will be the output of the program?

#include<stdio.h> A. prints "SMART, C-Program“


int main()
{ int a=1; infinitely
if(!a) printf(“SMART,"); B. prints "C-Program" infinetly
else
{ C. prints "C-Program, SMART“
a=0; infinitely
printf("C-Program");
main(); D. Error: main() should
} Not inside else statement
return 0;
}
Answer: Option B

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

What will be the output of the program?


#include<stdio.h>
int addmult(int ii, int jj)
{ int kk, ll; A. 12, 12
kk = ii + jj; B. 7, 7
ll = ii * jj;
return (kk, ll); C. 7, 12
} D. 12, 7
int main()
{
int i=3, j=4, k, l;
k = addmult(i, j);
l = addmult(i, j);
printf("%d, %d\n", k, l);
return 0;
}
Answer: Option A
Explanation:
Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer
type and variable i, j are initialized to 3, 4 respectively.
The function addmult(i, j); accept 2 integer parameters.
Step 2: k = addmult(i, j); becomes k = addmult(3, 4)
In the function addmult(). The variable kk, ll are declared as an integer
type int kk, ll;
kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.
ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.
return (kk, ll); It returns the value of variable ll only.
The value 12 is stored in variable 'k'.
Step 3: l = addmult(i, j); becomes l = addmult(3, 4)
kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.
ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.
return (kk, ll); It returns the value of variable ll only.
The value 12 is stored in variable 'l'.
Step 4: printf("%d, %d\n", k, l); It prints the value of k and l
Hence the output is "12, 12".
Functions in C

What will be the output of the program?


#include<stdio.h>
#include<stdlib.h>
int main() A. Prints "SMART" 5 times
{ B. Function main() doesn't calls
int a=0;
a++; itself
if(a<=5) C. Infinite loop
{
printf("SMART"); D. Prints "SMART"
exit(1);
main();
}
return 0;
}
Answer: Option D

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

• The pointer in C language is a variable which stores the address of


another variable. This variable can be of type int, char, array, function,
or any other pointer. The size of the pointer depends on the
architecture. However, in 32-bit architecture the size of a pointer is 2
byte.
• Consider the following example to define a pointer which stores the
address of an integer.
int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the
address of the variable n of type integer.
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.

int *a;//pointer to int


char *c;//pointer to char

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

Can you combine the following two statements into one?


char *a;
a = (char*) malloc(100);

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

ptr = (data type *)malloc(size);


- where ptr is pointer of type datatype.

So in the above example as we need to allocate memory for char it can be


done with the following statement:

char *p = (char*)malloc(1000); // here p is a pointer of type char


C Pointers

What will be the output of the program ?

#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

What will be the output of the program ?

# 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)

Explanation: Parameters are always passed by value in C. Therefore, in


the above code, value of y is not modified using the function fun(). So how
do we modify the value of a local variable of a function inside another
function. Pointer is the solution to such problems. Using pointers, we can
modify a local variable of a function inside another function. See the next
question.
Note that everything is passed by value in C. We only get the effect of pass
by reference using pointers.
C Pointers

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)

Explanation: When we add a value x to a pointer p, the value of the


resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of
data type pointed by p. That is why ptr2 is incremented to point to arr[3]
in the above code. Same rule applies for subtraction. Note that only
integral values can be added or subtracted from a pointer. We can also
subtract or compare two pointers of same type.
Strings
C Strings

• The string can be defined as the one-dimensional array of characters terminated by


a null ('\0'). The character array or the string is used to manipulate text such as
word or sentences. Each character in the array occupies one byte of memory, and
the last character must always be 0. The termination character ('\0') is important
in a string since it is the only way to identify where the string ends. When we define
a string as char s[10], the character s[10] is implicitly initialized with the null in the
memory.

There are two ways to declare a string in c language.


1. By char array
2. By string literal
C Strings

Example of declaring string by char array

char ch[10]={‘S', ‘M', ‘A', ‘R', ‘T', '\0'};

As we know, array index starts from 0, so it will be represented as in the


figure given below.
0 1 2 3 4
S M A R T

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

What will be the output of the program ?

#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

What will be the output of the program ?

#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

What will be the output of the program ?

#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.

step 2: strcat(str3,str2) append the dills at the end of Daffo.

step 3: strcmp(str3,"Daffodils") returns the no. of charecters of str3 which


are not find in Daffodills.

step 4: since both strings are same so results will be 0(zero)


C Pointers

What will be the output of the program ?

#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.

and the expression *(s+6)= s[6]. (acc. to pointers to array theory).

now in a strng the last element/bit always contains '\0'.


so, s[0]= 'H'
s[1]= 'e'
s[2]= 'l'
s[3]= 'l'
s[4]= 'o'
s[5]= '!'
s[6]= '\0'

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

What will be the output of the program ?

#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:

0x88 means 128 in decimal hence


0000 0000 1000 0000
now left shift 1 byte means
0000 0001 0000 0000=256
C Pointers
If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#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

What will be the output of the program?

#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

What will be the output of the program?

#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 :

Binary format of 4 = 0100 and 8 = 1000

Therefore 4|8 is = 0100|1000 = 1100 = 12

Similary 8|4 is also 1100

now i|j & j|i=1100 & 1100 =1100=12

Then i|j && j|i =12&&12 condition is true .. so it return 1.

Then i^j = 0100^1000 = 1100 = 12.


C Pointers

What will be the output of the program?

#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

So the answer is 0 (zero).


Const Qualifier in C
Const Qualifier in C

• The qualifier const can be applied to the declaration of any variable to


specify that its value will not be changed ( Which depends upon where
const variables are stored, we may change the value of const variable
by using pointer ). The result is implementation-defined if an attempt
is made to change a const.
• Examples
const int *ptr;
int const *ptr;
Const Qualifier in C

What will be the output of the program?

#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

What will be the output of the program?

#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

What will be the output of the program?

#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.

int * const ptr;


const * int ptr;

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

What will be the output of the program?

#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

• C Standard library functions or simply C Library functions are inbuilt


functions in C programming.
• The prototype and data definitions of these functions are present in
their respective header files. To use these functions we need to include
the header file in our program. For example,
• If you want to use the printf() function, the header file <stdio.h> should
be included.
• If you try to use printf() without including the stdio.h header file, you
will get an error.
C Header Files
Library Functions
<assert.h> Program assertion functions

Library Functions in Different <ctype.h> Character type functions

<locale.h> Localization functions


Header Files
<math.h> Mathematics functions

<setjmp.h> Jump functions

<signal.h> Signal handling functions

Variable arguments
<stdarg.h>
handling functions

Standard Input/Output
<stdio.h>
functions

<stdlib.h> Standard Utility functions

<string.h> String handling functions

<time.h> Date time functions


Library Functions

What will be the output of the program?

#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

What will be the output of the program?

#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

What will be the output of the program?

#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

What will be the output of the program?

#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'.

if((i = memcmp(dest, src, 2))==0) When comparing the


array dest and src as unsigned chars, the first 2 bytes are same in both
[Link] memcmp returns '0'.
Then, the if(0=0) condition is satisfied. Hence the output is "Got it".
Control Statements
Control statements

• 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.

There are five types of control statements in C:


1. If statements
2. Switch Statement
3. Conditional Operator Statement
4. Goto Statement
5. Loop Statements
Control statements

What will be the output of the program?

#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

What will be the output of the program?

#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

What will be the output of the program?

#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?


#include<stdio.h>
int main()
{
int a=4; A. This is default
switch(a) This is case 1
{ B. This is case 3
default: printf("This is default\ This is default
n"); case 1: printf("This is case C. This is case 1
1\n"); break; This is case 3
case 2: printf("This is case 2\n"); D. This is default
break;
case 3: printf("This is case 3\n");
}
return 0;
}
Answer: Option A
Explanation:
In the very begining of switch-case statement default statement is
encountered. So, it prints "This is default".
In default statement there is no break; statement is included. So it prints
the case 1 statements. "This is case 1".
Then the break; statement is encountered. Hence the program exits from
the switch-case block.
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

What will be the output of the program ?

#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

What will be the output of the program ?

#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

What will be the output of the program?

#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

What will be the output of the program?

#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

• Structure in c is a user-defined data type that enables us to store the


collection of different data types. Each element of a structure is called a
member. Structures ca; simulate the use of classes and templates as it
can store various information
• The ,struct keyword is used to define the structure. Let's see the
syntax to define the structure in c.
struct structure_name Example
{
data_type member1; struct employee
data_type member2; { int id;
. char name[20];
data_type memeberN; float salary;
}; };
Structure
Example

struct employee
{ int id;
char name[20];
float salary;
};
Union in c

• Like structure, Union in c language is a user-defined data type that is


used to store the different type of elements.
• At once, only one member of the union can occupy the memory. In
other words, we can say that the size of the union in any instance is
equal to the size of its largest element.
Enums in C

• Enumeration is a user defined datatype in C language. It is used to


assign names to the integral constants which makes a program easy to
read and maintain. The keyword “enum” is used to declare an
enumeration.
• syntax of enum in C language,
enum enum_name{const1, const2, ....... };

• 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

What will be the output of the program?

#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

What will be the output of the program?

#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

What will be the output of the program ?

#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.

& - AND , | - OR , ^ - XOR operators

(4)d = (0000100)b, (8)d = (0001000)b

& has higher precedence over |

By considering all these, you will get the answer as expected.

You might also like