COMPUTER PROGRAMMING (CT 101) – Overview of C: Complete IOE Notes
An overview of C programming code and concepts
Overview of C Programming
3 hours | 4 marks

COMPUTER PROGRAMMING (CT 101) – Chapter 2

This chapter provides a foundational overview of C programming, a powerful and widely-used language. We will explore its history, basic structure, and core components like data types, variables, and constants. Understanding these fundamentals is the first step to mastering C programming.

Chapter Information

Chapter 2: Overview of C (3 hours) – 4 marks

Course: Computer Programming (CT 101), I Year I Part

Description: This guide provides a complete overview of C Programming, covering its history, features, basic structure, data types, variables, constants, and more, as per the IOE syllabus.

Credit: Important Notes

Detailed Chapter Notes

2.1 Introduction & History of C Programming

C is a powerful, general-purpose programming language known for its efficiency and control. It’s a procedural language, meaning it follows a series of steps to solve a problem.

2.1.1 History of C Evolution

The history of C is a story of refinement, building upon the ideas of its predecessors.

ALGOL (1960s) ► BCPL ► B ► K&R C (1978) ► ANSI C (1989) ► C99 (1999)

  • ALGOL (1960s): An early language that introduced key concepts like block structure and structured programming, which heavily influenced C.
  • BCPL (Basic Combined Programming Language): Developed by Martin Richards, it was designed for writing compilers and system software.
  • B Language: Created by Ken Thompson at Bell Labs, it was a simplified version of BCPL and was used to create early versions of the UNIX operating system.
  • K&R C (1978): After C was created by Dennis Ritchie, he co-authored the book “The C Programming Language” with Brian Kernighan. This book served as the language’s informal specification for many years.
  • ANSI C / C90 (1989): The American National Standards Institute created the first official standard for C to ensure that code was portable between different compilers.
  • C99 (1999): A later revision of the standard that added new features like inline functions and new data types.

2.2 C Headers and Library Functions

The C Standard Library is a collection of pre-written functions and macros that perform common tasks. To use these functions, you must include the corresponding header file (with a .h extension) at the beginning of your code.

2.2.1 Common Header Files and Their Functions

  • <stdio.h> (Standard Input/Output): Contains functions for input and output operations.
    • Functions: printf(), scanf(), fopen(), fclose().
  • <stdlib.h> (Standard Library): Includes functions for memory allocation, process control, and number conversions.
    • Functions: malloc(), calloc(), free(), exit(), rand().
  • <string.h> (String Handling): Provides functions for manipulating strings.
    • Functions: strcpy(), strcat(), strlen(), strcmp().
  • <math.h> (Mathematics): Contains common mathematical functions.
    • Functions: sqrt(), pow(), sin(), cos(), log().

2.3 Preprocessor Directives

2.3.1 Definition and Examples

Preprocessor directives are lines in your code that begin with a # character. They are processed by the preprocessor before the program is actually compiled.

  • #include: This directive tells the preprocessor to copy the contents of a specified header file into your source code.
    #include <stdio.h>
  • #define: This directive creates a symbolic constant or a macro. The preprocessor replaces all occurrences of the macro with its defined value.
    #define PI 3.14159

2.4 Basic Structure of a C Program

2.4.1 The 6 Sections of a C Program

  1. Documentation Section: Contains comments to explain the logic, author, and date. Example: /* Program to add two numbers */
  2. Link Section: Includes header files needed for the program. Example: #include <stdio.h>
  3. Definition Section: Defines symbolic constants. Example: #define MAX 100
  4. Global Declaration Section: For variables and functions that need to be accessible from anywhere in the program.
  5. main() Function: The heart of the program where execution begins. Every C program must have one main() function.
  6. Sub-program Section: Contains the definitions of user-defined functions that are called from main().

2.5 C Tokens

A token is the smallest element of a C program that is meaningful to the compiler.

2.5.1 Character Set

The set of characters that can be used to write a C program.

  • Letters: A-Z, a-z
  • Digits: 0-9
  • Special Characters: + - * / % & _ ( ) { } [ ] # etc.
  • Whitespace Characters: Space, tab (\t), newline (\n).

2.5.2 Keywords

There are 32 reserved words in C that have special meanings and cannot be used for other purposes (like variable names).

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

2.5.3 Identifiers

An identifier is a name used to identify a variable, function, or any other user-defined item.

Naming Rules & Guidelines:

  • An identifier must start with a letter or an underscore (_).
  • Subsequent characters can be letters, digits, or underscores.
  • Keywords cannot be used as identifiers.
  • Identifiers are case-sensitive (e.g., sum and SUM are different).
  • Use meaningful names to improve code readability (e.g., totalScore instead of ts).

2.6 Type Casting

2.6.1 Implicit and Explicit

  • Implicit Type Casting: This is an automatic conversion performed by the compiler. When you perform an operation involving two different data types, the “lower” type is promoted to the “higher” type to prevent data loss. For example, when adding an int and a float, the int is temporarily converted to a float.
  • Explicit Type Casting: This is a manual conversion forced by the programmer using the cast operator (data_type). This is useful when you want to intentionally change the data type, even if it means losing data.
double price = 19.99;
int approximatePrice = (int)price; // Explicit cast: value becomes 19
            

2.7 Data Types, Variables and Constants

2.7.1 Data Types

Data types define the type of data a variable can hold.

  • Fundamental (Primary): The basic building blocks, including int, float, double, and char.
  • Derived: Constructed from fundamental types, including arrays, pointers, structures (struct), and unions (union).
  • User-Defined: Types defined by the programmer using typedef (to create an alias for a type) and enum (to create a set of named integer constants).

2.7.2 Variables

A variable is a named memory location to store data.

  • Declaration: You must declare a variable’s type before using it (e.g., int count;).
  • Types: Variables can be of any data type (int, float, char, etc.).
  • Scope: This defines the region of the code where a variable is accessible.
    • Local Variables: Declared inside a function or block, accessible only within that block.
    • Global Variables: Declared outside all functions, accessible from any part of the program.

Constants

Constants are fixed values that do not change. They are defined using the const keyword (e.g., const float PI = 3.14;) or the #define directive.

2.8 Compilers and IDE for C

  • Compiler: A compiler is a special program that translates your C source code into machine code that the CPU can execute. Examples include GCC (GNU Compiler Collection) and Clang.
  • IDE (Integrated Development Environment): An IDE is a software application that bundles essential tools for programmers, including a source code editor, a compiler/interpreter, and a debugger. Popular IDEs for C include Code::Blocks, Visual Studio Code, and Dev-C++.

PDF Notes

×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.

We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.

Scroll to Top