SlideShare une entreprise Scribd logo
1  sur  111
Télécharger pour lire hors ligne
Variables
Variable is the name of a memory
location which stores some data.
25 S
a b
Memory
Variables
Rules
a. Variables are case sensitive
b. 1st character is alphabet or '_'
c. no comma/blank space
d. No other symbol other than '_'
Variables
Data Types
Constants
Values that don't change(fixed)
Types
Integer
Constants
Character
Constants
Real
Constants
1, 2, 3, 0
, -1, -2 1.0, 2.0,
3.14, -24
'a', 'b', 'A',
'#', '&'
32 Keywords in C
Keywords
Reserved words that have special
meaning to the compiler
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Program Structure
#include<stdio.h>
int main() {
printf("Hello World");
return 0;
}
Comments
Single Line Multiple
Line
Lines that are not part of program
//
/*
*/
Output
printf(" Hello World ");
printf(" kuch bhi n");
new line
Output
printf(" age is %d ", age);
printf(" value of pi is %f ", pi);
printf(" star looks like this %c ", star);
CASES
integers
1.
2. real numbers
3. characters
Input
scanf(" %d ", &age);
Compilation
Hello.c C Compiler
A computer program that translates C code
into machine code
a.exe (windows)
a.out (linux & mac)
Instructions
These are statements in a Program
Types
Type Declaration
Instructions
Control
Instructions
Arithmetic
Instructions
Instructions
Type Declaration Instructions Declare var before using it
int a = 22;
int b = a;
int c = b + 1;
int d = 1, e;
VALID INVALID
int a = 22;
int b = a;
int c = b + 2;
int d = 2, e;
int a,b,c;
a = b = c = 1;
int a,b,c = 1;
Arithmetic Instructions
a + b
Operand 1 Operand 2
Operator
NOTE - single variable on the LHS
a = b + c
VALID INVALID
b + c = a
a = bc
a = b^c
a = b * c
a = b / c
NOTE - pow(x,y) for x to the power y
Arithmetic Instructions
Modular Operator %
Returns remainder for int
3 % 2 = 1
-3 % 2 = -1
Arithmetic Instructions
Arithmetic Instructions
Type Conversion
int int
op int
int float
op
op
float
float float float
Arithmetic Instructions
Operator Precedence
*, /, %
+, -
=
x = 4 + 9 * 10
x = 4 * 3 / 6 * 2
Arithmetic Instructions
Associativity (for same precedence)
Left to Right
x = 4 * 3 / 6 * 2
Instructions
Control Instructions
Used to determine flow of program
a. Sequence Control
b. Decision Control
d. Case Control
c. Loop Control
Operators
a. Arithmetic Operators
b. Relational Operators
d. Bitwise Operators
c. Logical Operators
e. Assignment Operators
f. Ternary Operator
Operators
==
>, >=
<, <=
!=
Relational Operators
Operators
&& AND
|| OR
! NOT
Logical Operators
Operator Precendence
Priority
1
2
3
4
5
6
7
8
Operator
!
*, /, %
+, -
<, <=, >, >=
==, !=
&&
||
=
Operators
=
+=
-=
Assignment Operators
*=
/=
%=
Conditional Statements
Types
if-else Switch
if-else
if(Condition) {
//do something if TRUE
}
else {
//do something if FALSE
}
Ele is optional block
can also work without {}
else if
if(Condition 1) {
//do something if TRUE
}
else if (Condition 2) {
//do something if 1st is FALSE & 2nd is TRUE
}
Conditional Operators
Condition ? doSomething if TRUE : doSomething if FALSE;
give 1 & 0 cases
Ternary
Conditional Operators
switch(number) {
case C1: //do something
break;
case C2 : //do something
break;
default : //do something
}
switch
Conditional Operators
a. Cases can be in any order
switch Properties
b. Nested switch (switch inside switch) are allowed
Loop Control Instructions
Types
for do while
while
To repeat some parts of the program
for Loop
for(initialisation; condition; updation) {
//do something
}
- Increment Operator
Special Things
- Decrement Operator
- Loop counter can be float
or even character
- Infinite Loop
while(condition) {
//do something
}
while Loop
do {
//do something
} while(condition);
do while Loop
exit the loop
break Statement
skip to next iteration
continue Statement
Nested Loops
for( .. ) {
for( .. ) {
}
}
Take
Argument
Do
Work
Return
Result
block of code that performs particular task
Functions
increase code reusability
it can be used multiple times
Function Prototype
Syntax
void printHello( );
> Tell the compiler
1
Function Definition
Syntax
printf("Hello");
void printHello() {
}
2
> Do the Work
Syntax
Function Call
int main() {
return 0;
}
printHello( );
3
> Use the Work
- Execution always starts from main
Properties
- A function gets called directly or indirectly from main
- There can be multiple functions in a program
Function Types
Library
function
User-
defined
Special functions
inbuilt in C
declared & defined by
programmer
scanf( ), printf( )
Passing Arguments
functions can take value & give some value
parameter return value
Passing Arguments
void printHello( );
void printTable(int n);
int sum(int a, int b);
Passing Arguments
functions can take value & give some value
parameter return value
Argument v/s Parameter
values that are
passed in
function call
values in function
declaration &
definition
used to send
value
used to receive
value
actual
parameter
formal
parameters
NOTE
a. Function can only return one value at a time
b. Changes to parameters in function don't change the values in
calling function.
Because a copy of argument is passed to the function
Recursion
When a function calls itself, it's called recursion
a. Anything that can be done with Iteration, can be done with
recursion and vice-versa.
Properties of Recursion
b. Recursion can sometimes give the most simple solution.
d. Iteration has infinite loop & Recursion has stack overflow
c. Base Case is the condition which stops recursion.
Pointers
A variable that stores the memory
address of another variable
22
age
Memory
2010
ptr
2010
2013
Syntax
int age = 22;
int *ptr = &age;
int _age = *ptr;
22
age
Memory
2010
ptr
2010
2013
Declaring Pointers
int *ptr;
char *ptr;
float *ptr;
Format Specifier
printf("%p", &age);
printf("%p", ptr);
printf("%p", &ptr);
Pointer to Pointer
A variable that stores the memory
address of another pointer
22
age
Memory
2010
ptr
2010
2013
pptr
2013
2012
Pointer to Pointer
Syntax
int **pptr;
char **pptr;
float **pptr;
Pointers in Function Call
Call by
Value
call by
Reference
We pass value of
variable as
argument
We pass address of
variable as
argument
Arrays
Collection of similar data types stored at
contiguous memory locations
Syntax
int marks[3];
char name[10];
float price[2];
Input & Output
scanf("%d", &marks[0]);
printf("%d", marks[0]);
Inititalization of Array
int marks[ ] = {97, 98, 89};
int marks[ 3 ] = {97, 98, 89};
Memory Reserved :
Pointer Arithmetic
Pointer can be incremented
& decremented
CASE 1
Pointer Arithmetic
CASE 2
CASE 3
Pointer Arithmetic
- We can also subtract one pointer from another
- We can also compare 2 pointers
Array is a Pointer
int *ptr = &arr[0];
int *ptr = arr;
Traverse an Array
int aadhar[10];
int *ptr = &aadhar[0];
Arrays as Function Argument
printNumbers(arr, n);
void printNumbers (int arr[ ], int n)
void printNumbers (int *arr, int n)
OR
//Function Declaration
//Function Call
Multidimensional Arrays
arr[0][0]
2 D Arrays
int arr[ ][ ] = { {1, 2}, {3, 4} }; //Declare
//Access
arr[0][1]
arr[1][0]
arr[1][1]
Strings
A character array terminated by a '0' (null character)
null character denotes string termination
EXAMPLE
char name[ ] = {'S', 'H', 'R', 'A', 'D', 'H', 'A','0'};
char class[ ] = {'A', 'P', 'N', 'A', ' ', 'C', 'O', 'L', 'L', 'E', 'G', 'E', '0'};
Initialising Strings
char name[ ] = {'S', 'H', 'R', 'A', 'D', 'H', 'A','0'};
char class[ ] = {'A', 'P', 'N', 'A', ' ', 'C', 'O', 'L', 'L', 'E', 'G', 'E', '0'};
char name[ ] = "SHRADHA";
char class[ ] = "APNA COLLEGE";
What Happens in Memory?
char name[ ] = {'S', 'H', 'R', 'A', 'D', 'H', 'A','0'};
char name[ ] = "SHRADHA";
2000
name
S H R A D H A 0
2001 2002 2003 2004 2005 2006 2007
String Format Specifier
"%s"
printf("%s", name);
char name[ ] = "Shradha";
IMPORTANT
scanf( ) cannot input multi-word strings with spaces
Here,
gets( ) & puts( ) come into picture
String Functions
gets(str)
input a string
(even multiword)
puts(str)
output a string
fgets( str, n, file)
Dangerous &
Outdated
stops when n-1
chars input or new
line is entered
String using Pointers
char *str = "Hello World";
Store string in memory & the assigned
address is stored in the char pointer 'str'
char *str = "Hello World";
char str[ ] = "Hello World";
//cannot be reinitialized
//can be reinitialized
Standard Library Functions
1 strlen(str)
count number of characters excluding '0'
<string.h>
Standard Library Functions
2 strcpy(newStr, oldStr)
copies value of old string to new string
<string.h>
Standard Library Functions
3 strcat(firstStr, secStr)
concatenates first string with second string
<string.h>
firstStr should be large
enough
Standard Library Functions
4 strcpm(firstStr, secStr)
Compares 2 strings & returns a value
<string.h>
0 -> string equal
positive -> first > second (ASCII)
negative -> first < second (ASCII)
Structures
a collection of values of different data types
EXAMPLE
name (String)
For a student store the following :
roll no (Integer)
cgpa (Float)
Syntax
struct student {
char name[100];
int roll;
float cgpa;
};
struct student s1;
s1.cgpa = 7.5;
Syntax
struct student {
char name[100];
int roll;
float cgpa;
}
Structures in Memory
struct student {
char name[100];
int roll;
float cgpa;
}
name
2010
cgpa
2114
2110
roll
structures are stored in contiguous memory locations
Benefits of using Structures
- Good data management/organization
- Saves us from creating too many variables
Array of Structures
struct student COE[100];
struct student ECE[100];
struct student IT[100];
IT[0].roll = 200;
ACCESS
IT[0].cgpa = 7.6;
Initializing Structures
struct student s1 = { "shradha", 1664, 7.9};
struct student s2 = { "rajat", 1552, 8.3};
struct student s3 = { 0 };
Pointers to Structures
struct student *ptr;
ptr =&s1;
struct student s1;
Arrow Operator
(*ptr).code ptr->code
Passing structure to function
void printInfo(struct student s1);
//Function Prototype
typedef Keyword
used to create alias for data types
coe student1;
File IO
RAM Hard Disk
File IO
- RAM is volatile
FILE - container in a storage device to store data
- Contents are lost when program terminates
- Files are used to persist the data
Operation on Files
Create a File
Open a File
Close a File
Read from a File
Write in a File
Types of Files
Text Files
.exe, .mp3, .jpg
Binary Files
textual data
.txt, .c
binary data
File Pointer
FILE is a (hidden)structure that needs to be created for opening a file
A FILE ptr that points to this structure & is used to
access the file.
FILE *fptr;
Opening a File
fptr = fopen("filename", mode);
FILE *fptr;
Closing a File
fclose(fptr);
File Opening Modes
open to read
"r"
open to read in binary
"rb"
open to write
"w"
open to write in binary
"wb"
open to append
"a"
BEST Practice
Check if a file exists before reading from it.
Reading from a file
fscanf(fptr, "%c", &ch);
char ch;
Writing to a file
fprintf(fptr, "%c", ch);
char ch = 'A';
Read & Write a char
fgetc(fptr)
fputc( 'A', fptr)
EOF (End Of File)
fgetc returns EOF to show that the file has ended
Dynamic Memory Allocation
It is a way to allocate memory to a data structure during
the runtime.
We need some functions to allocate
& free memory dynamically.
Functions for DMA
a. malloc( )
b. calloc( )
c. free( )
d. realloc( )
malloc( )
takes number of bytes to be allocated
& returns a pointer of type void
ptr = (*int) malloc(5 * sizeof(int));
memory allocation
calloc( )
initializes with 0
ptr = (*int) calloc(5, sizeof(int));
continuous allocation
free( )
We use it to free memory that is allocated
using malloc & calloc
free(ptr);
realloc( )
reallocate (increase or decrease) memory
using the same pointer & size.
ptr = realloc(ptr, newSize);

Contenu connexe

Similaire à The best every notes on c language is here check it out

presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
SandipPradhan23
 
Input output functions
Input output functionsInput output functions
Input output functions
hyderali123
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 

Similaire à The best every notes on c language is here check it out (20)

02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Session 4
Session 4Session 4
Session 4
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Python
PythonPython
Python
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
C
CC
C
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 

The best every notes on c language is here check it out