Chapter 1 Notes - Merged
Chapter 1 Notes - Merged
Memory
a b
25 S
Variables
Rules
c. no comma/blank space
Types
Integer Character
Constants Real Constants
1, 2, 3, 0
Constants 'a', 'b', 'A',
, -1, -2 1.0, 2.0, '#', '&'
3.14, -24
Keywords
Reserved words that have special
meaning to the compiler
32 Keywords in C
Keywords
auto double int struct
do if static while
#include<stdio.h>
int main() {
printf("Hello World");
return 0;
}
Comments
Lines that are not part of program
new line
printf(" kuch bhi \n");
Output
CASES
1. integers
printf(" age is %d ", age);
2. real numbers
printf(" value of pi is %f ", pi);
3. characters
printf(" star looks like this %c ", star);
Input
a.exe (windows)
Hello.c C Compiler
a.out (linux & mac)
Instructions
These are statements in a Program
Types
VALID INVALID
int a = 22; int a = 22;
int b = a; int b = a;
int c = b + 1; int c = b + 2;
int d = 1, e; int d = 2, e;
int a,b,c;
int a,b,c = 1;
a = b = c = 1;
Arithmetic Instructions
a+b
Operand 1 Operand 2
Operator
3%2=1
-3 % 2 = -1
Arithmetic Instructions
Type Conversion
*, /, % 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
c. Loop Control
d. Case Control
Operators
a. Arithmetic Operators
b. Relational Operators
c. Logical Operators
d. Bitwise Operators
e. Assignment Operators
f. Ternary Operator
Operators
Relational Operators
==
>, >=
<, <=
!=
Operators
Logical Operators
&& AND
|| OR
! NOT
Operator Precendence
Priority Operator
1 !
2 *, /, %
3 +, -
4 <, <=, >, >=
5 ==, !=
6 &&
7 ||
8 =
Operators
Assignment Operators
=
+=
-=
*=
/=
%=
Conditional Statements
Types
if-else Switch
if-else
if(Condition) {
//do something if TRUE
}
else {
//do something if FALSE
}
if(Condition 1) {
//do something if TRUE
}
else if (Condition 2) {
//do something if 1st is FALSE & 2nd is TRUE
}
Conditional Operators
Ternary
Condition ? doSomething if TRUE : doSomething if FALSE;
Memory
age ptr
22 2010
2010 2013
Syntax
int age = 22;
int *ptr = &age;
int _age = *ptr;
Memory
age ptr
22 2010
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
Memory
age pptr ptr
22 2013 2010
int **pptr;
char **pptr;
float **pptr;
Pointers in Function Call
Call by call by
Value Reference
char name[10];
float price[2];
Input & Output
scanf("%d", &marks[0]);
printf("%d", marks[0]);
Inititalization of Array
int marks[ ] = {97, 98, 89};
Memory Reserved :
Pointer Arithmetic
Pointer can be incremented
& decremented
CASE 1
Pointer Arithmetic
CASE 2
CASE 3
Pointer Arithmetic
//Function Call
printNumbers(arr, n);
Multidimensional Arrays
2 D Arrays
int arr[ ][ ] = { {1, 2}, {3, 4} }; //Declare
//Access
arr[0][0]
arr[0][1]
arr[1][0]
arr[1][1]
Strings
EXAMPLE
char class[ ] = {'A', 'P', 'N', 'A', ' ', 'C', 'O', 'L', 'L', 'E', 'G', 'E', '\0'};
Initialising Strings
char class[ ] = {'A', 'P', 'N', 'A', ' ', 'C', 'O', 'L', 'L', 'E', 'G', 'E', '\0'};
char class[ ] = "APNA COLLEGE";
What Happens in Memory?
char name[ ] = {'S', 'H', 'R', 'A', 'D', 'H', 'A','\0'};
char name[ ] = "SHRADHA";
name
S H R A D H A \0
2000 2001 2002 2003 2004 2005 2006 2007
String Format Specifier
"%s"
Here,
gets( ) & puts( ) come into picture
String Functions
Dangerous &
gets(str) Outdated puts(str)
input a string output a string
(even multiword)
1 strlen(str)
count number of characters excluding '\0'
Standard Library Functions
<string.h>
2 strcpy(newStr, oldStr)
copies value of old string to new string
Standard Library Functions
<string.h>
3 strcat(firstStr, secStr)
concatenates first string with second string
enough
Standard Library Functions
<string.h>
4 strcpm(firstStr, secStr)
Compares 2 strings & returns a value
EXAMPLE
For a student store the following :
name (String)
roll no (Integer)
cgpa (Float)
Syntax
struct student { struct student s1;
char name[100]; s1.cgpa = 7.5;
int roll;
float cgpa;
};
Syntax
struct student {
char name[100];
int roll;
float cgpa;
}
Structures in Memory name roll cgpa
struct student {
char name[100]; 2010 2110 2114
int roll;
float cgpa;
}
ACCESS
IT[0].roll = 200;
IT[0].cgpa = 7.6;
Initializing Structures
struct student s3 = { 0 };
Pointers to Structures
struct student s1;
struct student *ptr;
ptr =&s1;
Arrow Operator
(*ptr).code ptr->code
Passing structure to function
//Function Prototype
void printInfo(struct student s1);
typedef Keyword
coe student1;
File IO
RAM Hard Disk
File IO
FILE - container in a storage device to store data
- RAM is volatile
Open a File
Close a File
Write in a File
Types of Files
Text Files Binary Files
textual data binary data
FILE *fptr;
Opening a File
FILE *fptr;
Closing a File
fclose(fptr);
File Opening Modes
"r" open to read
"rb" open to read in binary
"w" open to write
"wb" open to write in binary
"a" open to append
BEST Practice
Check if a file exists before reading from it.
Reading from a file
char ch;
fscanf(fptr, "%c", &ch);
Writing to a file
char ch = 'A';
fprintf(fptr, "%c", ch);
Read & Write a char
fgetc(fptr)
c. free( )
d. realloc( )
malloc( )
memory allocation
initializes with 0
free(ptr);
realloc( )
reallocate (increase or decrease) memory
using the same pointer & size.
Types
for do while
while
for Loop
}
Special Things
- Increment Operator
- Decrement Operator
- Infinite Loop
while Loop
while(condition) {
//do something
}
do while Loop
do {
//do something
} while(condition);
break Statement
for( .. ) {
for( .. ) {
}
}
Functions
Take Do Return
Argument Work Result
void printHello( );
Library User-
function defined
Special functions declared & defined by
inbuilt in C programmer
scanf( ), printf( )
Passing Arguments
functions can take value & give some value
void printHello( );
actual formal
parameter parameters
NOTE
calling function.
Because a copy of argument is passed to the function
Recursion