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

structural programming assignment

The document outlines the Structured Programming unit, focusing on the C programming language, its importance, and fundamental concepts such as tokens, data types, functions, and variable naming rules. It emphasizes the advantages of C, including portability and efficiency, and provides examples of code and constants. Additionally, it includes an example program for calculating the area of a circle.

Uploaded by

Kelvin Thamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

structural programming assignment

The document outlines the Structured Programming unit, focusing on the C programming language, its importance, and fundamental concepts such as tokens, data types, functions, and variable naming rules. It emphasizes the advantages of C, including portability and efficiency, and provides examples of code and constants. Additionally, it includes an example program for calculating the area of a circle.

Uploaded by

Kelvin Thamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT NAME: STRUCTURED PROGRAMMING

UNIT CODE: CS/C/7154

DATE OF SUBMISSION:29-01-2025

GROUP 3 MEMBERS:

KELVIN THAMU KANIA - 2025CS161668

LANGAT ERICK - 2025CS161367

KIPROP KORIR - 2025CS162222

FAITH CHEPCHIRCHIR- 2025CS162357

ENOCK KIPKOECH - 2025CS162754

FAITH NJERI MWATHI - 2025CS161465

KIPKOECH ENOCK- 2025CS162754


ASSIGNMENT

1. What is C programming language?

- C programming language is a general-purpose, procedural, middle-level programming


language for system administration, network programming, and embedded software.

(i) Importance of and Advantages of C Programming:

a. Portability - A versatile language suitable for cross-platform applications.

b. Efficient - Fast and simple; runs with minimal resources; ensures high execution speed in
critical systems.

c. Procedural - Organized into functions that executes a series of commands to perform tasks

d. Low-level Access –programming language C provides direct access to memory.

e. Easy Debugging - Tools like Ctrl + D help in error detection and debugging.

2. What is white space?

Characters used to create spaces, such as tabs in source code, to improve readability.

(i)Importance of White Space

a) Readability-Makes the code easier to locate and debug.

b) Separation of Tokens- Differentiates keywords, variables, operators, and function names


for the compiler to distinguish between them.

c) Legibility- Enhances the clarity of the code.

3. Tokens in C

i)Definition:

Basic building blocks of C; sequence of characters that represent a specific element in a


program.

ii. Types of Tokens in C:

-Keywords: Reserved words with predefined meaning (e.g., while, if, for).

-Identifiers: Names given to variables, functions, etc.


-Constants: Fixed values that do not change.

- String Literals: Represent sequences of characters.

4. Escape Sequences

- changes how the compiler interprets characters in a literal (string or character).

- Escape sequences are preceded by a backslash (\).

Examples of Escape Sequences:

\n: Newline

\t: Horizontal tab

\v: Vertical tab

\b: Backspace

\\: Backslash

\0: Null character

\": Double quote

\': Single quote

5. What is a Data Type in C?

- Defines the type of data a variable can hold.

Examples of Data Types:

- Primitive data types:

- int: Integer numbers (whole numbers).

- Float: Numbers with decimal points.

- Double: Double-precision floating numbers.

- Char: Single characters (e.g., letters).

- Derived data types:

- Arrays: Store variables of the same type.

- Example: float arr[5] = {6, 7, 4};


- Pointers: Store the address of another variable.

- Example: int* ptr = &age; // Points to the location of the variable age.

6) Functions:

- Functions are the fundamental building blocks of C. They help organize programs into sections,
making them modular and reusable.

Example:

int main() {

printf("Hello world");

return 0;

- Functions only run when called.

6)Variables:

- Variables are named memory locations for storing data values.

Examples:

- int: Stores integers.

- Float: Stores floating-point numbers.

- Char: Stores single letters (characters).

Variable Declaration and Initialization:

- Specify a type and assign a value.

- Syntax:

Example:

int a = 5;
type variable Name = value;

- The equal sign (=) is used to assign value 5 to it.

7.Rules for Naming/ declaring a variable in C programming?

i)Start with a Letter or Underscore:

A variable name must begin with a letter (a-z or A-Z) or an underscore (_).

Example valid names: age, total_sum, _counter.

ii)Subsequent Characters:

After the first character, the variable name can contain letters, digits (0-9), and underscores (_).

Example valid names: myVar, num1, count_3.

iii)No Reserved Keywords:

The name of the variable cannot be a keyword in C (e.g., int, return, if, while).

Invalid names: int, for, while.

iv)Case Sensitivity:

C is case-sensitive, meaning age, Age, and AGE are considered different variables.

Example: age and Age are two distinct variables.

7. What is a Constant?

- A constant is a name given to a variable whose value cannot be altered.

- Use the const keyword while declaring a constant.

Example:

const int myNum = 15; // Integer constant

- myNum will always be 15.


Types of Constants:

- Decimal Constants

- Real/Floating Constants

Example of Floating Constant:

#define PI 3.14159

9) Outline of a C Program (Example: Calculate the Area of a Circle)

Code example

#include <stdio.h> // Preprocessor directive for input/output

#define PI 3.14159 // Definition for PI

int main() {

float radius, area; // Declaration of variables

// Ask the user to enter the radius

printf("Enter the radius of a circle: ");

scanf("%f", &radius);

// Calculate the area of a circle using the formula: Area = PI × radius × radius

area = PI * radius * radius;

// Display the result

printf("The area of the circle with radius %.2f is %.2f\n", radius, area);
return 0; // Indicates the program executed successfully

You might also like