×
Everything you do now is for your future. Think About that.
--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-2

Modifying Basic Types Except for type void the meaning of the above basic types may be altered when combined with the following keywords. Signed unsigned long short The signed and unsigned modifiers may be applied to types char and int. For example an unsigned char has a range of 0 to 255. An unsigned integer on a 16-bit system has a range of 0 to 65535 as opposed to a signed int which has a range of -32768 to 32767. The long modifier may be applied to type int and double only. A long int will require 4 bytes of storage and has a range of -2,147,483,648 to 2,147,483,647. A long double will require 10 bytes of storage and will be able to maintain up to 19 digits of precision. The short modifier may be applied only to type int and will give a 2 byte integer Variables A variable is a named location which is used to hold a value which may be modified by the program. All C variables must be declared as follows :type variable-list ; For Example :int i ; char a, b, ch ; Variables are declared in three general areas in a C program. 1. When declared inside functions as follows they are termed local variables and are accessible within the function only. void main() { int i, j ; ... } 2. When declared outside functions they are termed global variables and are visible throughout the file or have file scope. int i ; void main() { ... } 3. When declared within the braces of a function they are termed the formal parameters of the function as we will see later on. int func1( int a, char b ) ; Rules for Variable Names ➢ Names of variables and functions in C are called identifiers and are case sensitive. ➢ The first character of an identifier must be either a letter or an underscore. ➢ while the remaining characters may be letters, numbers, or underscores. ➢ Identifiers in C can be up to 31 characters in length. Initialising Variables Syntax :- type var-name = constant ; For Example :char ch = 'a' ; double d = 12.2323 ; int i, j = 20 ; /* note in this case i is not initialised */ 2

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

Lecture Notes