0% found this document useful (0 votes)
12 views13 pages

C Notes

This document provides a comprehensive overview of C programming concepts, including compilation, data types, operators, control structures, loops, arrays, strings, functions, and pointers. It explains syntax, usage, and examples for various programming constructs, as well as common functions from standard libraries. The content serves as a reference for both beginners and intermediate programmers in understanding and applying C language features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

C Notes

This document provides a comprehensive overview of C programming concepts, including compilation, data types, operators, control structures, loops, arrays, strings, functions, and pointers. It explains syntax, usage, and examples for various programming constructs, as well as common functions from standard libraries. The content serves as a reference for both beginners and intermediate programmers in understanding and applying C language features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

gcc -o fileName FileName.c it makes a exe file of the .

c file and we can run it


using next command

./fileName just run it and it will give the output

include<math.h> % operator can be only applied to int not to


float
(mathematical header file)

limits.h> functions - INT_MIN and INT_MAX they are values of the smallest and
largest value for a number in int datatype

if we have to print % in the print as a


normal character always use %% with any number or
pow(4,5)=4s power 5 with just itself

for only 2/3 deciaml point place in


float (%.2f)
sqrt(49)=square root of 49

we can take many values in at a same time in the scanf if we include at the same
line %d and &r etc after putting comma

for swaping use a different variable

// single line comments just for convience won't count in compiler ctrl+/ for
bulk quantities

heirarchy of operators:- make groups around / and move from left to right and
remember the concept of .0 in the
calculations

ASCII values(skai values):-A=65 B=66.............. american standard code for


info interchange
a=97 b=98 .............
65 is ascii value of A

data type :- short value only from -32768 to 32767 short =


2bytes=16bits
long 32768 nbits=2^n
numbers can be stored
long long
int =
4bytes=32bits
long
long8bytes=64bits so 2^64 numbers can be stored

char=1bytes=8bits

-8 =8 its
always the same there is not any different
if-else
relational operators

---------------

if(condition)
< greater than {
> lesser than
--------------------------
>= greater than or equal }
<= lesser than or equal else{
== equals to
----------------------------
!= not equals to }

-----------------------------------------------------------------------------------
---------------------

Logical operators
&& , || (or or)

syntax if ( n>99 && n<1000)

if ( n==0 || n>200)

its not necessary that we have to take only 2 conditions in operators we can take
any number of them

nested if else ( ek if else k andar bhi or if else)

while giving many operators always remember heirarchy


&&>>>|| (remember either divisible by 5 or 3 but not divisible by 15 question)

to overcome it use brackets

-------------------------------

if we write only one line after if else it will automatically use that without
using {}

else if it can be used with 3rd one in if else series like

-------------------------------------
if(cond){
}
else if (cond){
}
else(cond){
}

----------------------------------------------
! not logical operator it reverts the true and false in each case

x=6;
if(!x<10)
its a false because of ! operator
---------------------------------------------------------------------------

if we write else if in the statement then if it worked then no other statement will
work
but if we continue writing just if statements then it will come to other statement
even after completing
first one\

so whenever we need to give range we can just use else if without using the &&
statements

-----------------------------------------------------------------------------------
--------------------------------

Ternary operator

exp1?exp2:exp3
only used for single if else not for nested
ex:-
a%2==0?printf("even"):printf("odd"):

-----------------------------------------------------------

3 and 3.0 are cosidered same by c lang.

---------------------------------------------------------

Bolean data type ( it is intialized like )

bool x it has only two values either true or false

true is given by 1 and false by 0

