0% found this document useful (0 votes)
28 views

C Programming Answers.

The document discusses different data types in C programming including integer, character, float, and double. It explains the range, size, and format specifier for each data type. It also covers operator precedence and associativity in C as well as differences between increment and decrement operators.
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)
28 views

C Programming Answers.

The document discusses different data types in C programming including integer, character, float, and double. It explains the range, size, and format specifier for each data type. It also covers operator precedence and associativity in C as well as differences between increment and decrement operators.
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

Answers for c Questions.

1 {A} . Each variable in C has an associated data type. It specifies the type of
data that the variable can store like integer, character, floating, double, etc.
Each data type requires different amounts of memory and has some specific
operations which can be performed over it. The data type is a collection of
data with values having fixed values, meaning as well as its characteristics.

i. Integer Data Type :


The integer datatype in C is used to store the integer numbers(any number
including positive, negative and zero without decimal part). Octal values,
hexadecimal values, and decimal values can be stored in int data type in C.
• Range: -2,147,483,648 to 2,147,483,647
• Size: 4 bytes
• Format Specifier: %d
Syntax of Integer
We use int keyword to declare the integer variable:

int var_name;

Character Data Type :


Character data type allows its variable to store only a single character. The
size of the character is 1 byte. It is the most basic data type in C. It stores a
single character and requires a single byte of memory in almost all
compilers.
• Range: (-128 to 127) or (0 to 255)
• Size: 1 byte
• Format Specifier: %c
Syntax of char
The char keyword is used to declare the variable of character type:
char var_name;
Float Data Type :
In C programming float data type is used to store floating-point values.
Float in C is used to store decimal and exponential values. It is used to store
decimal numbers (numbers with floating point values) with single precision.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f
Syntax of float
The float keyword is used to declare the variable as a floating point:
float var_name;

Double Data Type:


A Double data type in C is used to store decimal numbers (numbers with
floating point values) with double precision. It is used to define numeric
values which hold numbers with decimal values in C.
The double data type is basically a precision sort of data type that is
capable of holding 64 bits of decimal numbers or floating points. Since
double has more precision as compared to that float then it is much more
obvious that it occupies twice the memory occupied by the floating-point
type. It can easily accommodate about 16 to 17 digits after or before a
decimal point.
• Range: 1.7E-308 to 1.7E+308
• Size: 8 bytes
• Format Specifier: %lf
Syntax of Double
The variable can be declared as double precision floating point using
the double keyword:
double var_name;

{B}.

Precedence refers to the order in which operators are evaluated in an expression.


When an expression contains multiple operators, the one with higher precedence is
evaluated first. It helps determine the grouping of operands in the absence of
parentheses.

For example, in the expression 3 + 5 * 2, multiplication has a higher precedence than


addition, so the multiplication operation is performed first, resulting in 3 + (5 * 2).
Associativity:

Associativity is a property that determines the grouping of operators with the same
precedence. It defines the direction in which the operations are performed when they
have the same precedence.

For example, in the expression 6 / 2 / 3, division has left-to-right associativity, so the


expression is evaluated as (6 / 2) / 3.

Precedence and Associativity of Common Operators:

The following is a general guideline for the precedence and associativity of common
operators in many programming languages. Operators at the top have higher
precedence, and operators on the same line have the same precedence.

1. Unary Operators:
• +, - (unary plus and minus)
2. Multiplicative Operators:
• * (multiplication)
• / (division)
• % (modulo)
3. Additive Operators:
• + (addition)
• - (subtraction)
4. Relational Operators:
• < (less than)
• > (greater than)
• <= (less than or equal to)
• >= (greater than or equal to)
5. Equality Operators:
• == (equal to)
• != (not equal to)
6. Logical AND:
• &&
7. Logical OR:
• ||
8. Assignment Operators:
• =
• +=, -=, *=, /=, %= (compound assignment operators)

Note: It's essential to consult the documentation of a specific programming language


