0% found this document useful (0 votes)
15 views8 pages

PROGRAMMING IN C Part1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

PROGRAMMING IN C Part1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAMMING IN C

1. Define identifier, keyword in C


Identifier: An identifier is a name given to a variable, function, array, or any other
user-defined item in a program. Example: sum, total, N, average_mark
Keyword: Keywords, also known as reserved words, are predefined words in the C
language that have special meaning and are reserved for specific purposes. They
cannot be used as identifiers for variables or other user-defined items.
Example of Keywords: int, float, if, else, for, while, return, break, switch, case, void, …

2. Write the rules for naming identifiers in C


-Identifiers can consist of letters (both uppercase and lowercase), digits, and
underscores (_)
-The first character of an identifier must be a letter or underscore
-Identifiers are case-sensitive. For example, "count" and "Count" are treated as
different identifiers
-Identifiers cannot be the same as C keywords

3. Write 2 examples of valid & invalid identifiers in C:


Valid Identifiers: sum, average_mark
Invalid Identifiers: 123abc (do not start identifier name with a digit))
sum of N (spaces not allowed in variable names)
if (keywords no allowed)

4. What are the basic data types in C?


The basic data types in C are: int, float, char, double

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.

6. How variables are declared in C? Write some examples


In C, variables are declared using the following syntax:
data_type variable_name;
where data_type specifies the type of data that the variable can store, such as int,
float, char, double, etc.
variable_name is the name given to the variable. It must follow the rules for naming
identifiers in C
Examples:
int age; // Declares a variable named 'age' of type int
float salary; // Declares a variable named 'salary' of type float
char initial; // Declares a variable named 'initial' of type char
double pi; // Declares a variable named 'pi' of type double
We can also declare multiple variables of the same type in a single declaration:
int num1, num2, result; // Declares three variables of type int: num1, num2, and
result
Also, we can combine variable declaration and initialization in a single statement:
int age = 25; // Declares and initializes a variable named 'age' with the value 25

7. What is an operator in C? What are the different types of operators in C?


An operator is a symbol or token that represents a specific operation or computation
to be performed on one or more operands/variables.
Operators in C can be categorized into several types based on their functionality:

8. What is the difference between Operator and Operand? Write an Example


Operator: An operator is a symbol or function that represents an operation to be
performed on one or more operands. Examples of operators: +, *, -, %, ++, -- etc
Operand: An operand is a value or variable on which an operator operates. Operands
can be literals, variables, constants, or expressions.
In the above figure Operator is + and Operands are a , b

9. What is meant by an expression in C? Write Example


An expression is a combination of constants, variables, operators, and function calls
that evaluates to a single value.

result = a + b * c is an example of expression. Here +, * are the operators and a, b,


c are the operands.

10. What are the different categories of expressions in C?


The various categories of expressions in C are:

Arithmetic expressions: These expressions involve arithmetic operators (+, -, *, /, %)


and compute numeric values. Example: a+b

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)

Assignment expressions: These expressions involve assignment operators (=, +=, -


=, *=, /=, %=) and assign values to variables. Example: n=10

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

Pointer expressions: These expressions involve pointer operations. Example: *ptr

11. What is the difference between Unary operator, Binary operator, Ternary operator?
The difference is shown in the diagram given below:

Unary Operator: A unary operator is an operator that operates on a single operand


Binary Operator: A binary operator is an operator that operates on two operands
Ternary Operator: A ternary operator takes three operands and is used for conditional
expressions. There is only one ternary operator in C.

Example: Unary Operator: ++, -- Unary Expression: a++


Binary Operator: + , - Binary Expression: a+b
Ternary Operator: ? : Ternary Expression: (a>b) ? a : b

12. What is meant by operator precedence in C?


Operator precedence in C determines the order in which operators are evaluated
when an expression contains multiple operators. Operators with higher precedence
are evaluated before operators with lower precedence. Precedence of arithmetic
operators are given below:
Precedence Of Arithmetic Operators

13. What is meant by associativity of operators in C?


Associativity refers to the order in which operators of the same precedence are
evaluated when they appear in an expression without parentheses
There are two types of associativity:

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.

14. What is meant by type casting in C?


Type casting refers to the process of converting a value from one data type to
another. This can be useful in situations where you need to perform operations or
assignments involving different data types
There are two main types of type conversion in C:

Implicit Type Conversion (Coercion): Implicit type conversion, also known as


coercion, occurs automatically by the compiler when values of different data types
are used together in expressions or assignments.
Example:
int num1 = 10;
float num2 = 5.5;
float result = num1 + num2; // Implicitly converts 'num1' to float before addition
Explicit Type Conversion (Type casting): Explicit type conversion involves manually
specifying the desired data type to which a value should be converted. It's achieved
by using the type cast operator, which is represented by enclosing the value or
expression to be cast within parentheses followed by the target data type.
Example:
int num1 = 10;
float num2 = 5.5;
int result = (int)(num1 + num2); // Explicitly casts the result to an integer
In the explicit type casting example above, (int) is used to explicitly cast the result of
num1 + num2 to an integer data type, even though the expression would result in a
float due to implicit type conversion. This ensures that the result is stored as an
integer.

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("You entered: %d and %d\n", num1, num2);


return 0;
}
In this example, %d is the format specifier for integers, and &num1 and &num2 are the
addresses of variables where the input values will be stored. The scanf() function
reads two integers from the user and assigns them to the variables num1 and num2.

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.

There are two ways of providing comments in a C program:


Single-line comments: Single-line comments start with // and continue until the
end of the line. Everything after // on the same line is considered a comment and is
ignored by the compiler.

Example:
// This is a single-line comment
int x = 10; // This is also a single-line comment

Multi-line comments: Multi-line comments, also known as block comments, start


with /* and end with */. Everything between /* and */ is considered a comment and
is ignored by the compiler, including newline characters. They can span across
multiple lines and can contain multiple lines of text
Example:
/* This is a multi-line comment
It can span across multiple lines
and is used for longer comments or explanations */
int y = 20; /* This is also a multi-line comment */

You might also like