Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LetUsC Chapters #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions C tutorial/LetUsC/Chapter 3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Let Us C #

## Decision Control Instruction ##
These are the instructions used to control the flow of the program in such a way so that it executes certain statements based on the outcome of a condition.
A *Decision Control Instruction* can be implemented in C using three statements
1. The **if** statement
2. The **if-else** statement
3. The **conditional operators**

### The **if** statement ###
* The *if* statement is used to execute a statement if the condition in the *if* statement is *true*. If the condition is not true, the statement is not executed and the program skips the *if* statement.
* The syntax for *if statement* is
<img width="337" alt="pic 1" src="https://user-images.githubusercontent.com/74143496/128640658-8dc6aa24-9064-4932-829c-6f0e1701b288.PNG">

* We can also use arithmetic expression in the condition for *if statement*. If the expression is a *non zero*, it gets executed as C language trets non-zero as *True*.
* *Relational operators* can be used in the condition, if the relation holds true, the program gets executed. The relational operators are shown in the figure.
<img width="295" alt="pic 2" src="https://user-images.githubusercontent.com/74143496/128640685-421f17ef-845b-42c3-a97a-caf0a1e8b9a8.PNG">

* Multiple statements can be used in the *if statement*, if the condition is hold true, all the statements within the if statements gets executed.

*NOTE: If there is only one statement within if, it is not mandatory to write the statement within '{}'. But, in case of multiple statements, it is mandatory.*

### The **if-else** statement ###
* The *if-else* statement is used to execute a statement when the condition is true and other statement when it is false.
* The syntax for *if-else* statement is


<img width="256" alt="pic 3" src="https://user-images.githubusercontent.com/74143496/128640789-53bcd927-4045-4858-84c5-46dd408f1a0f.PNG">

#### **Nested if-else** ####
* We write an **if-else** construct within either the body of **if** statement or the body of **else** statement.
* The syntax of *Nested if-else* is


<img width="175" alt="pic 4" src="https://user-images.githubusercontent.com/74143496/128640819-ef01eb31-2e58-40e5-84b8-ccc272eda590.PNG">

### The Conditional Operators ###
* They are used like *if-else* statement but the code is much smaller for it.
* The syntax for *Conditional Operator* is


<img width="338" alt="pic 5" src="https://user-images.githubusercontent.com/74143496/128640917-c8018764-d7b1-49b2-89ab-89021d6eccce.PNG">

In the following syntax, if the *expression 1* is true, then *expression 2* is executed and if the *expression 1* is false, then *expression 3* is executed.
### Logical Operators ###
There are 3 *Logical Operators* namely
1. AND '&&'
2. OR '||'
3. NOT '!'
#### AND operator ####
If both the operands are non-zero, it gives true.
#### OR operator ####
If any of the two operands is non-zero, it gives true.
#### NOT operator ####
It is used to reverse the logical output. So, if the condition is true, it will turn it false and vice-versa.
The following table will summarize the working of all the *Logical Operators*

<img width="285" alt="pic 6" src="https://user-images.githubusercontent.com/74143496/128640937-04afb4f7-3fe2-49c0-b094-a3b1fb552d97.PNG">

134 changes: 134 additions & 0 deletions C tutorial/LetUsC/Chapter1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# **Let us C**

### **Getting Started**

#### Introduction
* C is a programming language, designed and written by 'Dennis Ritchie' in 1972.
* It bacame very popular because it was reliable, simple and easy to use and replaced many familiar languages like PL/I, ALGOL, etc.
* Popular Operating system like windows, Unix, Linux and apart from that kernels of iOS, Android are also written in C.

#### Steps in Learning C language

Learning C language is just like learning any other human language and can be done in steps.
1. Learning Alphabets, Digits, Special Symbols
2. Learning about Constants, Variables, Keywords
3. Instructions
4. Making a Program

A conclusive relation can be drawn easily to understand how a language can be understood as shown in the figure below.


<img width="481" alt="Chapter 1, Pic1" src="https://user-images.githubusercontent.com/74143496/128599637-f4406c33-72c2-4fb9-ba27-f9d078766033.PNG">


##### Learning Alphabets, Digits, Special Symbols
All these entities used in C programming are as follows


<img width="391" alt="chapter 1, pic2" src="https://user-images.githubusercontent.com/74143496/128599667-591b53c3-fa21-4f53-ae77-341edd5c81be.PNG">

##### Learning about Constants, Variables, Keywords

###### *Constant*

A *constant* is the entity that doesn't changes.
###### Type of C constants
1. Primary constants
2. Secondary Constants

These are further categorised as


<img width="353" alt="chapter 1, pic3" src="https://user-images.githubusercontent.com/74143496/128599682-d3ebe744-4c66-4290-b064-c73fcc095381.PNG">


**Integer constant**

* As the name suggest, an *integer constant* should be an integer.

* It can be both positive and negative.

* The allowed range for integer constants is -2147483648 to +2147483647

**Real Constant**

* A *real constant* must have a decimal point.

* It can be either positive or negative

**Character Constant**

* A *character constant* can be a single alphabet, single digit or a single special symbol.

* The constant must be enclosed within single inverted commas, 'A', '1', '@'.

###### *Variables*
Variables can also have the same characters like constants such as Integer, Real, Character.
**Rules to construct Variable name**
* A variable name can be made up of any alphabet, digit.
* The first character in the name must either be an aplphabet or underscore.
* No other symbol except '_'can be used

###### *Keywords*

Keywords are the words whose meaning has already been explained to the C compiler.
There are 32 keywords in C language


