C Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14
At a glance
Powered by AI
The key takeaways from the passage are that C is a procedural programming language that provides basic data types, control flow statements like loops and conditionals, and standard library functions. It is commonly used to develop system software like operating systems and is close to machine language.

The basic data types in C are integer (int), floating point (float), character (char), and void. The size of integers and floats may depend on the system. Strings are arrays of characters.

The looping constructs in C are while loop, do-while loop, and for loop. While and for loops check the condition at the start, do-while checks at the end. Loops are used to repeat a block of code multiple times.

A Short Guide to Preliminary Exam 1 C Programming

C Programming

Important Topics for C programming M.C.Q: Data type, variable, iteration, size of data
type, logic, control statement, break,continue, output, string output and previous questions.

1. Which one is not a header file? [BREB -AJE-2019]


a) Conio.h b) iostream.h c)math.h d)instead.h Ans.:d
Explanation:
 Header file is a file that contains function declaration and macro definition for C in-built
library functions.
 All C standard library functions are declared in many header files which are saved as
file_name.h.
 We are including these header files in our C program using “#include <file_name.h>”
command to make use of the functions those are declared in the header files.
 Example:stdio.h,conio.h,math.h, assert.h, ctype.hetc
2. What is the output of following program: [ICML (AP)-2019]
int main ()
{
printf ("%d\t", sizeof (6.5));
printf ("%d\t", sizeof (90000));
printf ("%d\t", sizeof ("A"));
printf ("%d\t", sizeof('x'));
return 0;
}

Ans.: 8 4 2 4
Solution:
Hereprintf ("%d\t", sizeof (6.5));is a double value. So, size of double value is 8
bytes. Next line print sizeof integer which size is 4 byte and the third line character constant is 2
bytes (size of char data type is one byte). The fourth line is treated as integer which size is 4
bytes.

Type Storage size Value range


char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767 or -2,147,483,648 to
int 2 or 4 bytes
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
A Short Guide to Preliminary Exam 2 C Programming
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
-9223372036854775808 to
long 8 bytes
9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615

3. What is the output of the following program?


int main()
{
printf("%d %d%d",sizeof(3.14f),sizeof(3.14),sizeof(3.1414));
return 0;
}
Ans.: 4 8 8 (sizeof(3.14f) is consider as float then next two are consider as double)

4. What is the size of an int data type?


a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
Ans.: c
Explanation: The size of the data types depends on the system.
5. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short b) unsigned short c) long d) int
Ans.:b
Explanation: 65000 comes in the range of short (16-bit) which occupies the least memory. Signed
short ranges from -32768 to 32767 and hence we should use unsigned short.
6. Which Format Specifier is used for typing double data? [BREB 2019]
a) %f b)%1f c)%d d)%s Ans.: b
Explanation:

char
%c Character
unsigned char
short
unsigned short
%d Signed Integer
int
long
float
%e or %E Scientific notation of float values
double
%f Floating point float
%lf Floating point double
A Short Guide to Preliminary Exam 3 C Programming
%p Address of pointer to void void * void *
unsigned int
%u Unsigned Integer
unsigned long
7. Which one is the not basic data type of c programming?[SBL (AP)-2016]
a) Char b) int c) void d) None of this
Ans.: c
8. Suppose a C program has floating constant 1.414, what's the best way to convert it as
afloat data type?[Combined (Officer- IT/ICT)-2019]
a) (float)1.414 b) float(1.414) c) 1.414f or 1.414F d) None of these Ans. c
Explanation: By default, floating constant is of double data type. By suffixing it with f or
F,can be converted to float data type.
9.What is the output of the following program?[SBL (AP)-2016]
int main ()
{
printf("%.3f", 8/((3*8)*3));
return 0;
}
a) 0.000 b) 1.000 c) 0.111 d) None
Ans.: a
Explanation:
The call of printf has undefined behavior because the conversion specifier %f does not
correspond to the type of the expression of int.
10.Consider the following variable declarations and definitions in C. [Combined (Ofc- IT/ICT)-2019]
i. int var_9=1
ii. int 9_var=2
iii. int _=3
Choose the correct statement above variables.
a) Both i) and ii) are valid b) Only i) is valid
c. Both i) and iii) are valid d) All of these
Ans.: c
Explanation:
 Every variable name should start with alphabets or underscore (_).
 No spaces are allowed in variable declaration.
 Except underscore (_) no other special symbol are allowed in the middle of the variable
