C Programming Notes! Yayyy!
CHAPTER 1
Variables: Name of a memory location which stores some data.
RULES
a. Variable are case sensitive
b. 1st Character is alphabet or '_'
c. no comma/blank space
d. no other symbol other than '_'
int stores integer
float is for decimal values
char is for special characters/symbols with ' '
Constants:
integer constants= 1,2,3,0,-1
reals constants= 1.0, 3.14, -2.4
character constants= 'a', 'b', '#' ALWAYS IN SINGLE QUOTES
Keywords: reserved words that have special meaning to the compiler
32 KEYWORDS IN C
cant be used as variables
Programming Structure
#include<stdio.h> pre-processor director
int main(){
printf("Hello World");
return 0;
}
Comments:
Lines that are not part of program. its just for our use. doesn't make sense to the
compiler.
Single line comment //
Multiple line comment /*
*/
Output: printf("Hello World");
printf("Hello World\n"); for next line output printing
CASES FOR VARIOUS OUTPUTS (FORMAT SPECIFIERS)
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: scanf("%d", &age); (& is the address)
COMPLILATION: A computer program that translates C code into machine code
INSTRUCTIONS:
These are statements in a program
>>TYPE DECLARATION INSTRUCTION: declare variable before using
>>ARITHMETIC INSTRUCTIONS: operand and operator
single variable on the LHS only
> for power a= pow(b,c);
printf("%d", power);
use #include<math.h> for these wokayyy!!
> for modular operator% returns remainder for int
printf("%d", 16%10);
it'll come as 6 which is the remainder.
!!doesnt work for float values.
#include<stdio.h>
#include<math.h>
int main(){
int a,b,power;
printf("enter a: ");
scanf("%d", &a); //just an example code covering topics above
printf("enter b: ");
scanf("%d", &b);
power= pow(a,b);
printf("power is: %d", power);
return 0;
}
Type conversion
int op int------ int
int op float----float also int<<<float wokay!
float op float--float
int a= (int) 1.9999; so it will convert float to int
Operator Precedence
its similar to bodmas but not exactly bodmas
* / % ------ + - ------ = that's the order.
Associativity rule= for same operators same precedence left to right
also solve brackets first.
OPERATORS:
Arithmetic---- + - % * /
Relational---- == > >= < <= !=
now a=b it puts value of b in a
in a==b it checks weather they're equal.
1= true, 0= false in c programming.
Logical------- &&and ||or !not
LOGIC GATES
1 2 OUT
T T T
T F F THIS IS FOR &&
F T F
F F F
1 2 OUT
T T T
T F T THIS IS FOR ||
F T T
F F F
OPERATOR PRECEDENCE:
PRIORITY OPERATOR
1 !
2 * / %
3 + -
4 < <= > >=
5 = !=
6 &&
7 ||
8 =
Bitwise---------
Assignment------ short hand operators, basically combines
like a = a + b it's like a+=b ashte.
= += -= *= /=
Ternary---------
Syntax: Condition? doSomething if TRUE : doSomething if False;
CONTROL INSTRUCTIONS: determines flow of program
>>> sequence control: goes one after the other line no big deal
>>> decision control: if/else for yes no questions
>>> loop control: same instruction baar baar baar baar
>>> case control: like monday to Tuesday kaam pe jaana hai but sunday
nahi isko ham code pe dikhaenge.
Conditional statements:
IF-ELSE
if(condition){
//do something if true if, else if, else if.... else.
}
else{
//do something if false
}
SWITCH
switch(number){ Rulesss
case C1/do something cases can be in any order
break; Nested switch(switch inside switch)is ok
case C2: //do something
break;
default: //do something
}
Loop Control Instructions:
FOR LOOP:
syntax: for(initialisation; condition; updation){
//do something
}
can be used in i=i+1
for ( int i = 0; i <= 10; i=i+1){ i++ post increment
printf("%d \n", i); ++i pre increment
} i-- pre decrement
--i post decrement
WHILE LOOP:
syntax:
int i=1;
while (i<=5)
{
printf("Hello World \n"); variable is declared outside
i++;
}
DO WHILE LOOP:
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n", i);
i++;
} while (i<=5);
return 0;
}
FUNCTIONS: block of code that performs a particular task.
TAKE ARGUMENT
|
DO WORK
|
RETURN RESULT
>> It can be used multiple times
>> Increases code reusability
>> Execution always starts from main
>> A function gets called directly or indirectly from main
>> There can be multiple function in a program.
syntax:
Function Prototype TELL COMPILER
void printHello();
Function Definition
void printHello(){ DO WORK
printf("hello");
}
Function Call
int main(){
printHello(); USE WORK
return 0;
}
Types of functions
>library function >user defined function
special functions
inbuilt in c
eg: printf, scanf
Passing arguments: functions can take value and give some value
| |
parameter return value
void printHello();
void printable(int n);
int sum(int a, int b);
example:
//print table of n using function okay
#include<stdio.h>
void printTable(int n);//assigning
int main(){
int n;
printf("enter value of n: ");
scanf("%d", &n);
// this is argument or actual parameter
printTable(n);//calling the function
return 0;
}//its pretty simple only guys dw.
void printTable(int n){
for (int i = 1; i <= 10; i++)
{//defining
printf("%d \n", i * n);// this is parameter or formal parameter
}
ARGUMENT V/S PARAMETER
values that are values in function
passed in declaration &
function call definition
used to send used to receive
value value
actual parameter formal parameter
>>>function can only return one value at a time
>>> changes to parameters in function don't change the values in calling function.
RECURSION!!
example:
#include<stdio.h>
void printHW(int count);
int main(){
printHW(5);
return 0;
} MAGICCCC IT SEEMS BS.
//recursive function ITS JUST SOO MUCH MORE CONFUSING. ugh
void printHW(int count){
if (count == 0){
return;
}
printf("Hello World \n");
printHW(count-1);
}