Introduction
Algorithm
Input: The data or variables that the algorithm will use to perform the task.
Output: The result or solution that the algorithm will produce.
Control Structures: These are the statements that determine the flow of the algorithm, such as if-then statements, loops, and conditionals.
Operations: These are the computations or actions that the algorithm will perform on the input to produce the output.
- Sorting algorithms, which are used to sort data in a specific order.
- Search algorithms, which are used to find a particular item in a list or dataset.
- Encryption algorithms, which are used to encode data to protect it from unauthorized access.
- Optimization algorithms, which are used to find the best possible solution to a problem.
Flow chart
Start/End: This symbol indicates the beginning or end of a process, typically represented by a rounded rectangle or oval shape.
Process: This symbol represents an action or step in the process, typically represented by a rectangle shape.
Decision: This symbol represents a decision point in the process, where a choice needs to be made, typically represented by a diamond shape. The decision is usually based on a yes or no question.
Input/Output: This symbol represents data or information being input into or output from the process, typically represented by a parallelogram shape.
Connector: This symbol is used to connect different parts of the flowchart together, typically represented by a circle shape.
Introduction of programming languages
History of C
Basic structure of C Programming
Preprocessor directives: These are commands that tell the compiler to perform certain tasks before the code is compiled. They are usually indicated with a hash symbol (#) at the beginning of the line.
Functions: These are the building blocks of a C program. A function is a block of code that performs a specific task. Every C program must have at least one function, called the main function.
Variables: These are containers that hold values that can be used in the program. They must be declared before they are used, and their data type must be specified.
Statements: These are individual instructions that tell the computer what to do. They end with a semicolon (;).
Comments: These are notes that are added to the code to explain what it does. They are ignored by the compiler and are indicated with two forward slashes (//) for single-line comments, or with /* and */ for multi-line comments.
Executing C Program
Write the program: Use a text editor to write the code for the C program. Save the file with a .c extension.
Compile the program: Open a terminal or command prompt and navigate to the directory where the program is saved. Use a C compiler, such as gcc, to compile the code into an executable file. This can be done by typing
gcc programname.c -o programname
into the terminal. This will create an executable file namedprogramname
.Run the program: To run the program, type
./programname
into the terminal. This will execute the compiled code and display the output of the program.
Write the program: Use a text editor to write the code for the program and save the file as
hello.c
.Compile the program: Open a terminal and navigate to the directory where
hello.c
is saved. Typegcc hello.c -o hello
to compile the code into an executable file namedhello
.Run the program: Type
./hello
into the terminal to execute the compiled code and display the output of the program.
Data types
Constant
Integer constants: These are whole numbers (with or without a sign) that are represented using the digits 0 to 9. For example: 42, -73, and 0.
Floating-point constants: These are numbers with decimal points or exponents, represented using the format: [digits][.][digits][e|E][sign][digits]. For example: 3.14, -2.5E-3.
Character constants: These are single characters enclosed in single quotes. For example: 'A', '9', or '$'.
String constants: These are a sequence of characters enclosed in double quotes. For example: "Hello, world!".
Enumeration constants: These are user-defined constants that represent a set of named integer values.
Variables
Identifiers
Keywords
- if: Used to create conditional statements. For example: "if (x > 5) { printf("x is greater than 5"); }"
- while: Used to create loops. For example: "while (x < 10) { printf("%d\n", x); x++; }"
- int: Used to declare an integer variable. For example: "int age = 25;"
- char: Used to declare a character variable. For example: "char firstInitial = 'J';"
- void: Used to define a function that doesn't return a value. For example: "void printName(char* name) { printf("Hello, %s!", name); }"
Tokens
- Keywords: Words that have special meanings in the language, such as "if" and "while".
- Identifiers: Names that represent variables, functions, or other user-defined items.
- Constants: Values that do not change during the execution of the program, such as integers or strings.
- Strings: A sequence of characters enclosed in double quotes.
- Operators: Symbols used to perform mathematical or logical operations, such as "+" or "&&".
- Special symbols: Characters with special meanings in C, such as parentheses or semicolons.
Declaration of Variables
Assigning values to variables
OperatorsIn C programming language, an operator is a symbol or a keyword that is used to perform certain operations on one or more operands.
There are several types of operators in C:Arithmetic Operators: They are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus (%).
Relational Operators: They are used to compare two values and return a boolean value (true or false) based on whether the comparison is true or false. Examples of relational operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
Logical Operators: They are used to combine multiple conditions or expressions and return a boolean value based on the result of the combination. The logical operators include AND (&&), OR (||), and NOT (!).
Assignment Operators: They are used to assign a value to a variable. The basic assignment operator is =, but there are also compound assignment operators such as +=, -=, *=, /=, and %=.
Increment and Decrement Operators: They are used to increase or decrease the value of a variable by 1. The increment operator is ++ and the decrement operator is --.
Bitwise Operators: They are used to perform operations on the binary representation of values. The bitwise operators include AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>).
Conditional Operator: It is used to evaluate a condition and return one of two values based on the result of the evaluation. The conditional operator is also known as the ternary operator because it takes three operands.
These are the basic operators in C programming language that are used to perform various operations on variables and values.
Arithmetic Operators: They are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus (%).
Relational Operators: They are used to compare two values and return a boolean value (true or false) based on whether the comparison is true or false. Examples of relational operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
Logical Operators: They are used to combine multiple conditions or expressions and return a boolean value based on the result of the combination. The logical operators include AND (&&), OR (||), and NOT (!).
Assignment Operators: They are used to assign a value to a variable. The basic assignment operator is =, but there are also compound assignment operators such as +=, -=, *=, /=, and %=.
Increment and Decrement Operators: They are used to increase or decrease the value of a variable by 1. The increment operator is ++ and the decrement operator is --.
Bitwise Operators: They are used to perform operations on the binary representation of values. The bitwise operators include AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>).
Conditional Operator: It is used to evaluate a condition and return one of two values based on the result of the evaluation. The conditional operator is also known as the ternary operator because it takes three operands.