0% found this document useful (0 votes)
3 views24 pages

C program

The document provides an overview of C programming concepts including libraries, data types, operators, control structures, loops, and functions. It explains how to take input, define variables, and use various operators, along with examples of control flow using if-else statements, switch cases, and loops. Additionally, it covers advanced topics like pointers, arrays, and recursion, emphasizing the importance of syntax and structure in C programming.

Uploaded by

Rajat Suvarna
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)
3 views24 pages

C program

The document provides an overview of C programming concepts including libraries, data types, operators, control structures, loops, and functions. It explains how to take input, define variables, and use various operators, along with examples of control flow using if-else statements, switch cases, and loops. Additionally, it covers advanced topics like pointers, arrays, and recursion, emphasizing the importance of syntax and structure in C programming.

Uploaded by

Rajat Suvarna
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

Video 1

 C has several libraries such as stdio.h which can be called by #incude<..> to use functions as printf and scanf

 To take input we use scanf() and in the brackets we use "%d", &(variable assigned)

 When we use int main() we have to conclude code with return 0

Video 2

 Flow of C program: Preprocessing, Compilation, Assembly, Linking, Loading

 Program gets stored in RAM and variable is a memory location, to define variable we use functions like (int a for no.,
char a for alphabets,

 float a for decimal no., also int_a can be used.

Types of Data types

 Basic Data Types - int, float, char, double


 Derived Data Types – array, pointer, structure, union
 Enumeration Data – enum
 Void Data Types – void

Data type sizes

How to calculate RAM into bytes


For egs: You have 6GB RAM that means in bytes
i.e. 6 x 1024 MB
6 x 1024 x 1024 KB
6 x 10243 Bytes = 6,44,24,50,944 bytes

How to calculate size of long

Use &lu to get size and use sizeof function(data type) for display of the size

NOTE: Use arithmetic calculation of a two number directly in printf if you want to
use program for a 2 definitely know no.s

Video 3

Operators

An Operator is a symbol used to perform operations in a given programming language.

Types of Operators

 Arithmetic Operators : +, -, *, /, %(modulus)


 Relational Operations
 Logical Operators
 Bitwise Operators
 Assignment operators
Note : % cannot be used for float no.s
Relational Operators

Logical Operators
Eg:
a = 45, b = 89 a&&b = 1 (means true) or if a = 0 now a&&b = 0 (means false)
a = 45, b = 89 a||b = 1 (means true) or if a = 0 now a||b = 1 (means true),
if both = 0 then a||b = 0 (false)
a = 45, b = 89 !(a&&b) = 0 (means false) or if a = 0 now !(a&&b) = 1 (means true)
a = 45, b = 89 !(a||b) = 0 (means false) or if a = 0 now !(a||b) = 0 (means false)

Bitwise Operators

Eg: a = 2, b = 3, then a&b=2 as 2 in binary is 10 and 3 is 11 by using & operator we get 10 which means 2
a = 2, b = 3, then a|b=3 as 2 in binary is 10 and 3 is 11 by using & operator we get 11 which means 3
a = 2, b = 3, then a^b=1 as 2 in binary is 10 and 3 is 11 by using & operator we get 01 which means 1
Other Bitwise operators (Boolean Algebra)
Assignment Operators

Eg: “=” >>> a = 7


“+=” >>> a = 7 >>> a+=1 >>> a = 8
“-=” >>> a = 7 >>> a-=1 >>> a = 6
“*=” >>> a = 7 >>> a*=4 >>> a = 28
“/=” >>> a = 30 >>> a/=6 >>> a = 5

Miscellaneous Operators
Operator precedence in C

Video 4
Format Specifier

 Format Specifier is a way to tell the compiler what type of data is in a variable during taking input
displaying output to the user.
 printf(“This is a good boy %[Link]”, var); will print var with b decimal points in a ‘a’ character space.

Eg: int a, float b are the way to define data types of a variable. Now say b = 3.7556

printf(“Rajat is a God = 0.7f ”,b); means the result will be like 3.7556000 and if I had put 5.7f then result
will be 3.7556 but if it is 7.7f then result will be [__3.7556] thus living two spaces in the front of the actual
no. and in -8.7f it will be [3.7556___]
thus digits before decimal point defines the no. of digits after decimal in the value whereas no. before
decimal point define the no. of digits before/after the value or in character space.

Some types of return addressing operators are:

%c >>>> for char values


%d >>>> for int values
%f >>>> for float values
%l >>>> for long values (independently incomplete )
%ld >>>> for long int values
%lf >>>> for long float values
%lld >>>> for very long int values
%lu >>>> for unsigned int values, %s >>>> for string values

Constants
 A constant is a value that can’t be changed in a program, for egs.: 15,32,’a’,3.4, “cool bhai” etc.
 There are 2 ways to define a constant in C programming:

1. Const. Keyword
2. #define processor

Eg : In const. keyword is a operand so as to set a variable such that it can’t be changed by revaluating the
variable
Like const int b = 7.90; printf(“b = %d”,b);
but if we put const int b = 7.90; and after it b = 9.87; then it shows error as variable is const.

same goes for #define operand as we put #include<define P = 3.14 then it will the same as const.(non
changeable variable).

