Computer Programming UCT-144 Unit-I Fundamentals of C: University Institute of Engineering
Computer Programming UCT-144 Unit-I Fundamentals of C: University Institute of Engineering
UCT-144
UNIT-I
Fundamentals of C
30-03-2019
Features of C
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. 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<conio.h> // 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
30-03-2019
Types of Operators
1. Arithmetic Operators
2. Increment & Decrement Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Assignment Operators
7. Conditional Operators
8. 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
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
30-03-2019
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()
{
When the above code is compiled and
int sum = 17, count = 5; executed, it produces the following
result:
double mean;
mean = (double) sum / count; Value of mean : 3.400000
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
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