You are on page 1of 118

Technical Concepts

Mr. Pramod Kumar Gupta C Language


Asst. Professor
ABC of C Language

2
Basic Concepts

• Developed in 1972 by Dennis Ritchie at Bell Telephone Laboratories (Now AT & T Bell

Laboratories).

• Confined in 1978 by Brian Kernighan and Ritchie.

• Popular due to Power, Portability, Performance and flexibility.

3
Variable and Constant

4
Basic Concepts

• A variable is a tool to reserve space in computer’s memory (RAM). The reserved

space is given a name, which we call a variable name.

• Constants are also stored in the reserve space.

• Depending on the purpose C allow us to decide how much memory to allocate to a

variable.

• char, int, float and double are the data type available in C language.

• Memory occupied by each datatype can be found out using an operator called sizeof.

• Example sizeof (int) will give 2 or 4 byte on 16 and 32 bit OS.

5
Basic Concepts (Cont..)

• Char and int can also be expressed in hexadecimal and octal notations.

• C doesn’t accept floats in hex or octal.

• Range of data type

6
Basic Concepts (Cont..)

• Length of the variable is a compiler dependent feature.

• C accept a variable name upto 32 characters.

• Generally c is having 32 keywords.

7
Operations on Data

8
Basic Concepts (Cont..)

• There are 45 operators available in c language.

9
Basic Concepts (Cont..)

• Priority of precedence of operator.

• Modulus operator is not defined for floating point values.

10
Multiple Choice Questions Practice

1. The C language consist of ____ number of keywords.

A. 32

B. 40

C. 24

D. 56

Answer : A

11
Multiple Choice Questions Practice

2. Which of the following is a keyword used for a storage class?

A. printf

B. external

C. auto

D. scanf

Answer : C

12
Basic Concepts (Cont..)

Storage class in C

13
Multiple Choice Questions Practice

3. What is storage class for variable A in below code?


void main()
{
int A;
A = 10;
printf("%d", A);
}

A. static

B. extern

C. auto

D. register

Answer : C

14
Multiple Choice Questions Practice

4. What is output of below program?


void main()
{
const int a = 10;
printf("%d",++a);
}

A. 11

B. 10

C. Compilation Error

D. None of the above

Answer : C

15
Multiple Choice Questions Practice

5. Can we modify a constant ?

A. Yes

B. No

16
Multiple Choice Questions Practice

6. Library function pow() belongs to which header file?

A. math.h

B. stdlib.h

C. conio.h

D. iomanip.h

Answer : A

17
Multiple Choice Questions Practice

7. What is the following is invalid header file in C?

A. math.h

B. mathio.h

C. ctype.h

D. string.h

Answer : B

18
Integer and Float Conversions

19
Basic Concepts

• An arithmetic operation between and integer and integer always yields an integer

result.

• An arithmetic operation between a float and float always yields a float result.

• In an arithmetic operation between an integer and float, the integer is first promoted to

float and then the operation is carried out. Result is always a float.

• On assigning a float to an integer (using assignment operator =) the float is demoted to

an integer.

• On assigning an integer to a float it is promoted to a float.

20
Basic Concepts

• Example here we assume i as an integer and f as float variable.

Operation Result Operation Result


i=5/2 f=5/2
i = 5.0 /2 f = 5.0 /2
i = 5 / 2.0 f = 5 / 2.0
i = 5.0 / 2.0 f = 5.0 / 2.0
i=2/5 f=2/5
i = 2.0/5 f = 2.0/5
i = 2/ 5.0 f = 2/ 5.0
i = 2.0 / 5.0 f = 2.0 / 5.0

21
Basic Concepts

• Example here we assume i as an integer and f as float variable.

Operation Result Operation Result


i=5/2 2 f=5/2 2.0
i = 5.0 /2 2 f = 5.0 /2 2.5
i = 5 / 2.0 2 f = 5 / 2.0 2.5
i = 5.0 / 2.0 2 f = 5.0 / 2.0 2.5
i=2/5 0 f=2/5 0.0
i = 2.0/5 0 f = 2.0/5 0.4
i = 2/ 5.0 0 f = 2/ 5.0 0.4
i = 2.0 / 5.0 0 f = 2.0 / 5.0 0.4