Escape Sequence in C
 An escape sequence in C programming language is a sequence of characters
 It doesn’t represent itself when used inside string literal or character
 It is composed of 2 or more characters starting with backslash \
Eg : \n = new line

Types of escape sequence operands


(Operands with tick are supposed to be learnt)

\a = beep
\b = backspace
\n = new line
\t = Tab(horizontal)
\\ = backslash
\’ = single quote
\’’ = double quote

Comments

This is set of operands which are used to add comments in the code so as to make it more easier to
comprehend and organised etc.
Single line - // Rajat is a Predator of “Gods”
Double line - /* ……..............
…………………
………………… */

Multiplication challenge : Create a program which can print the multiplication


table of any int no. upto 10 in the format eg : 6 x 1 = 6 ….
6 x 10 = 60
Code:

Video 4

If – else statement
It is used to perform operations based on some conditions
Types of statements

 If statement
 If else statement
 If – else if ladder
 Nested if
Eg :
Syntax(If): Syntax(If else):
if(a > 3) if(a>3){ print(“Rajat has great virility”); }
{ else { print(“error”); }
print(“Rajat has great virility”);
}
Flowchart to demonstrate the working of if code:

Start

Conditions No result
FALSE

TRUE

Print If Statement After If Code

Flowchart to demonstrate the working of If – else:

Start
Print Else statement
Else

If

After If – Else statement


Print If statement

Flowchart for If – else – if ladder:

Syntax:
if(cond.1){
code 1;
}
else if(cond.2){
code 2;
}
…….
Else{
code n;
}
Video 5
Switch Case Control
Switch case control is a programming tool used to showcase a specific output for a specific input much like in
case of if – else statement

Syntax : eg – int a = 3
switch(a){
case 3:
printf(“value of a = 3”);
break; This operand is used to stop the program progress right away if case 3 is satisfied
case 4:
printf(“value of a = 4”);
default:
printf(“value of a is not 3 or 4”);
}
Rules to follow:

 Switch expression must only be an int or char (no float allowed)


 Case value must be an int or char (no float allowed)
 Case must come inside the switch
 Break operand is not necessary always

Flowchart for switch case control

Start
Match
Unmatched

Match

Match

Match

Quiz: To create a program which shows what the reward you get for passing or
failing maths/science exam (describing what you get if you pass both, pass
in either one or pass in none)
Video 6
Loops
These are a set of instructions which run a program to execute a repetitive task in a sequence.
Eg: to print no. from 1 to 100 etc.

Advantages of using Loop:

 Code reusability
 Saves time
 Traversing

Basic Idea of loop: index = 0


Loop starts {
printf(index);
[Link]
} loop ends [until condition is satisfied]

Basic Syntax of loop: int i = 0, until i<10 loop works

Loop starts ------ check the condition ----False--- exit the loop
|----True-----> execute the loop: i = i + 1; (If the cond. i < 10 is
fulfilled the code continuous to work but when i = 10 the code stops working and you exit the loop)
Types of Loops:

 Do while loop
 While loop
 For loop

Do – while loop:

Used for the basic task of running a program to execute a set of repetitive tasks.
Note : Do while loop needs to be executed to repeat a particular operation at least once

Syntax: Sample: Actual: int i = 0


do{ do{
// code to be executed i = i + 1;
}while(cond.) printf(“%d”,i);}while(i<10)

Result : i = i +1 increases the value of i by 1 in loop and prints the the next code, this works in loop till i<10
cond. seizes to be satisfied by i.
Output: 1
2
3 …..
10

While loop:

Used for the basic task of running a program to execute a set of repetitive tasks.

