logo

Chapter 5: Programming Concepts and the C Programming Language

Computer Science - Class 11

No MCQ questions available for this chapter.

Chapter 5: Programming Concepts and the C Programming Language

5.1 Programming Concepts

5.1.1 Introduction to Programming Languages

A programming language is a formal set of instructions that can be used to produce various kinds of output, including software applications and algorithms. These languages allow humans to communicate with computers. Key components include:

  • Syntax: The set of rules that define the structure of the language.
  • Semantics: The meaning of the statements in the language.

5.1.2 Low-Level, High-Level, and 4GL Programming Languages

  • Low-Level Languages: These languages are closer to machine code. They include:

    • Assembly Language: Uses symbolic instructions that are translated into machine code. It provides little abstraction from the computer's instruction set.
    • Machine Language: The binary code understood by a computer's CPU.
  • High-Level Languages: These languages provide a higher level of abstraction and are easier for humans to understand. Examples include Python, Java, and C++. They are closer to natural languages and require a compiler or interpreter to convert them into machine code.

  • 4GL (Fourth Generation Language): These languages are designed to be more user-friendly and reduce the complexity of programming. They are often used for database queries and report generation. Examples include SQL and MATLAB.

5.1.3 Compiler, Interpreter, and Assembler

  • Compiler: A program that translates high-level source code into machine code before execution. The entire program is converted at once, which often results in faster execution times.

  • Interpreter: A program that translates high-level code into machine code line by line during execution. This allows for easier debugging but may lead to slower execution times.

  • Assembler: A program that translates assembly language (low-level language) into machine code. It converts mnemonic instructions into binary code.

5.1.4 Syntax, Semantic, and Runtime Errors

  • Syntax Errors: Mistakes in the code that violate the rules of the programming language. These are detected during the compilation or interpretation process.

  • Semantic Errors: Errors that occur when the syntax is correct, but the code does not make logical sense (e.g., using the wrong variable).

  • Runtime Errors: Errors that occur while the program is running, such as division by zero or attempting to access an invalid memory location.

5.1.5 Control Structures: Sequence, Selection, and Iteration

  • Sequence: The default control structure where statements are executed in the order they appear.

  • Selection: Used to choose between different paths of execution based on conditions. Common selection statements include:

    • if statements
    • switch statements
  • Iteration: Allows for repeated execution of a block of code. Common iteration statements include:

    • for loops
    • while loops
    • do while loops

5.1.6 Program Design Tools – Algorithm, Flowchart, and Pseudocode

  • Algorithm: A step-by-step procedure or formula for solving a problem. It outlines the logic of the program.

  • Flowchart: A graphical representation of an algorithm, using symbols to represent different types of actions or steps.

  • Pseudocode: A way of expressing an algorithm using structured but informal language that resembles programming languages. It is not executable but helps in understanding the logic.

5.1.7 Absolute Binary, BCD, ASCII, and Unicode

  • Absolute Binary: The simplest binary representation used by computers, representing values directly in base-2.

  • BCD (Binary-Coded Decimal): A method of representing decimal numbers where each digit is encoded as a binary value. For example, the decimal number 45 is represented as 0100 0101 in BCD.

  • ASCII (American Standard Code for Information Interchange): A character encoding standard that uses 7 bits to represent characters. It includes 128 characters, such as letters, digits, and symbols.

  • Unicode: A character encoding standard that aims to include every character from every language. It can use up to 32 bits and supports a vast range of characters.

5.2 C Programming Language

5.2.1 Introduction and Features of C Language

C is a high-level programming language developed in the early 1970s. It is known for its efficiency and flexibility. Key features include:

  • Low-level access to memory (pointer arithmetic).
  • Simple and efficient syntax.
  • Portability across different platforms.
  • Rich set of operators and built-in functions.
  • Structured programming support through functions.

5.2.2 Structure of C Program

A typical C program includes:

  1. Preprocessor Directives: Lines that begin with # (e.g., #include).
  2. Main Function: The entry point of the program, defined as int main().
  3. Variable Declarations: Definition of variables used in the program.
  4. Function Definitions: Other functions used by the program.
  5. Return Statement: Indicates the end of the main function.

Example Structure:

c
#include <stdio.h> int main() { // Code goes here return 0; }