declaration (not allowed -> roll-no, allowed ->roll_no).
 Maximum length of variable is 8 characters depend on compiler and operation system.
 Every variable name always should exist in the left-hand side of assignment operator
(invalid -> 10=a; valid -> a=10;).
 No keyword should access variable name (int for invalid because for is a keyword).

Some Example of valid and invalid identifier


A Short Guide to Preliminary Exam 4 C Programming

Valid identifiers Invalididentifiers


RollNo 2name
Roll_No Roll No
_Roll_No int void
rollno
Year
11. Which of the declaration is correct?[ICB(AP)-2017] [Combined (Officer- IT/ICT)-2019]
a) int length; b)char int; c) int long; d) float double;
Ans.: a
12. Which of the following is not a valid variable name declaration?
a) int _a3; b) int a_3; c) int 3_a; d) int _3a
Ans.: c
Explanation: Variable name cannot start with a digit. Must start with an underscore or alphabets.
13. Which one is the invalid variable name in c programming? [ANE -BPSC -2019]
a) Average b) No#ofStrdentc) Xzy d) y23Z
Ans.: b
14. 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
Ans.: c
Explanation: According to the syntax for C variable name, it cannot start with a digit.
15.Which of the following doesn’t require an & for the input in scanf()?
a) char name[10]; b) int name[10]; c) float name[10]; d) double name[10];
Ans.: a
Explanation:gets() function for input.

Note: scanf(“%s”,str); does not read string type which contain white space. Hence to read multi
word string use gets(str).
16. Which of following comment regarding the reading of a string using scanf() and gets () is
true?
a) scanf is delimited by blank, gets is delimited by end of line
b) scanf is delimited by end of line, gets is delimited byblank space
c) Both can be used interchangeably
d) None of these
Ans.: a
17.int C=10; printf(“%d”,c--); given output off----[Combined(AP)-2019]
a) 10 b)11 c) 9 d)8
Ans.: a
A Short Guide to Preliminary Exam 5 C Programming
Explanation:

Operator Operator/Description
Pre increment operator (++i) value of i is incremented before assigning it to the variable i
Post increment operator (i++) value of i is incremented after assigning it to the variable i
Pre decrement operator (-- i) value of i is decremented before assigning it to the variable i
Post decrement operator (i--) value of i is decremented after assigning it to variable i

18. Which of the following will not increase the value of variable c by 1?[BREB(IT)-2016]
a) C++ b) C = C+1 c) C+1 >= C d) C+= 1
Ans.: c
19. What is the output of following program?
int main ()
{
int c=10;
printf("%d\t",c); // print 10
printf("%d\t",c++);// increment but not execute this line so
print 10
printf("%d\t",++c);// ++ imediate increment also execute
previous step so print 12
printf("%d\t",c--);// decrement but execute in next line so
print 12
printf("%d\t",--c);// imediate decrement with previous line so
print 10
return 0;
}
Ans.: 10 10 12 12 10
20. What is the output of the following program?[JBL(SO-IT/ICT)-2016]
int main ()
{
int i = 0;
int x = i++, y = ++i;
printf ("%d %d", x, y);
return 0;
}
a) 0,2 b) 0,1 c) 1,2 d) undefined
Ans.: a

21.The value 9.87 to 10 when use?[BB(AP)-2016]


