← Back to quizzesFree quiz

C Programming Fundamentals

Welcome to this comprehensive guide on the fundamentals of the C programming language. Whether you are a beginner or need a quick refresher, this course covers essential topics such as…

22 questions~11 min
C Programming Fundamentals — Qwi
0 / 22
Score: 0%
1

Which of the following identifiers is valid according to C naming rules?

2

What is the result of the expression `c = (a > b) ? a : b;` in C?

3

Which header file must be included to use the `malloc` function?

4

Given `#define CM_PER_INCH 2.54`, what will be the value of `cm` after executing `cm = CM_PER_INCH * inches;` if the user enters 5.0 for inches?

5

Which of the following is NOT a primitive (built‑in) data type in C?

6

What will the following code print? ```c char S='a'; printf("%c",S); ```

7

Which statement correctly describes the role of a preprocessor directive that begins with `#include`?

8

In the expression `area = 2 * pi * r;`, which operator has the highest precedence?

9

What is the effect of writing `const double taxrate = 0.0175;` in a C program?

10

Which of the following is a logical operator in C?

11

What will happen if the statement `int i, j;` is placed after the first use of `i` in a function?

12

Which of the following statements about comments in C is true?

13

When using `scanf` to read a string into `char name[10];`, what delimiter causes the input to stop?

14

Which of the following errors is detected by the compiler during the compilation phase?

15

What is the correct way to declare a constant macro for π with value 3.14159?

16

Which relational operator would you use to test if variable `x` is not equal to 10?

17

In the following code snippet, what is the type of the literal `‘A’`? ```c char ch = ‘A’;```

18

Which of the following statements about the `main` function is false?

19

When compiling a C program, which stage produces the final executable file?

20

What is the output of the following code? ```c int i = 5; printf("%d", i++); ```

21

Which of the following best describes a syntax error?

22

If a variable is declared as `float inches, cm;` and the user inputs `12.7` for inches, which format specifier should be used with `scanf` to read the value correctly?

C Programming Fundamentals: Core Concepts Explained

Welcome to this comprehensive guide on the fundamentals of the C programming language. Whether you are a beginner or need a quick refresher, this course covers essential topics such as identifiers, operators, preprocessor directives, memory allocation, and built‑in data types. Each section is crafted to be SEO‑friendly, using clear headings, concise paragraphs, and keyword‑rich lists to help you master C programming concepts.

1. Valid Identifiers in C

In C, an identifier is the name you give to variables, functions, arrays, and other user‑defined items. To be valid, an identifier must follow these rules:

  • Start with a letter (a‑z, A‑Z) or an underscore (_).
  • Contain only letters, digits (0‑9), or underscores.
  • Not be a reserved keyword (e.g., continue, return).
  • Be case‑sensitive ("gross_income" and "Gross_Income" are different).

Example of a correct identifier: gross_income. The following are invalid:

  • base+height – contains a plus sign.
  • continue – reserved keyword.
  • high balance – contains a space.

2. The Conditional (Ternary) Operator

The ternary operator ?: provides a compact way to assign a value based on a condition. Its syntax is:

variable = (condition) ? value_if_true : value_if_false;

For the expression c = (a > b) ? a : b;:

  • If a is greater than b, c receives the value of a.
  • Otherwise, c receives the value of b.

This operator is often used to replace simple if‑else statements, improving code readability.

3. Including Header Files: The Role of #include

Header files contain declarations of functions, macros, and types that your program can use. The preprocessor directive #include tells the compiler to copy the contents of the specified header file into your source file before compilation.

To use dynamic memory allocation functions such as malloc, you must include the standard library header:

#include <stdlib.h>

Without this inclusion, the compiler would not recognize malloc, leading to errors or undefined behavior.

4. Macro Constants and Simple Calculations

Macros allow you to define symbolic constants that the preprocessor replaces throughout your code. For example:

#define CM_PER_INCH 2.54

If a user inputs inches = 5.0, the calculation cm = CM_PER_INCH * inches; yields:

  • cm = 2.54 * 5.0 = 12.70

Using macros improves maintainability—changing the conversion factor in one place updates all related calculations.

5. Primitive (Built‑in) Data Types

C provides several primitive data types that map directly to hardware representations:

  • int – integer numbers.
  • char – single characters.
  • float – single‑precision floating‑point numbers.
  • double – double‑precision floating‑point numbers.

The type string is not a primitive in C. Strings are represented as arrays of char terminated by a null character (\0).

6. Printing Characters with printf

When you declare a character variable and print it using the %c format specifier, the character itself is displayed:

char S = 'a';
printf("%c", S); // Output: a

The output is the literal character a, not the variable name or any extra quotes.

7. Operator Precedence in Expressions

Understanding precedence determines how an expression is evaluated. In the expression area = 2 * pi * r;:

  • Multiplication (*) has higher precedence than assignment (=).
  • Both multiplication operators are evaluated left‑to‑right, producing the product 2 * pi * r before assigning the result to area.

Parentheses can be used to override default precedence, but in this case they are unnecessary because multiplication already takes priority.

8. Dynamic Memory Allocation with malloc

Dynamic memory allocation lets programs request memory at runtime. The function prototype is:

void *malloc(size_t size);

Key points to remember:

  • Always include <stdlib.h> to access malloc and related functions.
  • Check the returned pointer for NULL to ensure allocation succeeded.
  • Cast the returned void * to the appropriate pointer type (optional in C, required in C++).
  • Free allocated memory with free() to avoid memory leaks.

9. Summary of Core Concepts

Below is a quick reference that ties together the topics covered in this course:

  • Identifiers: Start with a letter or underscore; avoid keywords.
  • Ternary operator: condition ? expr1 : expr2 chooses between two values.
  • #include: Inserts header file contents; required for malloc (<stdlib.h>).
  • Macros: Use #define for constants like CM_PER_INCH.
  • Primitive types: int, char, float, double; no built‑in string type.
  • Printing characters: Use %c in printf.
  • Operator precedence: Multiplication before assignment; parentheses can change order.
  • Dynamic memory: Include <stdlib.h>, check for NULL, and free memory.

By mastering these fundamentals, you lay a solid foundation for more advanced C programming topics such as pointers, structures, and file I/O. Keep practicing the concepts with real code examples, and refer back to this guide whenever you need a quick refresher.