22
printf() and scanf()

23
Basic Concepts

• General format of printf is

• printf(“Format String”, list of variables);

• Here list of variables is optional and format string or string is compulsory.

• scanf(“Format String”, &variablename);

• In format string only format specifications such as %c, %d, %f etc. should occur.

• Variable name must always be preceded by the “address of” operator &.

24
Multiple Choice Questions Practice

8. main()

printf( “%d %d %d %d”, 72, 072, 0x72, 0X72);

Answer : 72, 58, 114, 114

25
Multiple Choice Questions Practice

9.
main()
{
printf( “\n Bytes occupied by ‘7’ = %d ”, sizeof(‘7’));
printf( “\n Bytes occupied by ‘7’ = %d ”, sizeof(7));
printf( “\n Bytes occupied by ‘7’ = %d ”, sizeof(7.0));
}

Answer : 2, 2,8

26
Multiple Choice Questions Practice

10.
main()
{
char ch = 257;
printf( “\n %d ”, ch);
printf( “\n %c ”, ch);
}

Answer :

27
Multiple Choice Questions Practice

11.
main()
{
int a,b;
a = -3 - -3;
b = -3 - -(-3);
printf( “\n a= %d ”, a);
printf( “\n b= %d ”, b);
}

Answer : 0, -6

28
Multiple Choice Questions Practice

12.
main()
{
int a;
a = 4%5 + 6%5;
printf( “\n a= %d ”, a);
}

Answer : 5

29
Control Statement

30
Basic Concepts

C has 3 major decision making media:

• If-else

• Switch

• Conditional Operator (Ternary Operator)

31
Basic Concepts (Important Points)

• In c language if a condition is satisfied or true it return or gives a value 1 and if it is

not satisfied or false it gives a value 0.

• Any non-zero number is always treated as a true, whereas zero treated as false.

• More than one condition can be combined together using logical operators &&(AND), ||

(OR )

• !(NOT) will negate the result

32
Multiple Choice Questions Practice

13.
main()
{
int x = 10, y = 5, p, q;
p = x > 9;
q = x > 3 && y!=3;
printf( “\n p= %d ”, p);
printf( “\n q= %d ”, q);
}

Answer :

33
Multiple Choice Questions Practice

14.
main()
{
int a = 30, b = 40, c;
c = (a!=10) && (b=20);
printf( “\n c= %d ”, c);
}

Answer :

34
Multiple Choice Questions Practice

14.
main()
{
int a = 500, b = 100, c;
if(!a>=400)
b=300;
c=200;
printf( “\n b= %d ”, b);
printf( “\n c= %d ”, c);
}

Answer :

35
Multiple Choice Questions Practice

15.
main()
{
int x = 10, y = -21;
x=!x;
y=!y;
printf( “\n x= %d ”, x);
printf( “\n y= %d ”, y);
}

Answer :

36
Multiple Choice Questions Practice

16.
main()
{
if(!3.14)
printf(“\n I know C language”);
else
printf(“\n I don’t know C language”);
}

Answer :

37
Multiple Choice Questions Practice

17.
main()
{
int x = 3, y = 4, z = 4;
printf(“\n ans=%d”,z >= y && y >= x ? 1 : 0);
}

Answer :

38
Multiple Choice Questions Practice

18.
main()
{
float f = 0.7;
if(f == 0.7)
printf(“\n I know”);
else
printf(“\n I don’t know”);
}

Answer :

39
Multiple Choice Questions Practice

19.
main()
{
int c = 0, d = 5, e = 10, a;
a = c > 1 ? d > 1 || e > 1 ? 100 : 200 : 300;
printf(“\n a = %d”,a);
}

Answer :

40
Repetition or Loop

41
Multiple Choice Questions Practice

