C+for+beginners (2).pdf hsudiksbdjdidkdnjd

itzvenkatesh21 8 views 48 slides May 01, 2024
Slide 1
Slide 1 of 48
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48

About This Presentation

Hsjjdjrn


Slide Content

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C for Beginners

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Agenda
•Introduction to C
•Variables in C
•Datatypes in C
•Input / Output in C
•Operators in C
•C Control Statements
•Arrays in C
•Functions in C
•Strings in C
•Structures and Union
•Pointers in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
•CwasfirstintroducedbyDennisRitchie.
•Cisaprocedure-orientedlanguage.
•Cisawidelyusedandpopularprogramminglanguagefordevelopingsystemapplicationsoftware.
•Itcanbecalledasamid-levelprogramminglanguage.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Features:
Some of the features of C Programming are:-
•Easy to learn
•Structured Programming language
•Machine Independent or Portable
•Powerful
•Used in low-level as well as high level applications

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Basic block of a C Program
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Preproccesor directives
Global declarations;
void main()
{
local declarations;
statement 1;
statement 2;
:
statement n;
}
User defined functions

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Comments
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
/* This is my First C Program
Author name: */
# include<stdio.h>
#include<conio.h>
void main()
{
printf(“C Programming”); // Single line Comment
getch();
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Tokens
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Ctokensaresmallestindividualunitsinaprogram.
●Identifiers-userdefinednames/abbreviations
●Keywords-wordswhicharespecifictoClanguage
●Constants-valueswhichdon'tchange
●Strings-sequenceofcharacters
●Operators-actonoperandsandgeneratesoutput
●SpecialSymbols-usedforpreprocessordirectives(#)

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables in C
WhatisaVariable?
•It’sanamegiventoamemorylocationwherethevalueisnotfixed,itcanchangewiththecourseof
time.Ex:intnum;
Syntax:
datatypevariable_name;
Assignment:
variable_name=value

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
ScopeofaVariable
•Scopegenerallyspeaksaboutthevisibilityofvariables.
-localvariableandglobalvariable
Local Global
Variables in C
void main ()
{
introllno= 10;
}
inty = 50;
void main ()
{
intx = 100;
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types in C
Data Types
Primitive Data type Derived Data type User Defined type
Boolean
Character
Integer
Float
Double
Void
Array
Pointer
References
Structure
Union
Enum

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Typecasting
•C allows for conversions between the basic data types
-Implicit and Explicit
Data Types in C
intnum= 5 + 13.75; // num= 18
float a = 5.25;
intb = (int) a; // b = 5
Implicit conversion Explicit conversion

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Input / Output in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Input / Output in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
InputandOutputfunctionsinCare:-
•printf()andscanf()
•gets()andputs()
•getchar()andputchar()

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Input / Output in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
FormatSpecifiers:-
Theformatspecifierswillinformthescanf()function,whatkindofinputisbeingfedthroughinput
device.Italsotellsprintf()functionwhattypeofdatahastobeprintedontheoutputdevice.
•%d –Integer
•%c –Character
•%s –String
•%f -Float

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
•Cprimarilysupports5typesofoperators
Arithmetic Relational Bitwise
Logical Assignment

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
ArithmeticOperators:-
•Usedformathematicaloperations
•Wehaveunaryandbinaryoperators
•Unary–(++,--)
•Binary–(+,-,*,/,%)
RelationalOperators:-
•Usedtocomparethevaluesoftwooperands
•Someofthemare(<,<=,>=,==)
•Wecanalsocheckforequalityofoperands,whetheranoperandisgreaterorsmallerthanthe
other.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
LogicalOperators:
•TocombineoneormultipleconditionswecanuseLogicaloperators.
•LogicaloperatorsalwaysgiveusaBooleanvalue,i.e.trueorfalse
•Someofthemare(&&-AND,||-OR,!–NOT)
BitwiseOperators:-
•Wecanperformbit-leveloperationsontheoperands.
•Someofthemare(&-bitwiseAND,|-bitwiseOR)
•Itconvertsthemtobit-levelfirstandthenperformstheoperations.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
AssignmentOperator:-
•Itisusedtoassignavaluetoagivenvariable.
•Thevalueontherightsideoftheassignmentoperatorisassignedtooperandatleftside.
•Wealsohavesomeshorthandassignmentoperators(+=,-=,/=).
.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
ConditionalStatements:-
•Alsoknownasselectionstatements.
•Basedontheconditionsspecifiedwecanmakesomedecisions.
•AnytimeweexecuteconditionalstatementswegetaBooleanvalueinreturn.
•Iftheconditionexecutedreturnsatruevalue,asetofstatementsareexecutedelseanothersetof
statementsareexecuted.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Theifstatement:-
•Selectionandexecutionofstatementsbasedonagivenconditionisdonebytheifstatement.
Syntax:
if(condition)
{
statement1;
statement2;
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Theif-elsestatement:-
•Dependingontheresultofthecondition,theif-elsestatementexecutesoneofthetwopotential
statements.
Syntax:
if(condition)
{
statement1; //ifblock
statement2;
}
else
{
statement3: //elseblock
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
TheNestedif-elsestatement:-
•Anestedif-elsecontainsoneormoreif-elsestatements.
if(condition1)
{
statement1;
if(condition2)
statement2;
else
statement3;
}
else
{
statement3:
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Loops:-
•Loopsareusedtore-executeapartofacodeagivennumberoftimes,dependinguponthe
conditionspecified.
Entrycontrolled:-
•Theconditionischeckedeach-timebeforeenteringtheloop.Iftheconditionissatisfiedthenonly
theloopbodygetsexecuted.Theloopbodydoesnotgetexecutediftheconditionisfalseinthe
firstiteration.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Entrycontrolledloopsare:-
1)Forloop
Syntax:
for(i=0;i<n;i++){
//dosomething
}
2)WhileLoop
Syntax:
while(conditionisTrue){
//dosomething
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Switchcase:-
●Theswitchstatementworksasamultiwaybranchstatement.
●It’samodifiedformofif-else.
●It’sacommonalternativetotheifstatementwhenyouwanttogetmultipleresults.
●Defaultconditiongetsexecutedwhennoconditionsaremet.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Switchcase:-
#include<stdio.h>
#include<conio.h>
void main(){
intn= 1;
switch (n)
{
case 1: Statement 1;
break;
case 2: Statement 2;
break;
default: Statement;
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
●Anarrayisacollectionofelementswherethedatatypesofalltheelementsaresame.
●Anarraystoresalltheelementsincontiguousmemorylocations.
●Everyelementinanarrayhasaspecifiedindex.
●SizeofanarrayinCisfixedatthetimeofdefinition
WehavetwotypesinArrays:
Singledimensional
Multi-dimensional
1 x 3 Array
2x 3 Array

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Definition:
AnarraycanbedefinedinthefollowingmannerinC:
Data_typename_of_array>[<size_of_array>]
Note:Sizeofanarrayshouldalwaysbeanintegeritcannotbearealvalue.
Example:
inta[10]; //singledimensionalarray
floatb[3][3];//float-dimensionalarray

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Arrayinitialization:
1)StaticInitialization
inta[10]={0,1,2,3,4,5,6}
charc[5]={‘h’,’e’,’l’,’l’,’o’}
2)DynamicInitialization
inta[10]; //arrayiscreatedwithgarbagevalue
for(inti=0;i<10;i++)
scanf(“%d”,&a[i]);

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functions in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functions in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
●Functionsareblocksofcodewhichareusedtoperformspecifictasks.
●InCafunctionneedstobedeclaredbeforeit’sused.
●Functionshaveafunctiondefinition,functionbodyandareturntype.
●Functionswithreturntypeexceptvoidneedstoreturnavalueattheend.
●Functionswithreturntypevoiddonotreturnanyvalue.
●Arecursivefunctioncancallitselfduringthecourseofexecutionofaprogram.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functions in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
AFunctionexample:
intsum(inta,intb)
{
returna+b;
}
voidmain()
{
printf(“%d”,sum(5,10));
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Strings in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Strings in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
●Stringsareusedtostoresequenceofcharactersasinformation.
●string.hheaderfileprovidessomebuilt-instringmanipulationfunctions.
Stringscanbeinitializedinfollowingways:-
chara[5]={‘H’,’o’,’l’,’a’};
chara[]=“Hola”;

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Strings in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Stringfunctionsareaccessiblebyimporting<string.h>.
●strlen()Findlengthofstring
●strcpy()Copyonestringtoother
●strcat()Jointwostrings
●strcmp()Comparetwostrings
●strlwr()Convertsstringtolowercase
●strupr()Convertsstringtouppercase

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Structures and Union

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Structures and Union
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
●Structuresprovidesawayforustocreateourowndatatypecalled“user-defineddatatype”
●Assumethatwehavetostorethedetailsofastudent,sowecanusestructuretostoreitasbelow:
structstudent
{
charname[10];
introllno;
floatcgpa;
};
name
rollno
cgpa
student
Structure
allocates space
for each variable
separately

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Structures and Union
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
●Unionalsoprovidesawayforustocreateourowndatatypecalled“user-defineddatatype”.
●Hereeachvariablesharesthememorycollectively.
unionstudent
{
charname[10];
introllno;
floatcgpa;
};
name
rollno
cgpa
student
Union shares the
memory
allocated for
variables
Memory

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointers in C

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointers in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
●Pointersarevariableswhichstoretheaddressofavariable.
●Theyhavedatatypejustlikevariables,forexampleanintegertypepointercanholdtheaddressof
anintegervariableandancharactertypepointercanholdtheaddressofcharvariable.
Example:
inta=20;
int*p=&a; 1001 2063
20 1001
a *p

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointers in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
ExampleofPointer:
#include<stdio.h>
voidmain()
{
inty=5;
int*p;
p=&y;
printf("Addressofy:%x\n",&y);
printf("Contentofp:%x\n",p);
printf("Contentof*p:%d\n",*p);
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Summary
•WesawanintroductiontoC,featuresavailableinCandtokensavailableinC.
•Then,westartedwithvariables,howtodeclareandusethem.
•Next,wecameacross,whatarethedatatypeswhichwecanusehereinClanguage.
•Thenwewentthroughtheinputandoutputfunctions,howcanweprocesstheinputanddisplay
results.
•OperatorsavailableinCthroughwhichwecanperformvariousoperationsonourdata.
•Variouscontrolsstatementssuchasif,if..else,loopsandswitchcase.
•Next,weintroducedarraysinC,howtodeclareandusethemwithexamples.
•Nextup,wesawwhatfunctionsare,howtousethemintheprogramandrecursion.
•LaterwecameacrossStrings,howtodeclareandbuilt-infunctionsavailableinstring.h.
•WeintroducedStructuresandUnionwithanexampleforeach.
•Abriefintroductiontopointers,howtodeclareandusethemwithexample.