to get accurate information on the precedence and associativity of operators, as they
may vary between languages.
2 . {A}. Differences between Increment And Decrement Operators
The following table list the difference between the increment and decrement
operators:

Increment Operator Decrement Operator

Increment Operator adds 1 to the Decrement Operator subtracts 1


operand. from the operand.

The Postfix increment operator means The Postfix decrement operator


the expression is evaluated first using means the expression is evaluated
the original value of the variable and first using the original value of the
then the variable is variable and then the variable is
incremented(increased).The decremented(decreased).

Prefix decrement operator means


Prefix increment operator means the
the variable is decremented first
variable is incremented first and then
and then the expression is
the expression is evaluated using the
evaluated using the new value of
new value of the variable.
the variable.

Generally, we use this in decision- This is also used in decision-


making and looping. making and looping.

Example : Example of Increment Operator

include <stdio.h>

void increment()

int a = 5;
int b = 5;

// PREFIX

int prefix = ++a;

printf("Prefix Increment: %d\n", prefix);

// POSTFIX

int postfix = b++;

printf("Postfix Increment: %d", postfix);

// Driver code

int main()

increment();

return 0;

Output
Prefix Increment: 6
Postfix Increment: 5

Example of Decrement Operator

#include <stdio.h>

void decrement()

int a = 5;
int b = 5;

// PREFIX

int prefix = --a;

printf("Prefix = %d\n", prefix);

// POSTFIX

int postfix = b--;

printf("Postfix = %d", postfix);

// Driver code

int main()

decrement();

return 0;

Output

Prefix = 4
Postfix = 5

{B}.

Identifiers : In C programming, identifiers are used to name variables, functions, arrays,


and other user-defined entities.

Variable : A variable, in particular, is a storage location identified by a memory address and an


associated symbolic name (an identifier) which contains some known or unknown quantity of
information referred to as a value.

Rules for writing identifiers in C include:


1. Character Set:
• Identifiers in C can consist of letters (both uppercase and lowercase),
digits, and underscores.
• The first character must be a letter or an underscore.
2. Length:
• The C standard does not prescribe a specific limit on the length of an
identifier, but it is generally good practice to keep them reasonably
short and meaningful.
3. Case Sensitivity:
• C is case-sensitive, so myVariable and MyVariable would be treated as
different identifiers.
4. Reserved Words:
• Avoid using keywords and reserved words of the C language as
identifiers. For example, you cannot use words like int, char, for, etc., as
variable names.
5. Digits in the First Position:
• While C allows digits in identifiers, it is not recommended to start an
identifier with a digit.
6. Special Characters:
• C does not allow special characters (except underscore) in identifiers.
For example, totalAmount is valid, but total$Amount is not.
7. Consistency:
• Be consistent in naming conventions throughout your C code.
Common conventions include camelCase or
underscore_separated_names.

2 {A}. #include<stdio.h>
int main(){
int a ;

printf("Enter your number to check whether the given number is


positive or negative or zero.\n");
scanf("%d",&a);

if (a>0) {
printf("The Number you entered is Positive .");}

else if (a<0) {
printf("The Number you entered is Negative .");
}

else{
printf("The number you entered is Zero.");

return 0;}
OutPut :

Enter your number to check wheather the given number is positive or


negitive or zero.
5
The Number you entered is Positive .

{B}.
#include <stdio.h>

int main() {
char char1, char2, char3;

// Input three characters


printf("Enter the first character: ");
scanf(" %c", &char1);

printf("Enter the second character: ");


scanf(" %c", &char2);

printf("Enter the third character: ");


scanf(" %c", &char3);

// Display ASCII values


printf("ASCII value of %c: %d\n", char1, char1);
printf("ASCII value of %c: %d\n", char2, char2);
printf("ASCII value of %c: %d\n", char3, char3);

return 0;
}

Output :

Enter the first character: a

Enter the second character: b

Enter the third character: c

ASCII value of a: 97

ASCII value of b: 98

ASCII value of c: 99

You might also like