int main (){


int x=3;
printf("%d",x=10"); output = 10

int main (){


int x=3;
printf("%d",x==10"); output = 0 as it is false and we are using
a integer and bolean data
type specifies false as 0

int main (){


int x=3;
printf("%d",x>10"); output=1 as true is denoted by 1

----------------------------------------------------------------------------------

********** in the if (______) statements we dont have to write a condition to


work we just need to write any expression either
character or number or just simple x=14 etc everything will work other
than 0 straight because 0 significe false statements
we can do anything in if ( ) and it will still work

Switch statement

switch(variable)
{
case val1: remember here to give the space after case and
before val1 like case val1
-------------------------------
break;
case val2:
----------------------
break:
case va3:
---------------------------------
break;
default:
------------------------
//break not neccessary here
}

without break it will contnue even if dont have the case matching

for using all case value without repiting same things at our code we can just write
case 1: case 2:... and so on
after that just use the command and we will get the output for all cases without
repition of code\

-----------------------------------------------------------------------------------
------------------------------

assignment opertors unary operators


+= a+= a=a+10 are of two types
-= 1: ++ (increment)
*= A): pre incre.++i (if we put a=+
+i it means first it will be go to 6 then a wil also be assigned to 6 )
/= B): post incr. i++ (in this
first a will be assigned 5 later 6 will be the value of i)
%= 2: -- (decrement)
A): pre incre.--i
B): post incr. i--

-----------------------------------------------------------------------------------
-------------------------------------------------------

*******LOOPS************* for repeatations iteraive statements


three types of loops
1- for loop
2-while loop
3-do while loop

1- for loop syntax


for (initialization;condition;increment)

{
------------------------------------- in loop if we want to use other
variable than initialization as a condition we can use it
}

scope of variable :- if we take an variable in any condition that variable will


only be valid in that condition only, out of that statement
it will not be considered
-- so we can give integer in direct for loop like for(int
i=1;i/<=10;i++)

2 --while loop syntax


initialization without increment it will became a
infinte loop
while(condition){
-----------------
increment }

when we print i outside the loop it will give value 6 if i=1 is initializaion and
i<=5 it the condition because it will still go on for the 5 times and
in 6 one escape while loop

fibonacci series where last two terms add make 3rd

armstrong numbers 153


1^3+5^3+3^3=153

perfect numbers ----- numbers whose sums of factors is equal to the number are
called perfect numbers
******
break; statement it is used to terminate the statement which means it will stop the
command right there
continue; statement it just lefts the part and continues over it it leaves the
whole loop and moves on wheteher anything is left or not in loop

3-) Do while loop:- syntax

initialization
do{
============
========================
increment}
while(condition);

if we want to take char as input after taking the valus of float or int we have to
use fflush(stdin); to refresh the values

*** nested loops***------------


loop within another loop
if we want to have factorial of numbers from 1 to 7 then we will

\t gives space between the different outputs of printf

*****pattern printing******-------------------------
*
**
***
**** nested for loop with variables

*
**
*** triple nested for loops
-----------------------------------------------------------------------------------
-----------------------------------------------------------
arrays

datatype array[size];
int a[5];
int a[5]={34,45,56,66,33} = providing the values in the array

it is collection of same type of data types

it gives index values to the variable in the name of a0 to last number -1

if we give values to the assigned arrays it stores in there

we cant take same variable as array like a[5] we cant take another variable as a
neither in any other data type

-----------------------------------------------------------------------------------
---------------------------------------------

printing ascii values in c with int or characters with ascii values


either directly put in printf %c in place of int value ex
int a=65;
printf("%c",a);

or put any other variable like int a=65;


char ch=(int)a;

-----------------------------------------------------------------------------------
-----------------------------------------

in pattern printing try finding out the reltion between the number of rows or
colums or x axis or y axis

pp always remember using (ap)-arithematic progression in any type of special


printing in the line like 1 in 1st line 3 in 2nd line 5 in 3rd line

try to make code as small as possible by making use of different variables and
condition statements

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------

2D arrays ] here data stores in the form of rows x columns (if we want to store
further data in the array) or matrix form can be said

syntax - datatype variable[row][col]


int a[3][2]
int a[2][1]={{1,2},{3,4}}:

if we write 2 rows and 3 colums so it will make a matrix where we have 2 rows and
one column but they are named as 0th row and 1st row
same with column case it starts with 0 and goes till no of rows minus 1

-----------------------------------------------------------------------------------
------------------------------------------------------------------

STRING is a collection of arrays


char str[6]={'d','u','c','a','t','\0'} \\null character zero 0 compiler
automatically gives this value at the end of each string we dont have to write it
each time it comes automatically

