Computer Programming UCT-144 UNIT-I Fundamentals of C University Institute of Engineering
Contents • Features of C language • Structure of C Program, • comments, header files, data types, constants and variables, operators, expressions, • evaluation of expressions, • type conversion, • precedence and associatively, • I/O functions
30-03-2019
Features of C
30-03-2019
Figure1. Features of C
Structure of C Program • Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below. 1. 2. 3. 4. 5. 6. 7.
Documentation section Link Section Definition Section Global declaration section Function prototype declaration section Main function User defined function definition section
30-03-2019
Structure of C Program /* The following is a simple C program that prints a message on the screen. */ 1. #include<stdio.h> // header file 2. #include // header file 3. void main() 4. { 5. clrscr(); 6. printf(“Welcome to C”); 7. getch(); 8. }
Rules to write C Program 1. All C statements must end with semicolon. 2. C is case-sensitive. That is, upper case and lower case characters are different. Generally the statements are typed in lowercase. 3. C statement can be written in one line or it can split into multiple lines. 4. Braces must always match upon pairs, i.e., every opening brace { must have a matching closing brace }. 5. Every C program starts with void main() function. 6. Comments cannot be nested. For example, /*Welcome to ‘C’,/*programming*/*/ A comment can be split into more than one line.
Basic Constructs • Preprocessor directives • Header files • Character set • Keywords • Identifiers • Variables • Constants • Operators • Data types and their storage 30-03-2019
Pre-processor Directive • Before a C program is compiled in a compiler, source code is processed by a program called pre-processor. • Commands used in pre-processor are called pre-processor directives and they begin with “#” symbol.
Pre-processor Directives
Header File • A file with extension .h which contains C function declarations and macro definitions and to be shared between several source files. • Types of header files: • the files that the programmer writes and the files that come with your compiler. • Request the use of a header file in program by including it, with the C pre-processing directive #include which comes along with your compiler.
30-03-2019
Header File
30-03-2019
C Variables • An entity that may vary during program execution is called a variable. • Variable names are names given to locations in memory. • These locations can contain integer, real or character constants. In any language, the types of variables that it can support depend on the types of constants that it can handle. • For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant.
30-03-2019
C Constants
30-03-2019
Data Types Sr. No. Types
30-03-2019
Data Types
1
Basic/Primary data types
int, char, float, double
2
enum
3
User-defined/Enumeration data types Derived data type
4
Empty/Void data type
pointer, array, structure, union void
Operators • An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. • Operators are used in program to manipulate data and variables. The data items that operators act upon are called operands. • Some operators require two operands, while others act upon only one operand. The operators are classified into unary, binary and ternary depending on whether they operate on one, two or three operands respectively.
30-03-2019
Types of Operators 1. 2. 3. 4. 5. 6. 7. 8.
Arithmetic Operators Increment & Decrement Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Conditional Operators Some Special Operators
30-03-2019
Precedence of Operators • Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression has to be evaluated. • It refers to the order in which C evaluates operators. • The evaluation of operators in an arithmetic expression takes place from left to right for operators having equal precedence . E.g. k = 2 * 3/4 + 4/4 + 8–2 + 5/8 ; O/P => 8
Precedence of Operators • For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7. • Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Associativity of Operators •
• • •
Associativity tells how an operator associates with its operands. for eg: 1. The unary minus associates minus with the quantity to its right. 2. The assignment operator = associates from right to left. Hence the expression on the right is evaluated first and its value is assigned to the variable on the left. Associativity also refers to the order in which c evaluates operators in an expression having same precedence. Such type of operator can operate either left to right or vice versa.
30-03-2019
Associativity of Operators • • •
The operator () function call has highest precedence & the comma operator has lowest precedence All unary , conditional & assignment operators associate RIGHT TO LEFT . All other remaining operators associate LEFT TO RIGHT
30-03-2019
Associativity of Operators • • •
The operator () function call has highest precedence & the comma operator has lowest precedence All unary , conditional & assignment operators associate RIGHT TO LEFT . All other remaining operators associate LEFT TO RIGHT
30-03-2019
Associativity chart
30-03-2019
Category
Operator
Associativity
Postfix
() [] -> . ++ - -
Left to right
Unary
+ - ! ~ ++ - - (type)* & sizeof
Right to left
Multiplicative
*/%
Left to right
Additive
+-
Left to right
Shift
<< >>
Left to right
Relational
< <= > >=
Left to right
Equality
== !=
Left to right
Bitwise AND
&
Left to right
Bitwise XOR
^
Left to right
Bitwise OR
|
Left to right
Associativity chart
30-03-2019
Category
Operator
Associativity
Logical AND
&&
Left to right
Logical OR
||
Left to right
Conditional
?:
Right to left
Assignment
= += -= *= /= %=>>= <<= &= ^= |=
Right to left
Comma
,
Left to right
Expressions • In programming, an expression is any legal combination of symbols that represents a value. • C Programming Provides its own rules of Expression, whether it is legal expression or illegal expression. For example, in the C language x+5 is a legal expression. • Every expression consists of at least one operand and can have one or more operators. • Operands are values and Operators are symbols that represent particular actions. 30-03-2019
Types of Expressions • Expressions can be classified on the basis of Position of Operators in an expression –
30-03-2019
Rules for evaluating expression 1. First parenthesized sub expression from left to right are evaluated. 2. If parentheses are nested, the evaluation begins with the innermost sub expression 3. The precedence rule is applied in determining the order of application of operators in evaluating sub expressions 4. The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression. 5. Arithmetic expressions are evaluated from left to right using the rules of precedence 6. When parentheses are used, the expressions within parentheses assume highest priority 30-03-2019
L- value and R-value • L-Value stands for left value • L-Value of Expressions refer to a memory locations • In any assignment statement L-Value of Expression must be a container(i.e. must have ability to hold the data) • Variable is the only container in C programming thus L Value must be any Variable. • L Value cannot be Constant, Function or any of the available data type in C.
30-03-2019
L- value and R-value • R Value stands for Right value of the expression. • In any Assignment statement R-Value of Expression must be anything which is capable of returning Constant Expression or Constant Value. • Example:
30-03-2019
Type conversions • It may so happen that the type of the expression and the type of the variable on the left-hand side of the assignment operator may not be same. • In such a case the value of the expression is promoted or demoted depending on the type of the variable on left-hand side of =. • C allows for conversions between the basic types, implicitly or explicitly
30-03-2019
Type conversions • Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. • It is considered good programming practice to use the cast operator whenever type conversions are necessary
30-03-2019
Type casting/ explicit conversion #include <stdio.h> main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean ); }
When the above code is compiled and executed, it produces the following result: Value of mean : 3.400000
NOTE: It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.
30-03-2019
Implicit conversion (a) An arithmetic operation between an integer and integer always yields an integer result. (b) An operation between a real and real always yields a real result. (c) An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.
30-03-2019
Implicit conversion • Conversion during assignments: char c='a'; int i; i=c; /* i is assigned the ASCII code of ‘a’ */
30-03-2019
Basic I/O Functions
30-03-2019
Fig3. Basic I/O
Basic I/O Functions (a) Console I/O functions - Functions to receive input from keyboard and write output to VDU. (b) File I/O functions - Functions to perform I/O operations on a floppy disk or hard disk.
30-03-2019
Console I/O Functions • The screen and keyboard together are called a console. • Console I/O functions can be further classified into two categories— formatted and unformatted console I/O functions. • The basic difference between them is that the formatted functions allow the input read from the keyboard or the output displayed on the VDU to be formatted as per our requirements. • For example, if values of average marks and percentage marks are to be displayed on the screen, then the details like where this output would appear on the screen, how many spaces would be present between the two values, the number of places after the decimal points, etc. can be controlled using formatted functions.
Console I/O Functions
FAQs • What is the difference between formatted and unformatted functions? Give example. • Evaluate the following expressions: a. (a+(a*b/c) b. (a%c/b) where a= 5,b=10,c=15 • What is difference between basic, user-defined and derived data type? • Explain the concept of variable with block diagram.
30-03-2019