C Programming Language Unit 1 Syllabus
C Programming Language Unit 1 Syllabus
Evolution and Applications of C structure of a C program Data Types Declarations operators Expressions Type conversions Built-in functions.
Evolution of C Created by Dennis Ritchie at AT&T Bell laboratory, USA in early 1970s. Initially it was implemented in UNIX operating system. C is the successor of the language B, by Ken Thompson Initially implemented in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, known as the K&R standard. In the mid 1980s it was standardized by the ANSI X3J11 committee and known as ANSI C Features of C Middle level language Structured language Machine independent Compiler based language Uses top down approach Supports pointers Applications of C Used for writing embedded software in electronics Used to develop verification software, test codes and simulators Used to develop system softwares such as loaders, linkers, assemblers, compilers, etc Used for creating graphics packages and game applications. Used to develop operating systems (eg. UNIX)
Structure of C program
1. Documentation section: this contains the information about the program, its author, versioning, algorithms used, etc. This is mainly used to describe the program. 2. Link section: This section is used to include/link the header files and other library files. #include<stdio.h> 3. Definition section: This section is used to define constant values. #define pi 3.14 //assigns the value 3.14 to the variable pi which is a constant 4. Global declaration: Used to declare the variables that are needed for use throughout the program. 5. Main function: Every C program should contain a main(). Execution starts only from main(). 6. Subprogram section: Definition of other user defined functions is done here. The number of functions in this section varies depending upon the program.
Structure of a function in C Every function in C consists of 1. Function header 2. Body of the function.
Function header: Eg. Consider the following function int add(int x, int y) { } In the above function the int represents the return type of the function. i.e. the type of the value which the function is intended to return back. This return type can be of any valid data type supported by C. Also a function can return only one value. The next word add denotes the name of the function. This name can be any valid identifier. The function names are defined by the user. The values enclosed within the braces () is called parameter list or argument list. Each value is a parameter or an argument. The number of parameter in the parameter list can be from 0 to any number of values. Body of the function: The body of the function is enclosed within curly braces {}. The body of the function contains the declaration statements and other necessary statements required for the function. Also a special statement called the return statement is generally used in functions. This return statement is used to return the value from the function. The value returned by the return statement must match the return type in the function header. Comment lines The comment lines are used to embed readable annotations within the program such as code description, algorithm description, name of the author, etc. To include a single line as comment in the C program, the line is preceded by two forward slash (//). When the comment to be included is in multi line then these lines are enclosed within the /* and the */ symbol. Eg. /*This program is used to find the sum of two numbers*/ #include<stdio.h> #include<conio.h> //This is the main program void main() { int a=2, b=3, c; c=a+b; printf(\n The sum is : %d,c); }
Character set of C The following set of characters is supported by C language. Uppercase letters A to Z Lowercase letters a to z Digits 0 to 9 Special characters
Identifiers Identifiers are the names given to various program elements such as variables, functions, etc. The following are the rules that are to be followed while define an identifier. The identifier must start with an alphabet or underscore only. The name of the identifier can include alphabets, numbers and special characters. Keywords cannot be used as identifiers. Upper case and lower case alphabets are distinct No commas or blank space is allowed. No special character other than underscore (_) is permitted.
Examples of valid identifiers: int x; int x1; int _x; int x$; int abc_sdf;
Examples of invalid identifiers: int 1x; int break; int student age; -starts with a number -break is a keyword -space is not allowed within a single identifier
Keywords Keywords are certain reserved word in C language. These words have a standard and predefined meaning in C. These words can be used for intended purposes only. These keywords cannot be used as identifiers. auto break case char const continue default do Constants double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
Rules for Constructing Integer Constants An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is -32768 to 32767. Ex.: 426, +782, -8000, -7605 Rules for Constructing Real Constants Real constants are often called Floating Point constants. The real constants could be written in two formsFractional form and Exponential form. Following rules must be observed while constructing real constants expressed in fractional form: A real constant must have at least one digit. It must have a decimal point.
It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. Ex.: +325.34, 426.0, -32.76, -48.5792
In exponential form of representation, the real constant is represented in two parts. The part appearing before e is called mantissa, whereas the part following e is called exponent. Following rules must be observed while constructing real constants expressed in exponential form: The mantissa part and the exponential part should be separated by a letter e. The mantissa part may have a positive or negative sign. Default sign of mantissa part is positive. The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38. Ex.: +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5 Rules for Constructing Character Constants A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotation. The maximum length of a character constant can be 1character. Ex.: 'A', 'I', '5', '=' Escape sequences Certain non printing characters are expressed in terms of escape sequences. An escape sequence always begins with a backward slash (\) followed by one or more characters. The escape sequences though they contain more than one character, they represent a single character only. They are generally used for formatting the output. Eg: printf(C\nProgramming\nLanguage); The output is as follows: C Programming Language
String Constants A string constant consists of any number of consecutive characters (including none), enclosed in (double) quotation marks. Eg: 123 C Programming 25$ A Variables Variables are the names given to the memory locations. The variables can store integer, real or character constants. A variable of a particular type can store a constant of that same kind only. i.e, an integer variable can hold an integer constant only. The rules for defining a variable are as follows A variable name is any combination of alphabets, digits or underscores. The first character in the variable name must be an alphabet or underscore. No commas or blanks are allowed within a variable name. No special symbol other than an underscore (_) can be used in a variable name. Ex.: si_int, m_hra, pop_e_89 Arrays An array is an identifier that refers to a collection of data items that all have the same name. All the data items must be of the same type (e.g., all integers, all characters, etc.). The individual data items are represented by their corresponding array-elements (i.e., the first data item is represented by the first array element, etc.). The individual array elements are distinguished from one another by the value that is assigned to a subscript.
Array declaration int marks[30] ; int specifies the data type of the values that are being stored in the array. marks specifies the name of the array variable. [30] represents the size of the array, i.e., this array is capable of storing 30 integer constants in it. Accessing an array element Individual element in the array can be access by using the subscript of the array (the number in the bracket following the array name). This subscript number specifies the elements position in the array. All array elements are numbered starting from zero. Eg: int num[5];
Initialising values for an array The values for an array can be given for individual elements or as a whole array. Eg: int num[5]={2,8,7,6,0} (or) int num[5]; num[0]=2; num[1]=8; num[2]=7; num[3]=6; num[4]=0; in the above examples both will have the same values stored in it. Expressions An expression represents a single data item, such as a number or a character. The expression may consist of a single entity, such as a constant, a variable, an array element or a reference to a function. It may also consist of some combination of such entities, interconnected by one or more operators. Eg: a+b x=y c=a+b x<=y x= =y i++ Statements A statement causes the computer to carry out some action. There are three different classes of statements in C. They are
Expression statement: An expression statement consists of an expression followed by a semicolon. The execution of an expression statement causes the expression to be evaluated.
Eg:
Compound statement: A compound statement consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression statements, compound statements or control statements. A compound statement does not end with a semicolon. Eg: 1. { pi = 3.141593; circumference = 2. * p i * radius; area = p i * radius * radius; } 2. for(i=0;i<=n;i++) { sum=sum+i; } Data types
The data types are used to identify the type of the variable and the value stored in it. The variables should be declared with the data types before they are used in the program. Declaring the variables with the data types enables the system to allocate the required memory for the variable depending upon its data type.
Qualifiers/Modifiers The qualifiers are used to qualify the primary data types. They define the amount of storage allocated to the variable. These are the qualifiers short long signed unsigned
Declarations A declaration associates a group of variables with a specific data type. All variables must be declared before they can appear in executable statements. A declaration consists of a data type, followed by one or more variable names, ending with a semicolon. Each array variable must be followed by a pair of square brackets, containing a positive integer which specifies the size (i.e., the number of elements) of the array. Eg: int a, b, c ; float rootl, root2; char flag, text[80] ;
the above can also be declared as follows int a; int b; int c; float rootl; float root2; char flag; char text[80]; The qualifiers of the data types should also be included while declaring the variable. Eg: short int a, b, c; long int r, s, t ; unsigned char x, y; Initial values can also be given to the variables while declaration itself. Eg: int c = 12; char star = * ; float sum = 0.0 ; double factor = 0.21023e-6; While initializing the array with values while declaration itself, it is not necessary to specify the size of the variable. The size of the length of the value is taken as the size of the array variable. Eg: int a[]={3,5,7,8}; //The size of a is automatically taken as 4 char place[]={karur}; //The size of place is now 6 char alphabet[]={a,b,c,d}; //The size of alphabet is 4 While initializing arrays with values lesser than its size, the remaining locations are automatically assigned with zeros or any other values arbitrarily. Eg: Char place[20]={California}; Here the word California is stored from 0th location to 10th location. The remaining locations from 11 to 19 are stored with any other random value.