PROGRAMMING IN C Part1
PROGRAMMING IN C Part1
5. Write the difference between variable and constant in C with example of each
Variable: A variable is a named storage location in memory whose value can change
during program execution. Variables are declared using a data type (e.g., int, float,
char) followed by an identifier (name). They can hold different values at different
times during program execution. The value of a variable can be modified using
assignment statements. Example
int age; // Declares a variable named 'age' of type int
age = 25; // Assigns the value 25 to the variable 'age'
Constant: A constant is a value that remains fixed and cannot be altered during
program execution. Constants are typically used to represent fixed values such as
mathematical constants, configuration parameters, or other values that should not
change.
Relational Expressions: These expressions involve relational operators (==, !=, <, >,
<=, >=) and evaluate to a Boolean value (true or false) based on the relationship
between operands. Example: a>b
Logical expressions: These expressions involve logical operators (&&, ||, !) and
perform logical operations on Boolean values. Example: (a>b) && (a>c)
Conditional expressions: These expressions involve the conditional operator (?:) and
provide a compact way to make decisions based on conditions. Example: (a>b) ? a : b
Bitwise expressions: These expressions involve bitwise operators (&, |, ^, ~, <<, >>)
and perform bitwise operations on integer operands. Example: a & b
11. What is the difference between Unary operator, Binary operator, Ternary operator?
The difference is shown in the diagram given below:
Left associativity: If operators are left-associative, they are evaluated from left to
right when they appear in an expression without parentheses
Right associativity: If operators are right-associative, they are evaluated from right
to left when they appear in an expression without parentheses.
For example, consider the expression a + b + c. Since the + operator is left-
associative, it will be evaluated as (a + b) + c, meaning that a and b will be added
first, and then the result will be added to c. If + were right-associative, the evaluation
would be a + (b + c), with b and c being added first before adding a.
15. Write the syntax and use of scanf( ) and printf( ) functions in C.
scanf( ): Reads input from the standard input stream (usually the keyboard) based on
the specified format and stores the results into variables. Syntax is given below:
int scanf(const char *format, ...);
format: A string that specifies the format of the input expected by scanf().
It may contain format specifiers, which are placeholders for the types of data to be
read.
These format specifiers start with the percent sign (%) followed by a conversion
character (e.g., %d for integers, %f for floats, %c for characters).
...: The ellipsis (...) indicates that scanf() can take a variable number of arguments.
These are pointers to variables where the input values will be stored
Example:
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2); // Reads two integers from the user
printf( ): Prints formatted output to the standard output stream (usually the console)
based on the specified format string and optional arguments. Syntax is given below:
int printf(const char *format, ...);
format: A string that specifies the format of the output to be printed by printf(). It
may contain format specifiers, which are placeholders for the types of data to be
printed. These format specifiers start with the percent sign (%) followed by a
conversion character (e.g., %d for integers, %f for floats, %c for characters).
...: The ellipsis (...) indicates that printf() can take a variable number of arguments.
These are the values to be printed corresponding to the format specifiers in the
format string.
Example:
#include <stdio.h>
int main() {
int num = 10;
float pi = 3.14159;
char ch = 'A';
printf("Integer: %d\n", num); // Prints an integer
printf("Float: %f\n", pi); // Prints a float
printf("Character: %c\n", ch); // Prints a character
return 0;
}
16. What is meant by built in function in C? Write some categories of built in functions as
example.
They are also called library functions. 8There are several built-in functions provided
by the standard C library (stdlib.h, stdio.h, math.h, etc.) that perform common tasks,
such as input/output operations, memory management, string manipulation,
mathematical computations, and more. Here are some commonly used built-in
functions in C:
Input Output functions : printf( ), scanf( ), getchar( ), putchar( )
Memory management functions : malloc( ), calloc( ), free( )
String manipulation functions : strlen( ), strcpy( ), strcmp( )
Mathematical functions: sqrt( ), pow( ), sin( ), Cos( )
Chatracter handling functions: isalpha( ), isdigit( ), isupper( ), islower( )
etc.
17. What is the use of comments in a C Program? How comments are represented in a C
program?
Comments in C programming are used to provide explanatory notes within the
source code. They are ignored by the compiler and serve the purpose of improving
code readability and understanding.
Example:
// This is a single-line comment
int x = 10; // This is also a single-line comment