20.
main()
{
char ch = 1;
while ( ch <=255)
printf(“\n%d”,ch++);
}

Answer :

42
Multiple Choice Questions Practice

21.
main()
{
int i = 1;
while ( i <=255)
printf(“\n%d”,i++);
}

Answer :

43
Multiple Choice Questions Practice

22.
main()
{
int i ;
for ( i = 1; ++i <= 5;)
printf(“%d”, i);
}

Answer :

44
Multiple Choice Questions Practice

23.
main()
{
int i =1, j=1;
for ( ; j ;)
{
printf(“\n%d %d”, i,j)
j = i++ <=5;
}
}

Answer :

45
Multiple Choice Questions Practice

24.
main()
{
int i =3;
for ( ; i-- ;)
printf(“\n%d”, i);
}

Answer :

46
Multiple Choice Questions Practice

25.
main()
{
int x=5;
x++;
printf(“\n x=%d”, x);
++x;
printf(“\n x=%d”, x);
}

Answer : 6 , 7

47
Multiple Choice Questions Practice

26.
main()
{
int x=5,z;
z=(x++)++ + 10;
printf(“\n x=%d z=%d”, x, z);

Answer :

48
Use of break and continue

49
Switch

50
Useful tips about the use of switch

 The cases need not necessarily be arranged in ascending order, 1,2,3 or ,’a’,’b’’c’.

 Even if there are multiple statements to be executed in each case, there is no need to
enclose them within a pair of braces (unlike if and else).

 The default case is optional. If it is absent, and no case matches with the value of the
expression, then the program simply falls through the entire switch and continues with
the next instruction(if any) that follows the control structure.

 The limitation of switch is that the logical operators can not be used in cases. Thus, a
case like,

case i<=20: (compiler error)

 All the value after the case is an int or a char. Even a float, double and string is not
allowed.

 In principle, a switch may occur within another, but in practice it is rarely done.

51
What will be the output of the following programs:

27.
main()
{
int i;
printf(“Enter any number”);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“I”);
case 2:
printf(“Love”);
case 3:
printf(“Programming”);
case default:
printf(“Bye”);
}

Answer: ERROR:Expression Syntax in function main

52
What will be the output of the following programs:

27.
main()
{
int i=3;
switch(i)
{
case 1:
printf(“I”);
case 2:
printf(“Love”);
case 3:
continue;
default:
printf(“Bye”);
}

Answer: ERROR: Misplace continue

Note: Continue can not work with switch and if , it only work with loop.

53
What will be the output of the following programs:

28.
#include<stdio.h>
add(int ii)
{
++ii;
return ii;
}
main()
{
int i=3,k,l;
k=add(++i);
l=add(i++);
printf("i=%d k=%d l=%d",i,k,l);
}
Answer: 5, 5, 5

54
Functioning of Functions

55
Basics of Function

 Function provide the mechanism for producing programs that are easy to write, read,
understand, debug, modify and maintain.

 A function is a self-contained block of code that performs a coherent task of some kind.

 Every C program is a collection of functions.


main()
{
f1();
printf(“\n Hello”);
} bring the control here

f1()
{ return the control back to the caller function
printf(“\n We are learning Function’);
}

56
What will be the output of the following programs:

28.
main()
{
int i=45;
float c;
c= check(i);
printf(“Valuc of c =%f”,c);
}
check( int ch)
{
ch>=45?return (3.14): return (6.28);
}

Answer: 3.000

Note: A function is by default returning an int.

57
What will be the output of the following programs:

29.
main()
{
C( )
{
c( )
{
printf(“I am in c( ) function which is inside C( ) function.”);
}
printf(“I am in C( ) function.”);
}
printf(“I am in main( ) function.”);

}
Answer: ERROR: Semicolon missing in function main

Note: A function can be called inside another function but it can not be defined in another

function.

58
What will be the output of the following programs:

30. What will be the output of the C program?


#include<stdio.h>
int main()
{
function();
return 0;
}
void function()
{
printf("Function in C is awesome");
}

Answer: Its a Compilation error, when compiler reads function(); it don't knew whether
function() is available or not. So we have to initialize the function before main function.

59
What will be the output of the following programs:

30. What will be the output of the C program?


#include<stdio.h>
int main()
{
main();
return 0;
}
Answer: Infinite Loop. It is not a recursion. The main() function repeatedly called the
main() and the program never ends.

60
Discussion over Interview
Question

61
Interview Question

1) What is the difference between declaration and definition of a


