C Programming Concepts - Answers and Explanations

C Programming Concepts - Answers and Explanations

C Programming Concepts - Answers and Explanations

1. What is an Identifier? Rules of Identifiers

An identifier is a name used to identify a variable, function, array, or any other user-defined item. The rules for identifiers in C are:

  • Identifiers must begin with a letter (A-Z or a-z) or an underscore (_).
  • Following the first character, identifiers can contain letters, digits (0-9), and underscores.
  • Identifiers must not be a C keyword.
  • Identifiers are case-sensitive (e.g., Variable and variable are different).

2. What is a Keyword in C? Write Down Any 5 Keywords

A keyword is a reserved word in C that has a special meaning and is used by the compiler. Here are five examples of keywords:

  • int
  • return
  • if
  • for
  • while

3. What is a Data Type?

Data types define the type of data that a variable can hold. In C, common data types include:

  • int - for integers
  • float - for floating-point numbers
  • char - for characters
  • double - for double precision floating-point numbers

4. What are Qualifiers?

Qualifiers are used to modify the properties of data types. Examples include:

  • const - indicates that the value cannot be changed
  • volatile - tells the compiler that the value can be changed by external factors
  • signed - specifies that the variable can hold both positive and negative values
  • unsigned - specifies that the variable can only hold positive values

5. What is Formatted Input and Output? How Do You Print 6.99999 in C Program?

Formatted input and output refer to the methods used to input and display data in a specific format. To print 6.99999 in C, use the printf() function with appropriate formatting:

printf("%.5f", 6.99999);

6. List Out the Types of Operators

Operators in C include:

  • Arithmetic Operators (+, -, *, /, %)
  • Relational Operators (==, !=, >, <, >=, <=)
  • Logical Operators (&&, ||, !)
  • Bitwise Operators (&, |, ^, ~, <<, >>)
  • Assignment Operators (=, +=, -=, *=, /=, %=)
  • Unary Operators (+, -, ++, --)
  • Conditional (Ternary) Operator (?:)
  • Comma Operator (,)

7. a=10, b=3; c=a+b; What is the Result printf("c=%c \n", c);

The result of this statement will not be correct because c is an integer and %c is for characters. The correct format should be %d:

printf("c=%d \n", c);

8. char b='A'; What is the Result (i) printf("b=%c \n", b); (ii) printf("b=%d \n", b);

(i) printf("b=%c \n", b); prints the character A. (ii) printf("b=%d \n", b); prints the ASCII value of A, which is 65.

9. a=10, b=20, c=50; What is the Result (i) printf("c=%d \n", c=a|b); (ii) printf("c=%d \n", c=a&&b);

(i) c=a|b performs a bitwise OR operation, so c will be 30 (binary 1010 OR 10100 = 11110). (ii) c=a&&b evaluates to 1 because both a and b are non-zero.

10. What are Comments & Use of Comments?

Comments are annotations in the code that are ignored by the compiler. They are used to explain code, make it easier to understand, and keep track of changes. In C, comments are written using // for single-line comments or /* ... */ for multi-line comments.

11. What is the Output of int a=10, b=3, max; a>b ? max=a : max=b; printf("%d", max);

The output will be 10 because the ternary operator evaluates a>b (10 > 3) to true, so max is assigned a.

12. What is the Output of int a=4, b=5, c=6; printf("%d", a*b/c);

The output will be 3 because a*b/c evaluates to (4*5)/6, which is 20/6 = 3 (integer division).

13. What is the Output of int a=4, b=5; printf("%d\n%d", a++*a--, b++ + b--);

The output is implementation-dependent due to the order of evaluation, but it is typically 20\n10. The expression a++*a-- computes to 20, and b++ + b-- computes to 10.

14. x=20, y=3; z=x/y; What is the Result printf("z=%f \n", z);

The result will be z=6.000000 because x/y performs integer division before assigning to z, which is then displayed as a floating-point number.

15. a=10, b=3; c=a/b; What is the Result printf("c=%d \n", c);

The result will be 3 because a/b performs integer division, so 10/3 equals 3.

These explanations and examples cover fundamental concepts in C programming. Understanding these will help you write efficient and correct C code.

Comments

Popular Posts