<img width="320" alt="chapter1,pic4" src="https://user-images.githubusercontent.com/74143496/128599702-9f18a7b1-97f4-4641-8f91-0ce70def7056.PNG">


#### Comments in a C program

*Comments* in a C program are used to clarify what the statement actually means in the program. This is generally used by the programmer to keep track of the steps and understand a particular line after some time. This also helps other people to understand the logic used by the programmer.

* Comments can be written in following syntax


<img width="47" alt="chapter1,pic5" src="https://user-images.githubusercontent.com/74143496/128599718-ec54dea6-c1c2-4251-927f-5dc505217b98.PNG">

* We can type any text inside comments and it won't affect the code in any way.
* '//' can also be used to write comments.

#### main()

* *main()* forms the crucial part of any C program.
* It is a function and all statements that belong to main() must be written between {}.
* A C program must have atleast one function and that must be *main()*.
* This function returns an integer value and this written as *int main()*. The return value for success is '0' and that for failure is any non-zero number.

#### printf()
* *printf()* is a function which is used to print the output on the screen.
* To use the printf() function, '#include <stdio.h>' must be written in the program. *#include* is a preprocessor directory and *stdio.h* is *'standard input output header file'* in which the declaration of the printf() function is written.
* The general form of *printf()* function is **printf ("<format string>",<list of variables>);**
* It can print any real, integer, character value and also print the result of expressions by computing on its own.

#### scanf
* *scanf()* function is used to recieve values from the user.
* The declaration of this function is also written in *stdio.h*. Thus, it is mandatory to mention *#include <stdio.h>* in the program.
* To recieve input for any variable, we must use '&' symbol before the name of the variable. This symbol is an 'Address of' operator and gies the location number used by the variable in the memory.


<img width="356" alt="chapter1, pic6" src="https://user-images.githubusercontent.com/74143496/128599737-5f7010c5-16a6-47be-a00a-fa0042eec3c4.PNG">






















77 changes: 77 additions & 0 deletions C tutorial/LetUsC/Chapter2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Let us C #

## C Instructions ##

> An **instruction** is an order given to a computer processor by a computer program.

### Types of Instructions ###
* Type Declaration Instruction
* Arithmetic Instruction
* Control Instruction

#### Type Declaration Instruction ####
* This instruction is used to declare the type of *variable* being used in the program.


<img width="338" alt="Chapter 2 pic2" src="https://user-images.githubusercontent.com/74143496/128627888-4b6eea4f-bdb4-4a54-8ee9-59d918c3067e.PNG">

* We can also initialize the variable while declaring the type.
* The order in which the variable is declared does not matter, but we should not use a variable before defining it


<img width="321" alt="chapter 2, pic1" src="https://user-images.githubusercontent.com/74143496/128627891-c4d652e3-9712-4f93-bc26-1dd49a0593da.PNG">


#### Arithmetic Instruction ####
* A C arithmetic instruction has variable name on the left side of '=' and variable anme and constants on right side of '='.
* The variable and constants on the right side of '=' are connected by arithmetic operators.

*The variables and constants together are called '**operands**'*.
*During an arithmetic operation, the operands on the right side of '=' are calculated and then assigned to the variable on the left side of '='*
* An arithmetic statement can be of 'Integer mode', having all operands either integer variables or integer constants


<img width="320" alt="pic3" src="https://user-images.githubusercontent.com/74143496/128627925-f9e89580-5244-4f3a-8b95-d726492d56d5.PNG">

or 'Real mode' having all operands as real constants or real variables

<img width="318" alt="pic4" src="https://user-images.githubusercontent.com/74143496/128627930-02a0cfac-8b3b-45ec-a23b-3b97789e93d1.PNG">

or 'Mixed mode' having some operands as integers and some as real.

<img width="318" alt="pic5" src="https://user-images.githubusercontent.com/74143496/128627935-54b54320-e447-499a-8fb5-87fec8b60f63.PNG">


#### Difference between '/' and '%' operator ####
* '/' is called *division operator* and is used for division.
* '%' is called *modular division operator* and is used for returning remainder after division of two integers
* For example: '10/2' will give '5', while '10%2' will give '0'.
*Modular Operator cannot be used for float.*
*In modular operator, the sign of the remainder is always same as the sign of the numerator.*

#### Integer and Float conversions ####
* An arithmetic operation between *integer* and *integer* always yield *integer* result.
* An arithmetic operation between *real* and *real* always yield *real* result.
* An arithmetic operation between an *integer* and a *real* always yield a *real* result. In this case, the integer is promoted to real and thus, the result is real.


<img width="330" alt="pic 6" src="https://user-images.githubusercontent.com/74143496/128627970-d82af1db-5dad-45a6-ba38-f2dbe0c2ab1e.PNG">

* If in an assignment operator, the type of variable on the left hand side does not par with the expression on the right hand side, the value of the expression is promoted to the type of variable on the left hand side.
#### Heirarchy of Operations ####
* While executing an arithmetic statement, which has two or more operators, we need to decide which one to execute first.
* The priority in which the operations are performed are shown in the figure.


<img width="327" alt="pic7" src="https://user-images.githubusercontent.com/74143496/128627985-5a1dc0ab-3018-4357-b132-c0eb14d40c3a.PNG">

* If there are more than one set of parentheses, the innermost parentheses would be performed first.
#### Control Instructions in C ####
*Control Instructions* enables us to specify the order in which various instructions should be executed in a program by the computer.
These are further of 4 types
1. Sequence Control Instruction
2. Selection or Decision Control Instruction
3. Repitition or Loop Control Instruciton
4. Case Control Instruction