a) floor () b) ceil() c) both d) None
Ans.: b
Explanation:
A Short Guide to Preliminary Exam 6 C Programming
Ceil :the ceil function is the function that takes as input a real numberx and gives as output the
greatest integergreater than or equal to x. Like ceil(3.7)=4 , ceil(4.2)=5.
Floor:the floor function is the function that takes as input a real numberx and gives as output the
greatest integer less than or equal to x. Like floor(3.7)=3 , floor(4.2)=2.
22.What's wrong with the statement? (x =4 && y = 5) ? (a=5); (b=6);[BB AP-2012]
a) The question mark should be an equal sign
b) There are too many variables in the statement
c) The conditional operator is only used with a string
d) The first semicolon should be a colon
Ans. d
23.Which is logical operator?
a) + b)> c) && d)<<
Ans. c
24.Which is logical operator?[JBL (AME)-2011]
a) + b)>= c) AND d)<<
Ans. b
25. Which of the following is not logical operator?
a) & b) && c) || d) !
Ans.: a
26. What is the output of the following program?
int main()
{
int f=11,i=3;
i+=(f>3)? i&2:5;
printf("%d ",i);
return 0;
}
Ans.: 5
Explanation:
i+=(f>3)? i&2:5
i+= (11>3)? 3&2 :5 [so statement(f>3) is true andi&2 executed]
i+=3&2 [0011 & 0010 =0010 =2]
i+=2
5
CONDITIONAL OR TERNARY OPERATORS IN C:
Syntax: (Condition? true_value: false_value);
Example: (A >100? 0 : 1);
int x=1, y ;
y = (x ==1 ? 2 : 0 ) ;
output: 2
Example:
int a=100,b=200,c=300,c;
x= (a>b)?((a>c)?a:c):((b>c)?b:c);
1st part 2nd part
3rd part
A Short Guide to Preliminary Exam 7 C Programming
Explanation:
Here a> b is false so 3rd part is working
In 3rd part, b>c also false. so C is working

Output: x=300

27.What is the final values of a and c in the following C statement? [CombinedAME-2019]


(initialize value a=2,c=1) c=c? a=0:2;
a) a=0,c=0 b) a=2,c=2 c) a=2,c=2 d) a=1,c=2 Ans. a
Explanation:
Steps: 2? condition is true
a=0;
c=a;
c=0;
28. Which of the following statement is used to exit loop immediately[ANE -BPSC -2019]
a) break b) if c) switch d) continue
Ans.: a
29. The keyword ‘break’ cannot be simply used within _________
a) do-while b) if-else c) for d) while
Ans: b
Explanation:“break” and “continue” can be used in “for”, “while” and “do-while” loop body. But
only “break” can be used in “switch” body.
30. Which one is the following can be replace instead of ‘if’ [ANE -BPSC -2019]
a)Return b) structure c) for d) switch
Explanation:
Control statement:
 if-else
 switch – case: Work only for integer and char value
 for
 while(): Infinite loop when while(1)
 do while() : work 1 st time without checking condition.

31. What is an example of iteration in C?[PKB(AP)-2018]


a) for b) while c) do-while d) all of the above
Ans.: d
32. Which control statement can be executed at least once?[ICB(AP)-2018]
a) while b) for c) dowhile d) all of the above
Ans.: c
Explanation:
In while loopthe given condition is checked at the start of the loop.If the condition is false
then the loop is not executed at all.Only when the condition is true the loop block is executed.
In do while loop first the loop block is executed then condition is checked at the end of 1st time
execution. Ex.
int main()
A Short Guide to Preliminary Exam 8 C Programming
{
int i = 2;
do
{
printf ("Test %d", i);
}
while (i> 3);// condition false but print 2 for the first time
return 0;
}
Ans.: Test 2
33. Which of the following cannot be checked in a switch-case statement?
a) character b)integer c) float d) none of the above
Ans.: c
34. Which datatype can accept the switch statement?
a) int b) char c) long d) all the mentioned
Answer: d
35. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________
a) break b) exit(0) c) abort() d) terminate
Answer: a
36.int number[]={10,20,30,40,50}; number[3]=?[SBL/JBL.(SO-IT/ICT)-2018]
a) 10 b) 20 c) 30 d) 40
Ans.: d
Explanation:array starts at position 0.
37.An array contains the following letters, Color = {E,L,E,C,T,I,O,N) The value of the
variable, E=3. Color [E] points to which value?
a) E b) C c) T d) 1
Ans.: b
38. What is the output for the following program?
int main(void){
char name[] = "Harry Potter";
printf("%s", name); // Output: Harry Potter
printf("%s", name+1); // Output: arry Potter
printf("%c", *name); // Output: H
printf("%c", *(name+7)); // Output: o
char *namePtr;
namePtr = name;
printf("%c", *namePtr); // Output: H
printf("%c", *(namePtr+1)); // Output: a
printf("%c", *(namePtr+7)); // Output: o
}

