Professional Documents
Culture Documents
2
Basic Concepts
• Developed in 1972 by Dennis Ritchie at Bell Telephone Laboratories (Now AT & T Bell
Laboratories).
3
Variable and Constant
4
Basic Concepts
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.
5
Basic Concepts (Cont..)
• Char and int can also be expressed in hexadecimal and octal notations.
6
Basic Concepts (Cont..)
7
Operations on Data
8
Basic Concepts (Cont..)
9
Basic Concepts (Cont..)
10
Multiple Choice Questions Practice
A. 32
B. 40
C. 24
D. 56
Answer : A
11
Multiple Choice Questions Practice
A. printf
B. external
C. auto
D. scanf
Answer : C
12
Basic Concepts (Cont..)
Storage class in C
13
Multiple Choice Questions Practice
A. static
B. extern
C. auto
D. register
Answer : C
14
Multiple Choice Questions Practice
A. 11
B. 10
C. Compilation Error
Answer : C
15
Multiple Choice Questions Practice
A. Yes
B. No
16
Multiple Choice Questions Practice
A. math.h
B. stdlib.h
C. conio.h
D. iomanip.h
Answer : A
17
Multiple Choice Questions Practice
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.
an integer.
20
Basic Concepts
21
Basic Concepts
22
printf() and scanf()
23
Basic Concepts
• 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()
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
• If-else
• Switch
31
Basic Concepts (Important Points)
• 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 )
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,
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”);
}
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”);
}
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.
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
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:
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:
60
Discussion over Interview
Question
61
Interview Question
62
Interview Question
63
Discussion over Interview
Question (Function)
64
Interview Question
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).
70
Cause of dangling pointers
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;
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
76
Void pointer
1. #include<stdlib.h> 9. // (int*)ptr - does type casting of void
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;
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.
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 int 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.
85
We can define a function static by using following syntax
function_body;
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.
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 :
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
OR
91
What are command line 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:
3) They are used to control program from outside instead of hard coding those
values inside the code.
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 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).
gets()
98
scanf() example
// C program to see how scanf()
#include <stdio.h>
int main()
char str[20];
printf("enter something\n");
scanf("%s", str);
return 0;
99
gets() example
// C program to show how gets()
#include <stdio.h>
int main()
char str[20];
printf("enter something\n");
gets(str);
return 0;
10
0
Difference between scanf() and gets()
The main difference between them is:
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
11
2
Multiple Choice Questions Practice
11
3
Multiple Choice Questions Practice
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
11
5
Multiple Choice Questions Practice
11
6
Multiple Choice Questions Practice
11
7
Thank You
11
8