TimeLinux1

Sunday, November 18, 2012

C Programming Basics - 1


-Major parts of popular operating systems like Windows,
UNIX, Linux are still written in C. This is because even today
when it comes to performance (speed of execution) nothing
beats C. Moreover, if one is to extend the operating system to
work with new devices one needs to write device driver
programs. These programs are exclusively written in C.
19 -Embedded systems in Mobiles and Gadgets are written in C because these programs not only have to run fast but also have to work in limited
amount of memory. Only C fits the bill.
-This is also true for Computer games that must be fast.
-C makes lowlevel hardware interaction possible without loss of performance.

23 -Constants in C are of two types:
.primary integers, real (aka float), characters
.secondary array, pointer, structure, union, enum etc

36 -printf() and scanf() are counterpart functions.
-printf() prints output, scanf() reads input.
-scanf() takes pointers as parameters (pointer=memory address of a variable).
-eg:
printf ( "Enter values of p, n, r" ) ;
scanf  ( "%d %d %f", &p, &n, &r ) ;

Nov 08
=====
174 -when a function is called, the caller is temporarily suspended while the
called function goes to work. Once the called function is done it
returns the execution control back to the caller
183 -To send back control, its enough to have a closing bracket }. No return reqd.
-any function can be called, even main().
179 -a function can call itself (recursion).
-to be called by a function, the called function must be defined outside the
caller.
-Two types of functions exist:
.library functions -a group of functions labeled as a library
.user defined functions
180 -if there are some logically isolated tasks, put them in a function.
-Such programs are easy to read and maintain.


Nov 10
=====
197 -Pointers:
Pointers are unsigned integers that hold an address in memory.
The address they hold is the address of a variable.
-Pointers are created with * operator.
-& - address of a variable
-* - value at the pointer, indicates the creation of a pointer.
- [ampao starvat]
-eg: float i=29.7; --variable created and given an address
int *j; --variable to store some address of some int
j=&i; --that some address is now address of i
-so *j is value at the address of i = value of i = 29.7
-also since j=&i,
*j=*(&i)=value at(address of i)=29.7
-also &j=&(&i)=address of(address of i)=address of i [verified]
-so *(&j)=value at(address of(address of i)=value at(addr of i)=29.7

198 -float *f does not mean address is float.
-float *f means value at f points to a float variable
-similarly, char *ch means value at ch points to a character variable

Nov 11
=====
201 -Pointers and functions.
-functions can receive arguments by one of two methods:
. by values  of arguments -eg afunc(i, j)   - call by value
. by address of arguments -eg afunc(&i, &j)  - call by reference
-difference between the two methods:
. arg by val - operations in called func dont affect caller func
. arg by addr - operations in called func affect caller func
-Note: call or arg by value is more common than call or arg by addr (or ref)
call by reference allows function to return more than one value
-eg:
main( )
{
int radius ;
float area, perimeter ;
printf ( "\nEnter radius of a circle " ) ;
scanf ( "%d", &radius ) ;
areaperi ( radius, &area, &perimeter ) ;
printf ( "Area = %f", area ) ;
printf ( "\nPerimeter = %f", perimeter ) ;
}
areaperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}

And here is the output...
Enter radius of a circle 5
Area = 78.500000
Perimeter = 31.400000
-conclusion: we sent val of radius and addr of area, perim and got two values of area and permiter in return.

204 -recursion:
when a function calls itself
-Thus during every recursive function call we are working with a fresh set of parameters.

212 -It is possible to add user defined functions to existing system libraries.
-It is possible to add user defined functions to user defined libraries.

Nov 13
=====

230 -int data type can be of two types:
.short int - 2 bytes in memory
.long  int - 4 bytes in memory
291 -For linux/windows these days, ints are 4 bytes
-further they can be signed and unsigned.
-unsigned means +ve values so while signed short int range from -32k to +32k,
unsigned int range from 0 to 65k, almost doubling the range, in a way.
-even chars have signs (since they are an ascii number internally).
-chars are 1 byte long. signed chars are -128 to +128. unsigned 0 to 256.
-floats occupy 4 bytes.
-double occupy 8 bytes in memory. long double is 10 bytes in memory.
-Note: for older 16 bit processors, which dont have the ability to hold huge
numbers outside their range (>2to16), an emulator software is used
that mimics larger address area. Downside, the emulator approximates
the numbers to fit in, so the answers may be a little ways off esp
for very large nums.
-Every variable has a default storage class.
-storage class of a variable, tells:
. where the variable will be stored
. what will be the initial value (unless explicitly specified)
. what will be the scope of the variable
. life duration of the variable (how long it exists?)
-there are four storage classes:
. automatic -stored in mem, no fixed defaults, scope local
. register -stored in cpu registers, no defs, scope local
. static -stored in mem, default 0, persist bet functions
. extern -srored in mem, default 0, scope global, life of prog
-if variables are not definded explicitly, they are assumed to be auto.