39. What is the output for the following program?

int main()
{
A Short Guide to Preliminary Exam 9 C Programming
char *str = "abcde";
printf ("%c", *str);
printf ("%c", *str++);
printf ("%c", *(str++));
printf ("%s", str);
return 0;
}
Ans.: aabcde
40. What is the output of the following program?

int main()
{
char str[]="Heal";
printf("%c",str[4]);
return 0;
}
a) H b) l c) d d) None
Ans.: d
41. Which function will you choose to join two words?
a) strcpy() b) strcat() c) strncon() d) memcon()
Ans.: b
Explanation: The strcat() function is used for concatenating two strings, appends a copy of the
string.
char *strcat(char *s1,const char *s2);
List of String function:
 strcat- concatenate two strings
 strchr- string scanning operation
 strcmp - compare two strings
 strcpy - copy a string
 strlen- get string length
 strncat - concatenate one string with part of another
 strncmp - compare parts of two strings
 strncpy - copy part of a string
 strrchr - string scanning operation
42. Which of the following function returns a pointer to the located string or a null pointer
if string is not found.
a) strtok() b) strstr() c) strspn() d) strrchr()
Ans.: b
Explanation: The strstr() function is used to return a pointer to the located string, or if string is not
found a null pointer is returned.
43. The______ function returns the number of characters that are present before the
terminating null character.
a) strlength() b) strlen() c) strlent() d) strchr()
Ans.:b
A Short Guide to Preliminary Exam 10 C Programming
Explanation: The strlen() function is used to return the number of characters that are present
before the terminating null character.size-t strlen(const char *s);The length of the string pointed
to by s is computed by strlen().
44.To store address in c programming which one is used? [ANE -BPSC -2019]
a) Break b) pointer c) char d) float
Ans.: b

45. Which of the following is not possible in C?


a) Array of function pointer b) Returning a function pointer
c) Comparison of function pointer d) None of the mentioned
Answer: d
46. What is the output of the following program: [Competition Commision(P)-2019]

int main()
{
char *a[2] = { "hello", "hi" };
printf ("%s", *(a + 1));
return 0;
}

Output: hi

47. For which of the following, “PI++;” code will fail?


a) #define PI 3.14 b) char *PI = “A”;
c) float PI = 3.14; d) none of the Mentioned
Ans.: a
48. Which of the following declaration is not supported by C?
a) String str; b) char *str;
c) float str = 3e2; d) Both String str; & float str = 3e2;
Ans.: a
49. Which among the following are the fundamental arithmetic operators, i.e, performing
the desired operation can be done using that operator only?
a) +, – b) +, -, %
c) +, -, *, / d) +, -, *, /, %
Ans.: a
50. When double is converted to float, then the value is?
a) Truncated b) Rounded
c) Depends on the compiler d) Depends on the standard
Ans.: c

KEY POINTS TO REMEMBER ABOUT POINTERS IN C:


 Normal variable stores the value whereas pointer variable stores the address of the
variable.
 The content of the C pointer always be a whole number i.e. address.