char str[5]={"john"}

format specifer is %s related to strings everywhere

we can take the any number of string input as we want there is no restriction in it

char s[30];
scanf only takes the value till the space is used so to overcome either we use
scanf("%[^\n]s",&s);
it tells read everything until enter is pressed so now it will even read the spaces

or we use gets(s) and its printf like pair is with puts and in puts it
automatically goes to next line in the puts we dont have to give \n
in gets and puts we dont need format specifiers it directly works like gets(a) and
puts(a)

we have to either initialize the string value at the start of take input from the
user we cant do things like we did with the integer data types
ex:- char s[30]={"john"}✅

char s[30]
s="john";❌

functions used (in <string.h> header file) :-- 1.)


(strlen) to count the length
of the string even counts spaces and special characters

2.)
strcpy(string in which data will copy , data which will
be copied) it always works on two parameters

we can even copy any thing in the string either a word or

whole string.

3.)
strcat(s1,s2) concatenate two strings

4,)
strcmp compares a string with another and if both are
same it gives a int digit 0 and if not then other than 0 it gets the
differences of ascii value so if its zero it means it is same

A-Z=65-90 a-z=97-122 0-9=48-57 space bar = 32


enter = 10

-----------------------------------------------------------------------------------
---------------------------------------

Function Name Function Description

strlen() Returns the length of


the string.

strcpy() Copy one string to


another.

strncpy() Copy first n


characters of one string to another.

strcat() Concatenates two


strings.

strncat() Concatenates first n


characters of one string to another.

strcmp() Compares two strings.

strncmp() Compares first n


characters of two strings.

strchr() Find the first


occurrence of the given character in the string.

strrchr() Finds the last


occurrence of the given characters in the string.

strstr() Find the given


substring in the string.

strcspn() Returns the span of


the source string not containing any character of the given string.

strspn() Returns the span of


the source string containing only the characters of the given string.

strpbrk() Finds the first


occurrence of any of the characters of the given string in the source string.

strtok() Split the given


string into tokens based on some character as a delimiter.

strcoll() Compares two strings


that are passed.

memset() Initialize a block of


memory with the given character.

memcmp() Compares two blocks


of memory.

memcpy() Copy two blocks of


memory.

memmove() Moves two blocks of


memory.

memchr() Finds the given


character in the block of memory.

-----------------------------------------------------------------------------------
-----------------------------------------------------------------

Functions \\

case 1- function with no return type and no parameters


case 2- function wiht no return type and parameters
case 3- function with return type but no parameters
case 4- function with return type and parameters

local variables- variables which are declared in any function are local variable of
that function

global variable which are not declared in any function and they are accessible in
all the functions

Whenever there is a necessity to bring a function which we have to repeat again and
again or just for any pre defined action or task we make any fuction or
use already made function

it is different from loop as we dont have to make complete syntax here every time
we have to make it only one time.

code always starts with main function in compiler


whenever we do parenthesis it means it is a function()
we have to use void function() to make our own function and whenever we use that
function it is called calling a funtion

after calling a function it goes to void from main and read it and execute
and whenever it sees the return in it that means close there like break;

BASIC SYNTAX OF A FUNCTION


fun(){ we can also write many things in front of it
like void,int etc . later on that
-----
}

the called function must always be above of the function from where we are caliing
functions

main function can be only used once in code

about function------)))
int add(int a,int b){ int a and int b are called arguments / parameters

return a+b;
}
same named variables like a in the different functions are not same
#include<stdio.h> after
taking input using scanf in main funtion in the code
int add(int a,int b){ that
gets stores in the respective data type variables
return a+b; then it
goes to add fuction
} values
of variables is transfered to the variables in the add function
int main(){ and
returns with the command of their addition
int a,b,c;
printf("enter the number :"); add
function is just the name and it starts after using parenthesis
scanf("%d",&a);
printf("enter the number :");
scanf("%d",&b); if we
did only two functions in the defined function it will only take two
int d=add(a,b); values
in input
}

===================================================================================
================================

