Expect problems and eat them for breakfast.
--Your friends at LectureNotes

Basic Computer Engineering

by Kamalakar Sreevatasala
Type: NoteInstitute: College Course: B.Tech Specialization: Computer Science EngineeringViews: 8Uploaded: 5 months agoAdd to Favourite

Touch here to read

Basic Computer Engineering by Kamalakar Sreevatasala

Kamalakar Sreevatasala
Kamalakar Sreevatasala

/ 26

Share it with your friends

Suggested Materials

Leave your Comments


Kamalakar Sreevatasala
Kamalakar Sreevatasala
Structure of C Program A C program consists of one or more functions or code modules. These are essentially groups of instructions that are to be executed as a unit. Each C program must contain a main() function. This is the first function called when the program starts to run. A C program contains the following sections. Documentation Section Define section Function prototypes and global data declarations The main() function Local variable and function declaration section Executable statements User defined Function definitions Consider first a simple C program. /* comment line */ #include <stdio.h> void main() { printf( "Hello World\n" ) ; } ➢ This program consists of just one function the mandatory main function. ➢ The parentheses, ( ), after the word main indicate a function. ➢ The curly braces, { }, are used to denote a block of code. ➢ Comments are contained within a /* ... */ pair. ➢ The line printf("Hello World\n " ) ; is the only C statement in the program and must be terminated by a semi-colon. ➢ All C compiler include a library of standard C functions such as printf which allow the programmer to carry out routine tasks such as I/O, maths operations, etc in a standard form. ➢ Header files must be included within a Preprocessor . ➢ The line #include <stdio.h> instructs the preprocessor to include the file stdio.h into the program. Basic Data Types There are five basic data types char, int, float, double, and void. All other data types in C are based on these. char int float double void 1 byte ( 8 bits ) with range -128 to 127 16-bit OS : 2 bytes with range -32768 to 32767 32-bit OS : 4 bytes with range -2,147,483,648 to 2,147,483,647 4 bytes with range 10-38 to 1038 with 7 digits of precision 8 bytes with range 10-308 to 10308 with 15 digits of precision generic pointer, used to indicate no function parameters etc. 1/26
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
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
\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

Lecture Notes