C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
31. What are symbolic constants? How are they defined?
Symbolic constants, conventionally written in uppercase, are the names which substitute a sequence of character that cannot be changed. The character may be a numeric constant, a
character constant, or a string constant.
|
General Form
|
:
|
#define NAME VALUE
|
|
Example
|
:
|
#define PI 3.1416
|
32.Define type casting and type promotion. Write the difference between implicit and explicit type casting with examples.
The mechanism of converting the type of an expression into another type is referred as type casting. The process by which values of lower type are converted to higher type is type promotion.
The differences between implicit and explicit type casting are listed as:
|
Implicit Type Casting
|
Explicit Type Casting
|
||
|
Implicit type casting happens whenever the
|
Explicit type casting is the use of direct and
|
||
|
compiler expects data of a particular type,
|
specific notation in the source code to request
|
||
|
but the data is given as a different type,
|
a conversion or to specify a member from an
|
||
|
leading to an automatic conversion by the
|
overloaded class.
|
||
|
compiler without an explicit indication by
|
|||
|
the programmer.
|
|||
|
Implicit casting is done automatically.
|
Explicit casting is done programmatically.
|
||
|
In Implicit conversion, no
|
data
|
loss takes
|
In explicit conversion, data loss may or may
|
|
place during the data conversion.
|
not be take place during data conversion.
|
||
|
Hence there is a risk of information loss.
|
|||
|
Implicit conversion does
|
not
|
require
|
Explicit conversion do require cast operator to
|
|
any special syntax.
|
perform conversion.
|
||
18
33. Explain the structure of C program.
The general structure of C program is given as:
|
Documentation Section
|
Information about program, author and other details
|
||
|
Link Section
|
Link functions from library to the program
|
||
|
Definition Section
|
Defines symbolic constants
|
||
|
Global Declaration Section
|
Variables, functions are declared
|
||
|
Main function Section
|
Entry point of the program
|
||
|
All the variables used in the execution part are declared
|
|||
|
Local Declaration part
|
|||
|
Statements for input, processing and output
|
|||
|
Executable part
|
|||
|
Subprogram Section
|
Definition of all the user defined functions
|
||
|
Functions can be called from any other function
|
|||
|
Function 1
|
|||
|
Functions, initially, must be declared before it is called
|
|||
|
Function 2
|
|||
|
………………
|
|||
|
Function n
|
|||
34. Write the purpose and syntax of formatted I/O functions.
Two functions, namely printf() and scanf(), are generally used formatted I/O function. Since it allows the formatting of inputs/outputs such as width, alignment, precision and so on, it is referred as formatted I/O functions.
•scanf() function
▪Purpose: reads values from the keyboard as given by the user
▪Syntax: scanf(“control string”, argument list);
1.Control String: contains one or more format specifier
2.Argument list: contains address of one or many variables corresponding to number of format specifier
▪Examples:
1.scanf(“%c”,&ch); %c as character specifier, & to represent address
2.scanf(“%c %d %f”, &c, &num, &fnum);
•printf() function
▪Purpose: outputs the values onto the screen
▪Syntax: printf(“control string”, argument list);
19
1.Control String: contains information and one or more format specifier
2.Argument list: contains one or many variables corresponding to number of format specifier
▪Examples:
1.printf(“Enter any number”); To display the information
2.printf(“Sum is %d”, sum); To display information along with value of sum
3.printf(“%d %d”, a, b); Displays two values without any other information
35.Define format specifier. List the format specifier for different data types used in C
Format specifier is a special combination of characters that defines what kind of values is being read or displayed using I/O functions. The generally used format specifiers are listed in the table below.
|
SN
|
Format Specifier
|
Description
|
|
1
|
%c
|
Single character
|
|
2
|
%d
|
Signed integer value
|
|
3
|
%f
|
Floating value without exponent
|
|
4
|
%s
|
Strings
|
|
5
|
%i
|
Signed integer value
|
|
6
|
%x
|
Hexadecimal form
|
|
7
|
%u
|
Unsigned integer value
|
|
8
|
%o
|
Octal value
|
|
9
|
%ld
|
Long integer value
|
|
10
|
%lf
|
Double value
|
|
11
|
%Lf
|
Long double value
|
36. Explain the meaning of & operator in scanf. What happens if we use & operator in printf function?
The & operator, also known as reference operator, is an address operator which gives the address in memory of the operand that uses it. In simple, it is used to represent the address in which the entered value from the user is stored.
If we want to display the value of a variable then using & operator along with variable will not display the correct value. But in case we want to display the address of a variable then we ought to use & operator and corresponding format specifier (%u) to display the address of a variable.
20
37.“The number and types of argument must be same to that of number and types of format
specification used”, clarify the statement with example
For example,
int num1 = 2, num2 = 5, sum;
printf(“%d + %d = %d”, num1, num2, sum); is a valid output statement. Here,
Number of format specifier = Number of arguments = 3
Type of format specifier = Type of arguments = integer
However, printf(“%d * %d = %d”, num1); is an invalid statement since the number of arguments do not match with the number of format specifier used. Also, printf(“%d %f”, num1, num2); is an invalid statement. Since the type of format specifier does not match with the type of variable used in arguments.
Hence, for any input or output statement using formatted I/O functions to be valid, the number and types of argument must be same to that of number and types of format specifier used.
38.Why is precision and field width required? What happens if the field width given is less than that of the length of the variable, explain in case of scanf() and printf()?
Precision, in any real number, is used to define how many numbers after decimal point to be used to represent a real number. While the field width, in real and integer numbers, is used to assign the number of spaces to display the given number.
If the width given is less than that of the length of the variable, then the following outcomes can be observed.
•scanf(“%3d”, &num);
if the given input is 14324 then, it takes only 143 and other numbers will be ignored.
•printf(“Num = %3d”, num);
if the value of num is 12492 then still it displays 12492 regardless of the width defined.
39. Explain about flags with few examples
Flags are used to format the results. It comes along with the format specifier. Commonly used formatting flags are listed as
|
SN
|
Flag
|
Description
|
|
1.
|
–
|
Used for left justification
|
|
2.
|
+
|
Used to display the signed of any number
|
|
21
|
8 2 . 4 4
8 2 . 4 4
|
3.
|
0
|
Causes leading zeros to appear, if space is available
|
|
4.
|
#
|
Used to represent in octal and hex form
|
printf(“%-8.2f”, fnum); where fnum = 82.4432 will result in If flag – is not used When flag – is used
40. What are unformatted I/O functions? Write the syntax of putchar(), getchar(), getch(), getche().
Unformatted I/O functions are those functions which do not have provisions to format the given values; defining width, precision, alignments and others formatting options are not available. For example, gets(), puts(), getchar() to name the few.
|
Sn
|
Function
|
Purpose
|
Syntax
|
Example
|
|
1.
|
getchar()
|
Read a single character
|
char_var = getchar();
|
char ch;
|
|
ch = getchar();
|
||||
|
2.
|
putchar()
|
Display a single
|
putchar(char_var);
|
char ch = ‘p’;
|
|
character
|
putchar(ch);
|
|||
|
3.
|
gets();
|
Read a string
|
gets(string_var);
|
char name[20];
|
|
gets(name);
|
||||
|
4.
|
puts();
|
Displays string
|
puts(string_var);
|
char name[] = “check”;
|
|
puts(name);
|
||||
|
5.
|
getch()
|
Reads a single
|
char_var = getch();
|
char ch;
|
|
character, but doesn’t
|
ch = getch();
|
|||
|
echo it on the screen
|
||||
|
6.
|
getche()
|
Reads a single
|
char_var = getche();
|
char ch;
|
|
character, echoes it on
|
ch = getche();
|
|||
|
the screen
|
||||
*char_var represents the character variable, while string_var represents string type variable
*Clarifying getchar(), getch() and getche():
•After the user enters the key, the entered character can be seen in the screen in case of getchar() and getche() but not in case of getch().
•After pressing any key, enter key must be pressed in case of getchar() but not in case of getch() and getche().
22
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