variable/function
Ans: Declaration of a variable/function simply declares that the
variable/function exists somewhere in the program but the memory is not
allocated for them. But the declaration of a variable/function serves an
important role.
And that is the type of the variable/function. Therefore, when a variable is
declared, the program knows the data type of that variable.
In case of function declaration, the program knows what are the arguments to
that functions, their data types, the order of arguments and the return type of
the function. So that’s all about declaration.
Coming to the definition, when we define a variable/function, apart from the
role of declaration, it also allocates memory for that variable/function.
Therefore, we can think of definition as a super set of declaration. (or
declaration as a subset of definition). From this explanation, it should be
obvious that a variable/function can be declared any number of times but it can
be defined only once.

62
Interview Question

2) How will you print “Hello World” without semicolon?

3) What is a static variable?


Ans: A static (local) variables retains its value between the function call and
the default value is 0. The following function will print 1 2 3 if called thrice.
void f() {
static int i;
++i;
printf(“%d “,i);
}

63
Discussion over Interview
Question (Function)

64
Interview Question

1) What is lvalue and rvalue?


Ans: The expression appearing on right side of the assignment operator is
called as rvalue. Rvalue is assigned to lvalue, which appears on left side
of the assignment operator. The lvalue should designate to a variable not a
constant.
2) Can a program be compiled without main() function?
Ans: Yes, it can be but cannot be executed, as the execution requires main()
function definition.
3) What is the difference between variable declaration and variable
definition?
Ans: Declaration associates type to the variable whereas definition gives the
value to the variable.

65
Interview Question

4) What is a token?
Ans: A C program consists of various tokens and a token is either a keyword, an
identifier, a constant, a string literal, or a symbol.
5) What is the default value of local and global variables?
Ans: A loop executing repeatedly as the loop-expression always evaluates to true such
as.
3) Can variables belonging to different scope have same name? If so show an
example.
Ans: Variables belonging to different scope can have same name as in the following
code snippet.
int var;

void f() {
int var;
}

main() {
int var;
}

66
Arrays

67
Working with String

68
Pointers

69
What is dangling Pointer?
Dangling pointers in computer programming are
pointers that pointing to a memory location that
has been deleted (or freed).

Dangling pointers arise during object destruction,


when an object that has an incoming reference is
deleted or deallocated, without modifying the
value of the pointer, so that the pointer still points
to the memory location of the deallocated
memory.

The system may reallocate the previously freed


memory, unpredictable behavior may result as the
memory may now contain completely different
data.

70
Cause of dangling pointers

1. Return Local Variable in Function Call

2. Variable goes Out of Scope

3. De-allocating or free variable memory

71
Return Local Variable in Function Call

1. #include<stdio.h>
2. #include<string.h>

3. char *getHello()
4. {
5. char str[10];
6. strcpy(str,"Hello!");
7. return(str);
8. }

9. int main()
10. {
11. //str falls out of scope
12. //function call char *getHello() is now a dangling pointer
13. printf("%s", getHello());
14. }

72
Variable goes Out of Scope
1. #include<stdio.h>

2. int main()
3. {
4. char **strPtr;
5. {
6. char *str = "Hello!";
7. strPtr = &str;
8. }
9. // str falls out of scope
10. // strPtr is now a dangling pointer
11. printf("%s", *strPtr);
12. }

73
De-allocating or free variable memory
1. #include<stdio.h>
2. #include<stdlib.h>