Syntax: Sample: Actual: int i = 0


while(cond.){ while(i<10){
// code to be executed i = i + 1;
}; printf(“%d”,i);};

Result : i = i + 1 increases the value of i by 1 in loop and prints the next code, this works in loop till i<10 cond.
seizes to be satisfied by i.
Output: 1
2
3 …..
10

For loop:

 For loop is used to iterate the statements or a part of the program several times
 It is used to traverse the data structures like the arrays and linked lists
 It has a different syntax than the do-while and the while loops
Syntax: sample actual
int n
for(expression 1, expression 2, expression 3) for(n = 0; n<10; n++)
{ // code to be executed;}; {printf(“%d”,n);}

Result: n++ increases the value of n by 1 in loop and prints the next code, this works in loop till n<10 cond.
seizes to be satisfied by n.
Output: 1
2
3 …..
10

Properties for expression 1:

 The expression 1 represents the initialization of the loop variable


 We can initialize more than one variable in expression 1
 Expression 1 is optional
Eg: like n = 0; or with two variables n = 0, j = 0

Eg: these cond. are like n < 100; j < 10 && l <10 etc
Note: expression2 is optional bcoz but in that cond. we use break;
Till the last cond. is not satisfied loop will continue to run.
Properties for expression 3:

 Expression 3 is used to update the loop variable


 We can update more than one variable at the same time.
 Expression 3 is optional (operands n++, i++ etc can be even written in code executing data area)

Video 7

Break Statements:

 Used to bring the program control out of the loop


 Break statements are generally used inside loops or switch statements
 Break statements can be used with
1. Loops
2. Switch Statement expression
Eg: loop – for(n = 0;n<10;n++)
{
printf(“%d\n Enter your name= “,n);
scanf(“%d”,&name);
if(name == simon){ printf(“Sorry but you can’t enter India”);}
};
Continue Statements:]

Eg: for(n = 0; n<10; n++)


printf(“Pt kolo %d Enter your name= ”,n);
scanf(“%d”,&name);
if(name == simon){
continue;}
printf(Get your hairy a*s out of India);

Video 8

Goto statements:
These statements are used to direct the control of program from one part to the other part where it is
supposed to be directed (just like teleporting from one place to another place).

Eg: for(int n = 0; n<9; n++) OR for(int n = 0; n<9; n++)


{ printf(“%d”,n); { printf(“%d”,n);
goto end; goto label;
} }
end: label:

Typecasting:

When you want to display execution of an arithmetic in an int variable it will always show the result of the
operation without any decimals but if you want to still show the execution in float then you have to use
typecasting

Typecasting – (int), (float) etc for arithmetic operations


Eg: int a = 3 , float b = 54/5 printf(“print 54/5 = %f”,b); ans will be 10.0000 as calculation happened as in data
type in but if you put float b = (float) 54/5 then result will be 10.8000 as the arithmetic calculation part
happened now in float data type form.

Video 9

Functions:

Eg: int (function type_name), etc


Printf();printf();, scanf();, etc int rajat(), int bhai etc

Eg: int printscape() -- with return


void printscape() - without return

Video 10
Recursive Function

In simple words when a function is used repeatedly in a single operation so as to get the desired output in a
single go then that function is known as recursive function.

Eg: factorial
Flowchart for execution

Execution:

Video 10
Arrays

Array – collection of items -> eg: a[100], b[500]


Pointers - *a, contiguous means all the diff. data are stored separately in the same array

For 56 students in an array you don’t need to make individual variables for each instead you can make a single
array of 56 storing capacity of same data type and then use the array to store all those names of pupils

Array makes content access really fast and convenient. Suppose a student is written in the 13 no. in the array
then that name can be used by that variable name – a[13]
Wastage of space happens when an array is given 1000 storage but the data input only consumes 300 space
Exercise 3: Fibonacci Series

Video 11
Pointers:

Eg: int *a, char *a etc

To assign pointer int *a = &b so as to store address of b in pointer a


Note: & and * have different operations as operators
& - it means it is the address of a variable eg – a = 7 then &a = the address stored in a variable which is 7, it
as a dual function of directing a data to store as address in a variable in case of input and then also store
the address of a in pointer

*- it is put in front of a var of any data type to declare it as a pointer eg: *a, *b etc. it also prints the address
towards which it is pointed to eg: *a = &b here if you print only a vlue of a prints but if you print *a,
address of b gets printed

You might also like