5.2.3 C Preprocessor and Header Files

  • C Preprocessor: A tool that processes directives before actual compilation. It handles tasks like file inclusion and macro substitution.

  • Header Files: Files that contain declarations for functions and macros. For example, #include <stdio.h> includes standard input-output functions.

5.2.4 Character Set Used in C

The character set in C includes:

  • Letters (uppercase and lowercase)
  • Digits (0-9)
  • Special characters (e.g., +, -, *, /, %, =, !, etc.)
  • Whitespace characters (spaces, tabs, newlines)

5.2.5 Use of Comments

Comments are used to document code. There are two types of comments in C:

  • Single-Line Comments: Begin with // and continue to the end of the line.
  • Multi-Line Comments: Enclosed between /* and */.

Example:

c
// This is a single-line comment /* This is a multi-line comment */

5.2.6 Identifiers, Keywords, and Tokens

  • Identifiers: Names given to elements in a program (e.g., variables, functions). They must begin with a letter or underscore and can contain letters, digits, and underscores.

  • Keywords: Reserved words in C that have special meaning (e.g., int, return, if, while, etc.).

  • Tokens: The smallest elements in a program. Tokens include keywords, identifiers, constants, operators, and punctuation.

5.2.7 Basic Data Types in C

C supports several basic data types, including:

  • int: Integer type.
  • float: Single-precision floating-point type.
  • double: Double-precision floating-point type.
  • char: Character type.

5.2.8 Constants and Variables

  • Constants: Fixed values that do not change during program execution (e.g., const int MAX = 100;).

  • Variables: Named storage locations that can hold different values during program execution.

5.2.9 Type of Specifier

C uses type specifiers to define the type of data that a variable can hold. Common specifiers include:

  • short: Short integer.
  • long: Long integer.
  • unsigned: Non-negative integer.

5.2.10 Simple and Compound Statements

  • Simple Statement: A single line of code that performs an action (e.g., variable assignment).

  • Compound Statement: A block of code enclosed in {} that can contain multiple statements. It is often used in functions and control structures.

Example:

c
if (condition) { // Compound statement statement1; statement2; }

5.2.11 Operators and Expressions

C supports various operators, including:

  • Arithmetic Operators: +, -, *, /, %.
  • Relational Operators: ==, !=, <, >, <=, >=.
  • Logical Operators: &&, ||, !.
  • Assignment Operators: =, +=, -=, *=, /=.
  • Unary Operators: ++ (increment), -- (decrement).
  • Conditional Operator: ?: (ternary operator).

5.2.12 Input/Output (I/O) Functions

C provides several standard I/O functions:

  • printf(): Used to output data to the console.
  • scanf(): Used to read input from the console.

Example:

c
int num; printf("Enter a number: "); scanf("%d", &num);

5.2.13 Selection Control Statement: Decisions

  • if Statement: Executes a block of code if a condition is true.
  • if-else Statement: Executes one block if the condition is true, another if it is false.
  • if-else-if Ladder: A chain of conditions.
  • switch Statement: A control structure that selects one of many blocks of code to execute based on the value of an expression.

5.2.14 Iteration Control Statement: Looping

C provides various looping structures:

  • while Loop: Repeats a block of code while a condition is true.
  • do while Loop: Similar to the while loop, but checks the condition after executing the block.
  • for Loop: Used for a known number of iterations.

Example of a for loop:

c
for (int i = 0; i < 10; i++) { // Code to execute }

5.2.15 Array: Definition, Types (1D and 2D), Matrix Addition and Subtraction

  • Array: A collection of elements of the same data type, stored in contiguous memory locations.

  • 1D Array: A single-dimensional array (e.g., int arr[5];).

  • 2D Array: A two-dimensional array (e.g., int matrix[3][3];). It is often used to represent matrices.

  • Matrix Addition and Subtraction: Operations on 2D arrays where corresponding elements are added or subtracted.

5.2.16 String: Definition and String Functions

  • String: An array of characters terminated by a null character ('\0').

Common string functions in C include:

  • strlen(): Returns the length of a string.
  • strcat(): Concatenates two strings.
  • strcmp(): Compares two strings.
  • strrev(): Reverses a string.
  • strcpy(): Copies one string to another.
  • strlwr(): Converts a string to lowercase.
  • strupr(): Converts a string to uppercase.

Example of string functions:

c
char str1[20], str2[20]; strcpy(str1, "Hello"); strcpy(str2, "World"); strcat(str1, str2); // str1 now contains "HelloWorld"