3. int main()
4. {
5. char **strPtr;
6. char *str = "Hello!";
7.
8. strPtr = &str;
9. free(str);
10. //strPtr now becomes a dangling pointer
11.
12. printf("%s", *strPtr);
13. }

74
Avoiding dangling pointer errors
We can avoid the dangling pointer errors by initialize pointer to NULL, after de-
allocating memory, so that pointer will be no longer dangling. Assigning NULL
value means pointer is not pointing to any memory location.

1. char **strPtr;
2. char *str = "Hello!";
3.
4. strPtr = &str;

5. free (str); /* strPtr now becomes a dangling pointer */


6. ptr = NULL; /* strPtr is no more dangling pointer */

75
Void pointer
 Void pointer is a specific pointer type – void * – a pointer that points to some
data location in storage, which doesn’t have any specific type.
 Void refers to the type. Basically the type of data that it points to is can be any. If
we assign address of char data type to void pointer it will become char Pointer, if
int data type then int pointer and so on.
 Any pointer type is convertible to a void pointer hence it can point to any value.

Important Points

1. void pointers cannot be dereferenced. It can however be done using typecasting


the void pointer.
2. Pointer arithmetic is not possible on pointers of void due to lack of concrete
value and thus size.

76
Void pointer
1. #include<stdlib.h> 9. // (int*)ptr - does type casting of void

2. int main() 10. // *((int*)ptr) dereferences the typecasted

3. { 11. // void pointer variable.

12. printf("Integer variable is = %d", *( (int*) ptr) );


4. int x = 4;
13. // void pointer is now float
5. float y = 5.5;
14. ptr = &y;
6. //A void pointer
15. printf("\nFloat variable is= %f", *( (float*) ptr) );
7. void *ptr;
16. return 0;
8. ptr = &x;
17. }

77
NULL pointer
NULL Pointer is a pointer which is pointing to nothing. In case, if we don’t have
address to be assigned to a pointer, then we can simply use NULL.

#include <stdio.h>
int main()
{
// Null Pointer
int *ptr = NULL;

printf("The value of ptr is %u", ptr);


return 0;
}

78
NULL pointer (Important Points)
1. NULL vs Uninitialized pointer – An uninitialized pointer stores an undefined
value. A null pointer stores a defined value, but one that is defined by the
environment to not be a valid address for any member or object.

2. NULL vs Void Pointer – Null pointer is a value, while void pointer is a type

79
Wild pointer
A pointer which has not been initialized to anything (not even NULL) is known as wild
pointer. The pointer may be initialized to a non-NULL garbage value that may not be a
valid address.

int main()
{
int *p; /* wild pointer */
int x = 10;
// p is not a wild pointer now
p = &x;
return 0;
}

80
What are near, far and huge pointers?
These are some old concepts used in 16 bit intel architectures in the days of MS DOS,
not much useful anymore.
Near pointer is used to store 16 bit addresses means within current segment on a 16 bit
machine. The limitation is that we can only access 64kb of data at a time.

A far pointer is typically 32 bit that can access memory outside current segment. To
use this, compiler allocates a segment register to store segment address, then another
register to store offset within current segment.

Like far pointer, huge pointer is also typically 32 bit and can access outside segment. In
case of far pointers, a segment is fixed. In far pointer, the segment part cannot be
modified, but in Huge it can be

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

/* Function with memory leak */


#include <stdlib.h>
void f()
{
int* ptr = (int*)malloc(sizeof(int));
/* Do some work */
return; /* Return without freeing ptr*/
}

82
What are local static variables? What is their use?

Ans: A local static variable is a variable whose lifetime doesn’t end with a function
call where it is declared. It extends for the lifetime of complete program. All calls to
the function share the same copy of local static variables. Static variables can be
used to count the number of times a function is called. Also, static variables get the
default value as 0.

83
Example of Static Variable

#include <stdio.h>

void fun()

// static variables get the default value as 0.

static int x;

printf("%d ", x);

x = x + 1;

int main()

fun();

fun();

return 0;

// Output: 0 1

84
What are static functions? What is their use?

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

