×
Yes studying sucks... But NO, NOT more than FAILURE.
--Your friends at LectureNotes
Close

Note for Basic Computer Engineering - BCE by Kamalakar Sreevatasala

  • Basic Computer Engineering - BCE
  • Note
  • College - Nallamuthugoundermahalingamcollege
  • Computer Science Engineering
  • B.Tech
  • 5 Topics
  • 24 Views
  • Uploaded 1 year ago
Kamalakar Sreevatasala
Kamalakar Sreevatasala
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-3

Storage Classes There are four storage class modifiers used in C which determine an identifier’s storage duration and scope. auto static register extern 1. The auto storage class is implicitly the default storage class used and simply specifies a normal local variable which is visible within its own code block only and which is created and destroyed automatically upon entry and exit respectively from the code block. 2. The register storage class also specifies a normal local variable but it also requests that the compiler store a variable so that it may be accessed as quickly as possible, possibly from a CPU register. 3. The static storage class causes a local variable to become permanent within its own code block i.e. it retains its memory space and hence its value between function calls. 4. When applied to global variables the static modifier causes them to be visible only within the physical source file that contains them i.e. to have file scope. Whereas the extern modifier which is the implicit default for global variables enables them to be accessed in more than one source file. Constants Constants are fixed values that cannot be altered by the program and can be numbers, characters or strings. Some Examples :char : 'a', '$', '7' int : 10, 100, -100 unsigned : 0, 255 float : 12.23456, -1.573765e10, 1.347654E-13 double : 1433.34534545454, 1.35456456456456E-200 long : 65536, 2222222 string : “Hello World\n” float f_val ; double d_val ; f_val = 123.345 ; d_val = 123.345 ; However the value may be coerced to type float by the use of a modifier as follows :f = 123.345F ; Integer constants may also be forced to be a certain type as follows :100U --- unsigned 100L --- long Integer constants may be represented as either decimal which is the default, as hexadecimal when preceded by "0x", e.g. 0x2A, or as octal when preceded by "O", e.g. O27. Character constants are normally represented between single quotes, e.g. 'a', 'b', etc. char ch = 97 ; char ch = 'a' ; There are also a number of special character constants sometimes called Escape Sequences, which are preceded by the backslash character '\', and have special meanings in C. 3

Text from page-4

\n \t \b \' \" \0 \xdd newline tab backspace single quote double quote null character represent as constant hexadecimal Console Input / Output functions some of the more common input and output functions provided in the C standard library. printf() The printf() function is used for formatted output and uses a control string which is made up of a series of format specifiers to govern how it prints out the values of the variables or constants required. The more common format specifiers are given below %c %d %i %u %ld %lu For Example :- character signed integer signed integer unsigned integer signed long unsigned long %f floating point %lf double floating point %e exponential notation %s string %x unsigned hexadecimal %o unsigned octal %% prints a % sign int i ; printf( "%d", i ) ; The printf() function takes a variable number of arguments.. Some further examples :int i = 10, j = 20 ; char ch = 'a' ; double f = 23421.2345 ; printf( "%d + %d", i, j ) printf( "%c", ch ) ; printf( "%s", "Hello World\n" ) ; printf( "The value of f is : %lf", f ) ;/*Output as : 23421.2345 */ printf( "f in exponential form : %e", f ) ; /* Output as : 2.34212345e+4 scanf() This function is similar to the printf function except that it is used for formatted input. The format specifiers have the same meaning as for printf() For Example :int i, d ; char c ; float f ; scanf( "%d", &i ) ; scanf( "%d %c %f", &d, &c, &f ) ; scanf( "%d:%c", &i, &c ) ; The & character is the address of operator in C, it returns the address in memory of the variable it acts on. 4

Text from page-5

getchar() and putchar() These functions are used to input and output single characters. The getchar() function reads the ASCII value of a character input at the keyboard and displays the character while putchar() displays a character on the standard output device i.e. the screen. For Example :char ch1, ch2 ; ch1 = getchar() ; ch2 = 'a' ; putchar( ch2 ) ; _flushall() The _flushall function writes the contents of all output buffers to the screen and clears the contents of all input buffers. getch() and getche() These functions perform the same operation as getchar() except that they are unbuffered input functions getche() echoes the character hit to the screen while getch() does not. For example :char ch ; ch = getch() ; Operators One of the most important features of C is that it has a very rich set of built in operators including arithmetic, relational, logical, and bitwise operators. Assignment Operator (=) It assigns a value, an expression to a variable. int x ; x = 20 ; Arithmetic Operators + , - , * , / , % -- modulus / remainder operator For Example :int a = 5, b = 2, x ; float c = 5.0, d = 2.0, f ; x=a/b; // integer division, x = 2. f=c/d ; // floating point division, f = 2.5. x = 5 % 2 ; // remainder operator, x = 1. x = 7 + 3 * 6 / 2 - 1 ;// x=15,* and / evaluated ahead of + and -. Increment and Decrement Operators There are two special unary operators in C, Increment ++, and Decrement -- , which cause the variable they act on to be incremented or decremented by 1 respectively. For Example :x++ ; /* equivalent to x = x + 1 ; */ ++ and -- can be used in prefix or postfix notation. In prefix notation the value of the variable is either incremented or decremented and is then read while in postfix notation the value of the variable is read first and is then incremented or decremented. 5

Text from page-6

For Example :int i, j = 2 ; i = ++ j ; i = j++ ; /* prefix :- i has value 3, j has value 3 */ /* postfix :- i has value 3, j has value 4 */ Special Assignment Operators Many C operators can be combined with the assignment operator as shorthand notation For Example :x = x + 10 ; can be replaced by x += 10 ; Similarly for -=, *=, /=, %=, etc. These shorthand operators improve the speed of execution as they require the expression, the variable x in the above example, to be evaluated once rather than twice. Relational Operators The full set of relational operators are provided in shorthand notation > >= < <= == != For Example :if ( x == 2 ) printf( “x is equal to 2\n” ) ; Logical Operators && || ! ---- Logical AND Logical OR Logical NOT For Example :- if ( x >= 0 && x < 10 ) printf( “ x is greater than or equal to zero and less than ten.\n” ) ; For Example :- 2>1 2>3 i=2>1 ; -- TRUE so expression has value 1 -- FALSE so expression has value 0 -- relation is TRUE -- has value 1, i is assigned value 1 Bitwise Operators These are special operators that act on char or int arguments only. & Bitwise AND | Bitwise OR ^ Bitwise XOR ~ Ones Complement >> Shift Right << Shift left Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 0 0 0 1 1 0 1 Bitwise AND, & RULE : If any two bits in the same bit position are set then the resultant bit in that position is set otherwise it is zero. For Example :1011 0010 (178) & 0011 1111 (63) = 0011 0010 (50) 6

Lecture Notes