assignment-1
assignment-1
ASSIGNMENT NO 1
Q1. Explain scanf () function with syntax and Format
Specifiers.
ANS: In C programming, the scanf() function is used to read formatted
input from the standard input device (stdin). The< code>scanf() function
is part of the standard library stdio.h and is used to receive data from
the user. The syntax of the function is as follows:
int scanf( const char *format, ... );
FORMAT SPECIFIERS IN C:
In the C programming language, the scanf function uses "format
specifiers" within a format string to define the expected data type of
user input it should read and store in variables, with each specifier
starting with a percent sign (%) followed by a character representing
the data type (e.g., "%d" for integers, "%f" for floats, "%c" for
characters, "%s" for strings).
Q2. Explain printf () function with syntax and Format
Specifiers.
ANS: In C language, printf() function is used to print formatted output
to the standard output stdout (which is generally the console screen).
The printf function is a part of the C standard library <stdio.h> and it
can allow formatting the output in numerous ways.The syntax of the
function is as follows:
printf ( "formatted_string", arguments_list);
FORMAT SPECIFIERS IN C:
In C programming, the "printf" function uses "format specifiers" which
are placeholders within a string that indicate where variable values
should be inserted, denoted by a percent sign (%) followed by a
character representing the data type (like %d for integers, %f for floats,
%s for strings) to print formatted output to the console; essentially
allowing you to dynamically include variable values within a printed
message.
Q3. How to take input and output of basic data types in C/C+
+.
ANS: To take input and output of basic data types in C, you can use
the scanf() and printf() functions. In C++, you can use
the cin and cout variables, or the stream insertion operator << and
extraction operator >>.
Explanation
C
Use the scanf() function to take input and
the printf() function to output.
These functions can read and format data such as integers,
characters, and real numbers.
The format specification string in these functions specifies
the data type, width, and size of the input or output.
C++
Use the cin variable for input and the cout variable for
output.
You can also use the stream insertion operator << for output
and the extraction operator >> for input.
These variables and operators are defined in
the <iostream> class.
Q4. Define Modifiers (Qualifiers) and discuss the types of
modifiers.
ANS: In C programming, "modifiers" (also called "qualifiers")
are keywords used to alter the default properties of a data type,
primarily affecting its size or whether it can hold positive values only
(signed vs. unsigned), allowing for more precise control over variable
declaration and memory allocation; the primary types of modifiers in C
are "short", "long", "unsigned", and "signed" when applied to
fundamental data types like "int" and "char".
Explanation of C modifiers:
Signed:
This is the default modifier for most data types, allowing variables to
store both positive and negative values.
Unsigned:
When used, a variable can only store non-negative values (0 and
positive integers).
Short:
This modifier creates a smaller integer data type, usually used when
memory usage is a concern and large integer values are not needed.
Long:
Used to create a larger integer data type, suitable for storing very large
numbers.