Hence, static functions are those functions which are callable in the same file where they
define.

We can define a function static by using following syntax

85
We can define a function static by using following syntax

static return_type function_name(arguments)

function_body;

Here is a function to find square root of a given number

static long int getSquare(int num){

return (num*num);

86
Program to demonstrate example of static function in
C language

#include <stdio.h>
//static function definition
static long int getSquare(int num){
return (num*num);
}
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",&num);
printf("Square of %d is %ld.\n",num,getSquare(num));
return 0;
}

87
Why the static functions are required?

Ans: Since functions are used to reuse the code (i.e. the code that you have written
can access in another file by putting them in the functions), but when you want to
define some functions that should not be sharable (callable) in another file.

static functions are also helpful to handle declaration conflict issue - if there are two
functions in two different files with same name, function declaration will be
conflict. We can make them static.

To fulfill such kind of requirement, we can make them static.

88
Command Line Argument

89
What are command line arguments?

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

90
What are command line arguments?

The most important function of C/C++ is main() function. It is mostly defined with a
return type of int and without parameters :

int main() { /* ... */ }

We can also give command-line arguments in C and C++. Command-line arguments are
given after the name of the program in command-line shell of Operating Systems.
 To pass command line arguments, we typically define main() with two arguments : first
argument is the number of command line arguments and

 second is list of command-line arguments.

int main(int argc, char *argv[]) { /* ... */ }

OR

int main(int argc, char **argv) { /* ... */ }

91
What are command line arguments?

argc (ARGument Count) is int and stores number of command-line arguments


passed by the user including the name of the program. So if we pass a value to a
program, value of argc would be 2 (one for argument and one for program name)

The value of argc should be non negative.

argv(ARGument Vector) is array of character pointers listing all the arguments.

If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will
contain pointers to strings.

Argv[0] is the name of the program , After that till argv[argc-1] every element is
command -line arguments.

92
Properties of Command Line Arguments:

1) They are passed to main() function.

2) They are parameters/arguments supplied to the program when it is invoked.

3) They are used to control program from outside instead of hard coding those
values inside the code.

4) argv[argc] is a NULL pointer.

5) argv[0] holds the name of the program.

6) argv[1] points to the first command line argument and argv[n] points last
argument.

93
Structure and Union

94
What is a nested structure?

Ans: A structure containing an element of another structure as its member is


referred so.

What is a self-referential structure?

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

95
Important Interview Questions

96
Important Interview Questions
1. Difference between scanf() and gets() in C.

97
Important Interview Questions
scanf()

 It is used to read the input(character, string, numeric data) from the standard
input(keyboard).

 It is used to read the input until it encounters a whitespace, newline or End Of


File(EOF).

gets()

 It is used to read input from the standard input(keyboard).

 It is used to read the input until it encounters newline or End Of File(EOF).

98
scanf() example
// C program to see how scanf()

// stops reading input after whitespaces

#include <stdio.h>

int main()

char str[20];

printf("enter something\n");

scanf("%s", str);

printf("you entered: %s\n", str);

return 0;

99
gets() example
// C program to show how gets()

// takes whitespace as a string.

#include <stdio.h>

int main()

char str[20];

printf("enter something\n");

gets(str);

printf("you entered : %s\n", str);

return 0;

10
0
Difference between scanf() and gets()
The main difference between them is:

1. scanf() reads input until it encounters whitespace, newline or End Of File(EOF)


whereas gets() reads input until it encounters newline or End Of File(EOF),
gets() does not stop reading input when it encounters whitespace instead it
takes whitespace as a string.

2. scanf can read multiple values of different data types whereas gets() will only
get character string data.

10
1
Important Program