A Short Guide to Preliminary Exam 11 C Programming
 Always C pointer is initialized to null, i.e. int *p = null.
 The value of null pointer is 0.
 & symbol is used to get the address of the variable.
 * symbol is used to get the value of the variable that the pointer is pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
 Two pointers can be subtracted to know how many elements are available between these
two pointers.
 But, Pointer addition, multiplication, division are not allowed.
 The size of any pointer is 2 bytes (for 16-bit compiler).
C Dynamic Memory Allocation Functions

Function Purpose
Allocates the memory of requested size and returns the
malloc
pointer to the first byte of allocated space.
Allocates the space for elements of an array. Initializes the
calloc
elements to zero and returns a pointer to the memory.
It is used to modify the size of previously allocated memory
realloc
space.
Free Frees or empties the previously allocated memory space.

Some Practices Question for M.C.Q:


 What is the difference between ++a and a++?
“++a” is called prefixed increment and the increment will happen first on a variable. ‘a++' is
called postfix increment and the increment happens after the value of a variable used for the
operations.
 Describe the difference between “=” and“==” symbols in C programming?
‘==' is the comparison operator which is used to compare the value or expression on the left-hand
side with the value or expression on the right-hand side.
‘=' is the assignment operator which is used to assign the value of the right-hand side to the
variable on the left-hand side.
 How many loops are in C programming?
There are 4 types of loop statements in C.
 While loop
 For Loop
 Do…While Loop
 Nested Loop
 What is the incorrect operator form following list(== , <> , >= , <=) and what is the
reason for the answer?
Incorrect operator is ‘<>'.This is the format correct when writing conditional statements, but it is
not a correct operation to indicate not equal in C programming and it gives compilation error as
follows.
 What are the modifiers available in C programming language?
A Short Guide to Preliminary Exam 12 C Programming
There are 5 modifiers available in C programming language as follows.
 Short
 Long
 Signed
 Unsigned
 long long
 What is preprocessor?
Preprocessor is a directive to the compiler to perform certain things before the actual compilation
process begins.
 What is the difference between Call by Value and Call by Reference?
When using Call by Value, you are sending the value of a variable as parameter to a function,
whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value
in the parameter is not affected by whatever operation that takes place, while in the case of Call
by Reference, values can be affected by the process within the function.
 Is it possible to run program without main() function?
No
 How many main() function we can have in our project?
1
 Is it true that a function may have several declarations, but only one definition.
True

 Difference between calloc() and malloc()


malloc() takes a single argument while calloc() needs two arguments
 Total number of keywords in C are
32
 rand() function returns
integer type
 What is the purpose of getc()
read a character from a file
 Is (*p) and **p is same?
No. *p point a single variable where **p is a pointer to a pointer variable, also called double
pointer.
 Difference between structure and union is
The way memory is allocated
 printf() belongs to which library of c
stdio.h
 Difference of getchar(), getc(),getche(), getch().
 getchar() - it reads from standard input and it is equivalent to getc(stdin). SYNTAX - int
getchar(void);
 getc() - it reads a single character from a given input stream and returns an integer value
on success. It returns EOF on failure. SYNTAX - int getc(FILE *stream);
 getche() - like getch() , this is also a non-standard function present in conio.h. It reads a
single character from keyboard and displays immediately on output screen without
waiting for enter key. SYNTAX - int getche(void);
A Short Guide to Preliminary Exam 13 C Programming
 getch() - it is a nonstandard function and id present in conio.h header file which is mostly
used by MS-DOS compilers like turbo C. It is not a part of the C standard library.
SYNTAX - int getch();
 What is right way to Initialize array?
int num[6] = { 2, 4, 12, 5, 45, 5 };
 What is the output of the following program?
int main()
{
int a[5] = { 5, 1, 15, 20, 25 };
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf ("%d, %d, %d", i, j, m);
return 0;
}
Ans.: 3, 2, 1
C programming previous questions:

1. Suppose a C program has floating constant 1.414, what's the best way to convert it as a
float data type? [Combined(O-IT/ICT)-2019]
a) (float)1.414 b) float(1.414) c) 1.414f or 1.414F d) None of these Ans.: c
2. Which of the declaration is correct?[Combined(O-IT/ICT)-2019]
a) int length; b) char int c)int long; d) float double; Ans.: a
3.An n*n armay v is defined as follows: v[i, j]=i-j for all i,j; 1<=i<=n, 1<=j<=n , the sum
of the element of array v is [Combined(O-IT/ICT)-2019]
a) 0 b) n-1 c) n -3n+2
2
d) n (n+1)/2
2
Ans.: a
4. An array contains the following letters, Color = {E, L,E,C,T,I,O,N) The value of the
variable, E=3. Color [E] points to which value? [Combined(SO-IT/ICT)-2018]
a) E b) C c) T d) 1 Ans.: b
5. int C=10; System.out.println(C--); gives a output of ---- [Combined(SO-IT/ICT)-2018]
a)10 b) 11 c) 9 d) 8 Ans.: a
6.What is an example of iteration in C? [Combined(AP)-2018]
a) for b) while c) do-while d) all of the above Ans.: d
7.Which of the following doesn’t require an & for the input in scanf()? [Combined(AP)-2018]
a) char name[10]; b) int name[10]; c) float name[10]; d) double name[10]; Ans.: a
8. int number[]={10,20,30,40,50}; number[3]=? [SBL,JBL (SO-IT/ICT)-2018]
a) 10 b) 20 c) 30 d) 40 Ans.: d
9.What is the final values of a and c in the following C statement?
(initialize value a=2,c=1) c=c? a=0:2;[Combined(AP)2018]
a) a=0,c=0 b) a=2,c=2 c) a=2,c=2 d) a=1,c=2 Ans.: a
10.What is the length of character pointer in below code? [ICB(AP)2017]
char *str="Hello"; printf("%d",strlen(str));
A Short Guide to Preliminary Exam 14 C Programming
a) 3b)5 c)2 d)undefined Ans.: b
11.Which control statement can be executed at least once?[ICB(AP)2017]
a) While b)for c) do—while d) All of the above Ans.: c
12. Which of the following cannot be checked in a switch-case statement?[ICB(AP)2017]
a) Character b)Integer c) Float d) None of the above Ans.: c
13. Which of the following will not increase the value of variable c by 1?[BREB 2016]
a) C++ b) C = C+1 c) C+1 >= C d) C+= 1 Ans.: c
14. Which is logical operator?
a) +b)>= c) AND d)<< Ans.: c
15. Which Format Specifier is used for typing double datatype?
a)%f b)%1f c)%d d)%s Ans.: b

Summary
 'C' was developed by Dennis Ritchie in 1972.
 It is a low programming level language close to machine language
 It is widely used in the software development field.
 It is a procedure and structure-oriented language.
 It has the full support of various operating systems and hardware platforms.
 Many compilers are available for executing programs written in 'C'.
 A compiler compiles the source file and generates an object file.
 A linker links all the object files together and creates one executable file.
 The main function is a mandatory part of every 'C' program.
 To use the functionality of a header file, we have to include the file at the beginning of
our program.
 There are total 32 keywords.
 A constant is a value that doesn't change throughout the execution of a program.
 There are four commonly used data types such as int, float, char and a void.
 We can also nest if-else within one another when multiple paths have to be tested.
 Looping is one of the key concepts on any programming language.
 It executes a block of statements number of times until the condition becomes false.
 Loops are of 2 types: entry-controlled and exit-controlled.
 'C' programming provides us 1) while 2) do-while and 3) for loop.
 A switch is used in a program where multiple decisions are involved.
 A switch must contain an executable test-expression.
 Each case must include a break keyword.
 A string is a sequence of characters stored in a character array.
 A character such as 'd' is not a string and it is indicated by single quotation marks.
 'C' provides standard library functions to manipulate strings in a program. String
manipulators are stored in <string.h> header file.
 Auto, extern, register, static are the four storage classes in 'C'.

You might also like