C LANGUAGE NOTES
C LANGUAGE NOTES
C LANGUAGE NOTES
1 2 3 4 printf(“xyz”);
Void print_grid(int size) // this has no output but takes input of variable size
Int uses 32 bit of storage of our computer’s RAM 2^31 to the positive and 2^-31 to the
negative
To avoid integer overflow we use long which has more bit’s……….long uses 64 bit
%f represents floating point value
Type casting converting one data value to another …. Used to avoid truncation
Floating point imprecision the computer can be precise only upto certain value……. Double is
used to solve this problem it has twice as many bits as that of float
Unsigned data type eliminates negative and and the range for positive goes upto 4 billion
Char data type takes 8 bit (1 byte) of memeory
Float store 32 bits (4 byte)……double 64 bits
Void is a type but not a data type…..functions can have void return type(they don’t return
value)
String stores a series of char
Don’t redeclare var again and again just once is enough…..intialisation is another thing it’s
different from declaring
Modulus operator (%) used for getting the remainder
X = x*5 or x *= 5 same thing
Boolean expressions are of two types logical and relational operators #cs50pg2
(&&) called impercent is used for two variables it is only true when both variables are true
(||) this is logical or true when either one is true called vertical block
(!) logical not
Relational operators: <,>,== (equality),=! (inequality),
Conditionals: if, if-else, else-if, nested loop if-if-if-else,
Switch statement (imp break;)
Int X = (expression) ? 5 : 6; ( if else statement short form ) called ( ? :) ternary operator ( it
works like if expression is true the value will be 5 else the value will be 6)
while ( Boolean expr) loop starts when the Boolean expr is true
do while this loop will execute all lines of code inside the curly bracket that comes under do
and then checks the condition again repeats the loop
for loop it can declare and initialize variable then gives Boolean expr in middle then
increment or decrement [ for (start; bool expr; increment)]
while -use to repeat a loop unknown no. of times, and possibly not at all
do- while – use to repeat a loop an unknown number of times, but at least once
for – loop repeats discreate number of times’
linux command line: 1)ls (list) (gives list of all the files in the directory..green is the
executable file)
2) cd (change directory) – allows to navigate between the directory … pwd- present working
directory (helps you identify where you are)
3) mkdir – make directory
4) cp – copy ( cp sorce to destination)
5) rm – to remove file
6) mv – move sorce to destination
ARRAYS
#cs50pg3
Pre processor – are used processed before the program- #include
For finding errors in the code you can use – printf , debug50 ,
rubber duck
Data types-
1) bool – 1 byte
2) int - 4 byte
3) long – 8 bytes
4) float – 4 bytes
5) double - 8 bytes
6) char - 1 byte
7) string – depends
If – strings words[2];
Words[0] = “hi!”;
Words[1] = “BYE”;
Printf(“%s\n”, words[0]);
#cs50pg4
Printf(“%s\n”, words[1]); ….this will print the letters on that index
You can find the length of the string length by using library string.h
as shown below-
Int main(void)
Printf(“%i\n”,length):
}
#cs50pg5
C++ NOTES
Data types
Return 0 – exit status it tells the complier that the program is over it
will not execute anything after that
= - Assigns value
C notes
variables is the name of a memory location which stores some data – case sensitive, 1st
character is alphabet or underscore, variable can’t be a comma, blank space, only
underscore can be used in variable
keywords are special words which are known to the compiler eg: int, switch, break, float etc.
%d is used to store int value, % f is used to store float value , %c is used to store character
value
For taking input use scanf : scanf (“%d”,&age)
Compiler translates C cod into machine code : gcc hello.c
For running the program for windows: ./a.exe
Instruction – statements
Type declaration instruction - in which you give value to a variable but remember to declare
variable before using it
A+B …. A= operand1 , B=operand 2, + = operand …. If y= A+B (the value of A+B is stored in y)
Precedence – priority order of operators : (1) *,/,% (2) +,-(3) =(assignment) …….. and always
calculate from left to right for operators like / , * they have the same preference so go from
left to right….if there’s bracket solve inside the bracket
Relational operators : ==, >,<,<=,!= (don’t give space between exclamation and equal to sign)
True =1
False=0
A= A+B OR A+=B
‘I’ is iterator variable or counter
++1 pre increment, 1++ post increment, --1 pre decrement, 1-- post decrement
Break helps us to get out of the loop…..remember break,gets you out of nested loop
Continue skip to next iteration
Function is a block of code that performs a particular task
Passing arguments –parameter (it takes value), return value (it gives value)
Above is the precedence order when logical and arithmetic operators are in one expression
ARGUMENT VS PARAMETER
In function definition
Call
Value Value
Gets(str)-input , puts(str)-output, fgets(str, n, file) – use stdin for file., and it stops when n-1 char
input or new line is entered……..puts print the statement and it automatically puts you in next line
String is a pointer
Difference between initializing the string with pointer and array – with pointer you can reinitialize
the strings, arrays can not be modified
Int roll;
Float cgpa;
S1.name
S1.rollno
Benefits of array – you don’t have to rememeber 100 variables, it also helps in concepts like salting
Benefits of structure – we don’t have to declare a lot of variable , good data management and
organization
ARRAY OF STRUCTURES –
ECE[0].roll=700;
INTIALIZING STRUCTURE –
Ptr = &s1;
structure pass value by call by value therefore if you make any change in the function and pass it to main the value will not change but
if you change the value in main before calling the function then the value will change and new value will be passed to the function
{ int roll;
2) a file ptr that points to this structure & is used to acess the file
4) fptr = fopen("filename",mode);
MODES –
And if a file exist then that file will open and all the data in the file will get deleted and new data
will be stored
5) “a” open to append………..if you want to keep the data and store new data
Make sure if you check if a file exists before using it
fgetc(fptr)…..char read
fputc(‘a’,fptr)….char read…..remember only applicable to charchter for other data types use fscanf
EOF(END OF FILE) -fgetc returns EOF to show that the file has ended
It is a way to allocate memory to a data structure during the runtime…..we need functions to
allocate memory dynamically
[] list
() tuple not compulsory
{} set
There is no index in set (unodered)
Dictionary if information is stored in pair then that info is stored in dictionary
Function : if a part performs a specific operation that is called function
Types of functions : in-built,module,user-defined
In built – int, str, bool
Module – import math
Print(dir(math)) …….(all the function in the math directory will be printed)
Print(sqrt(4))
From math import *……..(means you can use all the function no need to mention sqrt or any