10
2
Sum of array Elements without using loops
and recursion
// C++ program to find the sum of // return the sum
// N elements with goto statement return sum;
#include <iostream> }
using namespace std; // Driver Code
// Function to perform desired operation int main()
int operate(int array[], int N) {
{ // Get N
int sum = 0, index = 0; int N = 5, sum = 0;
label: // Input values of an array
sum += array[index++]; int array[] = { 1, 2, 3, 4, 5 };
// Find the sum
if (index < N) { sum = operate(array, N);
// backward jump of goto statement // Print the sum
goto label; cout << sum;
} }

10
3
C program to check if a given string is
Keyword or not
// C++ program to find the sum of // return the sum
// N elements with goto statement return sum;
#include <iostream> }
using namespace std; // Driver Code
// Function to perform desired operation int main()
int operate(int array[], int N) {
{ // Get N
int sum = 0, index = 0; int N = 5, sum = 0;
label: // Input values of an array
sum += array[index++]; int array[] = { 1, 2, 3, 4, 5 };
// Find the sum
if (index < N) { sum = operate(array, N);
// backward jump of goto statement // Print the sum
goto label; cout << sum;
} }

10
4
program to count the number of lines,
spaces and tabs
// C++ program to find the sum of // return the sum
// N elements with goto statement return sum;
#include <iostream> }
using namespace std; // Driver Code
// Function to perform desired operation int main()
int operate(int array[], int N) {
{ // Get N
int sum = 0, index = 0; int N = 5, sum = 0;
label: // Input values of an array
sum += array[index++]; int array[] = { 1, 2, 3, 4, 5 };
// Find the sum
if (index < N) { sum = operate(array, N);
// backward jump of goto statement // Print the sum
goto label; cout << sum;
} }

10
5
Practice Question

10
6
Multiple Choice Questions Practice

1. #include <stdio.h>
main()
{
char *p = 0;
*p = 'a';
printf("value in pointer p is %c\n", *p);
}
a) It will print a
b) It will print 0
c) Compile time error
d) Run time error

10
7
Multiple Choice Questions Practice

2. #include <stdio.h>
main()
{
if (sizeof(int) > -1)
printf("True");
else
printf("False");
}

a) True
b) False

10
8
Multiple Choice Questions Practice

3. #include <stdio.h>
main()
{
char *p = "Sanfoundry C-Test";
p[0] = 'a';
p[1] = 'b';
printf("%s", p);
}
a) abnfoundry C-Test
b) Sanfoundry C-Test
c) Compile time error
d) Run time error

10
9
Multiple Choice Questions Practice

4. #include <stdio.h>
int main()
{
float f = 0.1;
if (f == 0.1)
printf("True");
else
printf("False");
}
a) True
b) False
c) No Output will be printed
d) Run Time Error

11
0
Multiple Choice Questions Practice

5. #include <stdio.h>
main()
{
int n = 0, m = 0;
if (n > 0)
if (m > 0)
printf("True");
else
printf("False");
}
a) True
b) False
c) No Output will be printed
d) Run Time Error

11
1
Multiple Choice Questions Practice

6. Which of the following is not a valid variable name declaration?


a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned

7. All keywords in C are in ____________


a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned

11
2
Multiple Choice Questions Practice

8. Variable name resolving (number of significant characters for the uniqueness of


variable) depends on ___________
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None of the mentioned

9. Which of the following is true for variable names in C?


a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length

11
3
Multiple Choice Questions Practice

10 . Which is valid C expression?


a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;

11. What is the output of this C code?


#include <stdio.h>
int main()
{
printf("Hello World! %d \n", x);
return 0;
}

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!

11
4
Multiple Choice Questions Practice

12. What is the output of this C code?


#include <stdio.h>
int main()
{
int y = 10000;
int y = 34;
printf("Hello World! %d\n", y);
return 0;
}
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value

11
5
Multiple Choice Questions Practice

13. What is the output of this C code?


#include <stdio.h>
int main()
{
int ThisIsVariableName = 12;
int ThisIsVariablename = 14;
printf("%d", ThisIsVariablename);
return 0;
}

a) The program will print 12


b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration

11
6
Multiple Choice Questions Practice

13. Which of the following cannot be a variable name in C?


a) volatile
b) true
c) friend
d) export

11
7
Thank You

11
8

You might also like