Introduction😊
Human beings have interacted with machines through high-level programming (binary language).
Hardware- Machine language- Assembly language - High-level languages - Forten, COBOL, Pascal
Machine language instructions are difficult to remember so the assembly language is introduced in symbol forms.
Symbols in assembly languages are also difficult to remember so high level is introduced such as c languages, c++, C#, java etc.
History of programming language:-
Language | Released date |
ALGOL | 1960 |
BCPL | 1967 |
B-1970 | 1970 |
Traditional C | 1972-73 |
ANSI C | 1989 |
ANSI / ISOC | 1990 |
C17 is an updated version of C
Dennis Ritchie received the Turing Award in 1983.
The computer doesn't know English & only knows high-level language which a computer will help
There are three types of translators:-
Compiler - Convert high-level to machine language
Interpreter - Convert high-level to low-level
Assembler - Convert assembly code into machine code
Important Points 👇
You should include the header file i.e. <stdio.h> in c and then int main- where program execution starts.
printf
tells print the format.printf
information is only under the stdio.h fileC provides libraries that include different header files having different purposes
#include is a preprocessor
C is fast & case sensitive
Features in c are simplicity, speed, rich libraries, memory management, modularity, portability, extendibility, and pointers.
variables is a place in RAM that holds the values in the program
rules for naming c variables
Letter or underscore
Case sensitive
Digits, letters
Bo special symbols allowed
Typespacifier - %d to print int value
There are 32 keywords & that are reserved
Constant - You cannot change the variable
Types of constant -
Decimal
,Floating point
,Actual
,Hexadecimal
,Character
,String
Basic Structures of a C Program
Documentation - The comment on the programs is not included in the compilation
Link - includes a library we use in our c program
Definition - Specifies the body or implementation of a function or variable
Global declaration - If we declare any data globally we can access it throughout the programs
Main function - Execution starts here
subprograms - Here Functions are used throughout the programs
- Tokens in C - They are the smallest Units
Keywords - int, char, if, while
Constant- 15,0.26,-22
Strings - “Hello”,“World”
Operators - +,-,=
Identifier - marks, amount
Special symbols - [ ], { }
Datatype
It is a type of data that a variable will hold in the memory given by a user.
Datatype in C
Primitive(already known to the compiler) - int, char, float, double
Derived(Derived from primitive datatype) - array, structure, union, enum, pointer.
DATATYPE | MEMORY(BYTES) | FORMAT SPECIFIER | |
single | char | 1 | %c |
single | int | 2 or 4 | %d |
complex | long int | 4 | %ld |
complex | long long int | 8 | %lld |
fractional | float | 4 | %f |
complex | double | 8 | %lf |
complex | long double | 16 | %ld |
Operators
It is a special symbol in c that specifies some task
Types of Operator
Arithmetic Operator - performs mathematical operations such as addition, subtraction, multiplication, and division. Ex- + , - , * , / , %
Relational Operator - They are used to compare two quantities or values. Ex - == , !=, >, < ,>= , <=
Logical Operator - They are used when we test multiple conditions to make decisions.Ex- &&(And operator) , ||(Or operator) , !(Not operator).
Assignment Operator - It is used to assign value to a variable. Ex - = ,+= ,-=
Bitwise & Operator - It is used to perform the operations on the data at the bit-level. it consists of two digits, either 0 & 1. Ex - & , | , ^, ~ , << , >>
Increment & Decrement operator - They are unary operators that add or subtract one from their operand Ex - if a=5; ++a; a=6: a++; a=7(In pre-increment operator first it increases the value by 1 then assign the value), --a; a=6: a - -; a=5(In post increment first assign the value then increment by 1)
Conditional Operator (ternary) - It's an operator that is the decision-making operator depends upon the output of the expression. Ex -
condition? true statement: false statement
Main() is a function, Void is not recommended.
Type Specifier - It is a sequence format by initial(%) which is used to specify the type and format of data that tells the compiler what type of data a variable is going to hold.
Data Type | Format | Size(in bytes) |
int | %d | 4 |
char | %c | 1 |
float | %f | 4 |
double | %lf | 8 |
long int | %ld | 4 |
Precedence of operator
- It tells which operator is executed first if there is more than one operator in an expression.
Operator | Type |
( ) [ ] . -> | |
(++)(- -)(+ -) ! ~ * & | unary |
* / % | arithmetic |
+ - | arithmetic |
<< >> | shift |
< <= > >= | relational |
\== != | relational |
& | bitwise and |
^ | bitwise or |
&& | logical and |
? ; | ternary conditional operator |
\= += -= *= /= &= ^= | assignment |
\= <<= >>= |
C library function
- They are predefined functions that are built-in functions already known to the compiler.
stdio.h -
printf()
,scanf()
, gets(), puts() ,fopen()
,fclose()
math.h - sin() , cos() , pow() , sqrt(), floor() ,ceil()
string.h -
strlen()
,strcmp()
,strcpy()
conio.h -
clrscr()
,getch()
,getche()
time.h -
time()
,setdate()
,getdate()
If you want to print the name write it in
printf(“”)
quotation markIf you want to print integer value use %d
scanf()
use to take input from the user use %d for integer value.
Type casting
changes the behaviour of one data type to another data type.
Two types of typecasting
Implicit typecasting - smaller type to larger type
Explicit typecasting - larger type to smaller type
For 1 decimal place write %1d or %1f
Function
Input Function - allows you to take input from the user through the keyboard. Ex -
getchar()
,gets()
- use to input characters & strings.Output Function - It prints a message or value of a variable at the output screen. Ex - printf() - 1 word , putchar() - character , puts() - string.
Formatted Input - we can design as per our need
Control/ character | Explanation |
%c | a single character |
%d | a decimal integer |
%i | an integer |
%o | an ictal no |
%s | a string |
%x | a hexadecimal no |
Flow Control
Flow control in the c program describes the order in which statements will be executed at run time.
Three types of flow charts
Decision control
It is executed when the condition is true, otherwise, the else part statements are executed.
Types of decision control statements
if statement - execute for a true statement
if else statement - if execute otherwise else execute
if else if ladder - execute with the condition given multiped
nested if - including if-else statement inside the body of if else statement
switch case - alternate to if else- if ladder statement which allows us to execute multiple operations
Looping control
It allows to execute sequence of statements again & again until the condition becomes false
Types of loop control statement
while loop - If a condition is true & only the body of a loop is executed.
do while loop - always executed after the body of a loop. It is also called an exit-controlled loop.
for loop - It performs only at once
Jumping control
It allows one to jump to another section of the unconditionally when encountered.
Types of jump control are
break - It is used to end the loop immediately after the encounter.
Continue - It allows us to skip a particular statement if we don’t want to write any statement inside a condition.
Goto - It is used to transfer the program control to a predefined label.
Function
a function is a self-contained block of code that is written in a curly { } braces.
Function benefits
To improve the readability of code.
Debugging the code will be easier if you are using functions, as errors can be easily traced.
Reduce the size of the code.
Types of Functions
Library function - already known to the compiler(predefined)
User-defined - created by the user
- The function has 3 things
function declaration
function call
function definition
- Classified functions into 4 Types depending upon argument & return type
Function with argument & a return type
Function with argument & no return type
Function with no argument & no return type
Function with no argument & return type
Conclusion
Follow Me On Socials :
Like👍| Share📲| Comment💭