RETURN TYPES
if we use int at starting of function we have to return a numerical value and
respectively with float and char data type
and in void function we dont have to return anything we just have to write return
statement

functions prefined in c language are called library functions in their respective


header files
whichever header file we will include it will go through that file and find that
library function
-----------------------------------------------------------------------------------
--------------------------------------------------
function prototype

when our called function is not above the main function we use prototype to read it
anywhere it is
by using void fnctionname(); in the main function this is called function
prototype.

-----------------------------------------------------------------------------------
------------------------------------------------------------------
actual and formal parameters when we pass by value the name of the variables used
like a,b,c etc is called formal parameter while the value stored in them is known
as actual parameter

-----------------------------------------------------------------------------------
---------------------------------------------------------------------

POINTERS
---------

%u format specifier can be also used for address

* value at operator

*p increment in pointer means if it is storing a int datatype at 5602 it will


increase by 4 bytes and it will become 5606 address in char 1 byte and so on

syntax- datatype(of whose address we are storing ex char for a char variable and
int for int var) *ptername

-stores the address of any variable


-if we use & like &a it means the address of a in the ram
-%p is the format specifier for the printing of address of a variable
-everytime we run a code for address of any variable it will give different value
every time as whenever compiler runs it allocates the address that moment
-declaration of a pointer

int* x=&a; or int *x=&a;

even pointer also have some address it just it just stores the address of other
variable

for accessing the data of the pointer of the specific address we use "%d",*x;
this sgnify that we have to print a int value which is pointing at the address of
*x

if we declare a pointer we can assign it any address later on also we dont need to
give it right there
ex int *x;
x=&a;
we can even change the value of variable using pointers *x=10; now the a variable
will have the value of 10 instead of its previous value stored in it.
-----------------------------------------------------------------------------------
----------------------------------------------------------------------
Pass by reference and pass by value

pass by value is the normal way when the main function func(a,b) is passed to the
funct(x,y) but they both are different memory allocation so to overcome this we use
pointers that uses the place of that memory allocation address

in pass by refrence we pass the address of formal parameters basically we pass


actual parameters to the functions

pass by refrence
--- pass the actual parameter(pass the address of variables)
---- parameters of the function made must be made pointers to intake the address of
the main function parameters
---- make code using *y pointers and make a change in them and then in main
function print the original variable

-----------------------------------------------------------------------------------
----------------------------------------------------------------

DOUBLE POINTERS
when we point a pointer using a different pointer
we use int **x and assign it a pointer
we can make any number time for a pointer like triple quad etc
-----------------------------------------------------------------------------------
--------------------------------------------------------

Array using pointers\\

when we make an array it makes the addresses at consecutive differences of its


blocks so we can give a pointer address of first array element and in loop
increment of pointer can be done so it will take input for each array element

we dont even need to give pointer address of first array element as it


automatically assign it that to the first element whenever it is called
it is known as base address

passing array as function argument we have to make two paramters in both function
one made and one called int the made function we have to add [] in the datatype and
make another paramter for the size of that array like int a[],int b and in main
function we have to do a
**array just pass normally like other intergers just like normal parameters
-----------------------------------------------------------------------------------
---------------------------------------------
RECURSION- a function that repeats/ calls itself

it can be used as a loop

recurrence relation - when a result is easily being calculated with the help of any
other result it is known as recurrence relation

base case- in a recursion we have to make a base case to return our function at sum
point else it will keep on going

tree diagram the dry run type in elaborated way is called tree diagram
it is implemented as stack implementation
filo or lifo first in last out or last in first out

when we put unstack something we put upper part in the lowest part and above it all
the other lower parts of the stacked part

jo function sabse last mai call hoga wo sabse pehle return hoga ex factorial using
recursion

multiple calling of a function in a function ex function(m)=function(m-


1)+function(m-2)

pre in post 2-1-1-1-2-1-1-1-2 using eulers diagram when we cross the part outside
it is known as the pre part and when we came across below its in part and when we
go right side of the called function it is called the post part

-----------------------------------------------------------------------------------
------------------------------------------------------

You might also like