Nov 14
=====

257 -The C preprocessor is exactly what its name implies. It is a program that processes our source program before it is passed to the compiler. -Preprocessor commands (often known as directives) form what can almost be considered a language within C language.
-The directives can be placed anywhere in a program but are most often placed at the beginning of a program, before the first function definition.
-The directives could be in the form of:
. macro expansion #define ABC
. file inclusion #include abc.h
. conditional compilation --not common  #if elif
. miscellaneous --not common  #undef or pragma
-The benefit of preprocessor commands is that it improves compiler performnc.
-They also improve readability of a program.
-prerprocessor directives are defined with
. the '#define keyword'
. in CAPS
. no semicolons
. in the very beginning of the program
-eg: #define PI 3.14
main()
...
-Macros can also have arguments but everything should be in parentheses
-eg: #define AREA(x) (3.14*x*x)

257 -source code to executable in two steps:
. source code + preproc -> object code - by compile step
. object code + system libs -> exe code - by link step
267 -macros behave like functions but
-in macro calls, the compiler simply dumps macro code when called
-in func  calls, the control is actually passed to the func
-macros are faster than functions but good when code is small.
-macros increase code size, whereas functions decrease them.

269 -#include "file" - looks for file in the whole search path
-#include <file> - looks for file only in the pwd or compiler dirs

285 -array - a set of similar data types
-arrays allow multiple values to be stored in a single variable
-arrays are also called subscripted variable
-pointers and arrays are closely related.
-all arrays make use of pointers internally
289 -no matter how big an array, its elements are always stored in contiguous memory locations.
286 -a string is an array of characters.
290 -array initialization:
int num[6] = {1,2,3,5,5,6}
or int num[]  = {1,2,3,4,5,6}
292 -The compiler does not check if an array size is too small for the values being input.
296 -Numbers can be added and subtracted to pointers
-eg: if i=4 and j=&i=lets say 10000
-then i-2 and j-4
=> i=2 and j = j-4 = 10000 - 4x4 = 9986 (size of 4 ints subtracted)
297 -while a number can be added or subtracted to pointers, pointers themselves cannot be added, multiplied or divided by another pointer.

286 -Accessing array elements by pointers is always faster than accessing them by subscripts.
302 -Whole arrays can be passed to functions via pointers.
-Passing the zeroth element of an array is the same as passing the whole arr.
-eg: displ (&num[0],5) = displ (&num,5) = pass a 5 elemt arr num to displ()
-also if int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
-then *num = the zeroth element of the array, that is, 24.
-also, *num and *( num + 0 ) both refer to 24.
-similarly, *(num+i)=*(i+num)=num[i]=i[num] == the ith element of arr num.
Nov 15
=====
304 -2D arrays aka matrix can be thought of as a stack of 1D arrays on top of each other.
-arr[5][2] means 5 1D arrays of 2 elements each on top of each other.
-the first element is [0][0], last [n][m] which means
n rows, m columns
and [n+1][m+1] element is row col ender.
-each row has a two values.
-eg ID, age for a group of persons.
-you can omit row nums but not column nums.
-eg: arr[][3] is ok, arr[3][] is not.
307 -While a 2D array can be thought of as a matrix, the arrangement is imaginary.
-This is because memory doesn’t contain rows and columns.
-In memory whether it is a one-dimensional or a two-dimensional array the array elements are stored in one continuous chain.

Nov 16
======
-strings are arrays of characters.
-string arrays are terminated by a null character ('\0').
-to output strings, we can use %s placeholder and call string in printf.
-eg: printf("%s", str); --prints string called str.
-scanf() function cannot receive multiword strings.
-To receive and print multiword strings, we have gets() and puts() functions.
-puts automatically puts a new line character at the end of its output.

-The malloc function:
is a standard library function that requires the number of bytes to be allocated and returns the base address of the chunk of memory that it allocates.
-it allocates memory at runtime ie is dynamic.
-The address returned by malloc() is always of the type void *.
-to use it, we need to #include "alloc.h" as preprocessor.
-eg: ptr = malloc(6)  allocates 6 bytes of mem and sets the base addr to ptr.

No comments:

Post a Comment