chapter2 programming in C
chapter2 programming in C
PROGRAMMING IN C
INTRODUCTION TO C
-
INTRODUCTION TO C
FEATURES OF C
INTRODUCTION TO C
EXECUTION OF C PROGRAM
CHARACTER SET
The characters that can be used to form words, numbers and
expressions depend upon the computer on which the program is
run.
the characters in C are
• Letters
• Digits
• Special characters
• White spaces
C FUNDAMENTALS
Letters Numbers
Uppercase A,B,.......Z all decimal digits 0,1,....9
Lowercase a,b,........z
Special characters
,(comma) .(period) ;(semicolon) :(colon) ?(question mark) ‘(apostrophe) “(quotation mark) !(exclamation mark) |(vertical
tab) /(slash) \(back slash) ~(tilde) _(under score) $(dollar sign) %(percent sign) #(number sign) &(ampersand) ^(caret)
*(asterisk) -(minus sign) +(plus sign) <(opening angle bracket) >(closing angle bracket) ( (left paranthesis) )(right
paranthesis) [(left bracket) ](right bracket) {(left brace) }(right brace)
White spaces
Blank space
Horizontal tab
Carriage return
Newline
Form feed
C FUNDAMENTALS
C TOKENS
C
TOKENS
SPECIAL
KEYWORDS IDENTIFIERS SYMBOLS OPERATORS
CONSTANTS STRINGS
C FUNDAMENTALS
• KEYWORDS
Keywords are the basic building blocks for program statements. All keywords have fixed
meanings and these meaning cannot be changed,ie.they are reserved words
All keywords are written in lowercase letters
C KEYWORDS
PREPROCESSOR DIRECTIVES
A preprocessor is a program that executes before a source program is passed
to the compiler. The preprocessor offers certain actions are known as
preprocessor directives. It performs actions like inclusion of header files,
definition of symbolic constants etc.
Preprocessor directives begin with # symbol.
The directives most often placed at the beginning of the program,before the
function definition.
Example:
#include<stdio.h>
#include<math.h> header files
#define PI 3.14
#define AGE 15 symbolic constants
C FUNDAMENTALS
• OPERATORS
An operator is a symbol that informs the computer to carry out certain
operations. The data item on which the operator will act are called operands.
The operators in C are
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operator
7. Bitwise operators
8. Special operators
C FUNDAMENTALS
DATA TYPES
The data type defines the operations that can be done on the data, the
meaning of the data, and the way values of that type can be stored.
C supports 4 classes of data types
1. Primitive data type(Primary/Basic/Fundamental/Built-in data type)
-int,char,float,double
2. Derived data type -array,pointer,function
3. User defined data type -structure, union
4. Empty set data type -void
C FUNDAMENTALS
C FUNDAMENTALS
C FUNDAMENTALS
C FUNDAMENTALS
SIGNED UNSIGNED
C FUNDAMETALS
VARIABLE
A variable is a data name that may be used to store a data value
The data value can be changed during program execution
Rules for naming a variable
1. Variable names must begin with a letter, some systems permit underscore(_) as the first
character
2. The variable name should not be a keyword
3. White space is not allowed
4. Uppercase and lowercase are significant ie total is not the same as Total or TOTAL
5. 8 characters are treated as significant for many compilers
C FUNDAMENTALS
EXAMPLES
Declaration of variables
A variable can be used to store a value of any data type
Syntax:
datatype v1,v2,……vn;
Here v1,v2,…..,vn are the variables.
Variables are separated with commas.
A declaration statement must end with a semicolon.
Example:
int a,b,total;
float x;
char c;
double ratio;
C FUNDAMENTALS
scanf(“control string”,&variable1,&variable2,……..,&variablen);
Here the control string contains the format of data being received which is also
known as format specifiers.
The & symbol before each variable name is an operator that specifies the
variable name’s address.
We must always use this operator otherwise unexpected result may occur.
Example:
scanf(“%d”,&a);
C FUNDAMENTALS
printf(“control string”,variable1,variable2,……..,variablen);
Example:
printf(“%d”,a);
C FUNDAMENTALS
QUESTIONS
1.Write a C program to read an integer number from input console and print its
value in the output console
OPERATORS
An operator is a symbol that tells the computer to perform certain mathematical
and logical manipulations.
Based on number of operands operators are classified into 3
1. Unary : An operator is act on one operand is known as unary operator
Eg: unary +,unary -,++,--
2. Binary : An operator is act on two operand is known as binary operator
Eg: +, -,*,/,%
3.Ternary: An operator is act on three operands is known as ternary operator
(conditional operator)
Eg: ?:
C FUNDEMENTALS
OPERATORS
1. Arithmetic operators
The operator which are use to perform some basic operation e.g
addition,subtraction,multiplication and division,are called arithmetic
operators.
C FUNDEMENTALS
2.Relational operators
We often compare 2 quantities, it can be done with the relational operators
C FUNDEMENTALS
3.Logical operators
An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false.
C FUNDEMENTALS
4.Assignment operators(=)
Assignment operators are used to assigning value to a variable. The left
side operand of the assignment operator is a variable and right side operand of
the assignment operator is a value.
Syntax: Variable=expression
C FUNDEMENTALS
5.Bitwise operators
Used to manipulate data at bit level. They may not be applies on float or
double
C FUNDEMENTALS
6.Conditional operator(?:)
These operators are used to perform logical operations on the given two
variables. ... Conditional operators return one value if condition is true and
returns another value is condition is false.
Syntax:
Example2:
int x=100,y=20 ;
(x>y)?printf(“hai”):printf(“hello”); output: hai
Questions
1. Write a program to enter 2 numbers and find out the largest among them by
using conditional operator.
2. Write a program to check whether the given number is odd or even using
conditional operator
C FUNDEMENTALS
Questions
1. Write a program to enter 2 numbers and find out the largest among them by
using conditional operator.
//(a>b)?printf(“%d”,a):printf(“%d”,b);
C FUNDEMENTALS
Questions
2.Write a program to check whether the given number is odd or even using
conditional operator
#include<stdio.h>
void main()
{
int a;
printf("enter the number");
scanf("%d",&a);
(a%2==0)?printf("even"):printf("odd");
getch();
}
C FUNDEMENTALS
Questions
3.Write a program to check whether the given number is positive,negative or
zero using conditional operator
#include<stdio.h>
void main()
{
int a;
clrscr();
printf("enter the number");
scanf("%d",&a);
(a==0)?printf("zero"):((a>0)?printf("positive"):printf("negative"));
getch();
}
C FUNDEMENTALS
num
10 10
ans
10 11
num 11
11
HOME WORK
1.
2. a=5 b=10
b=a++ - --b what will be the value of b?
C FUNDEMENTALS
8. Special operators
C FUNDEMENTALS
C FUNDEMENTALS
C FUNDEMENTALS
PRECEDENCE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C FUNDEMENTALS
Let a = 2, b = 3, c = 7 and x = 5
Evaluate y = ( a * x * x ) + ( b * x ) + c;
C FUNDEMENTALS
Home work
Let a=9,b=12,c=3 evaluate the expressions
a. d=a-b/3+c*2-1
b. e=a-b/(3+c)*(2-1)
c. f=a-(b/3+c)*2)-1
C FUNDEMENTALS
EXPRESSIONS
An expression is a combination of variables ,operators and constants arranged as per syntax
Every expression in C results some value of certain type that can be assigned to a variable.
Example:
C FUNDEMENTALS
STATEMENTS
C FUNDEMENTALS
C FUNDEMENTALS
C FUNDEMENTALS
C FUNDEMENTALS
C FUNDEMENTALS
C FUNDEMENTALS
C FUNDEMENTALS
There are several standard library functions available under this category those which can deal
with a single character and those which can deal with a string of characters.
• getchar( ) Function
This function is used to read a single character. It is an unformatted input function. Its is found in
the header file <stdio.h>
Syntax :
variable = getchar();
• gets( ) Function
gets() Function is used to read a string i.e. group of characters; from the keyboard.
gets() is needed because scanf() discards the remaining string after a blank space where as
gets() is useful for inputting multiple lines statements.
Syntax:
char array-variable;
gets(array-variable);
C FUNDEMENTALS
• getche() Function
This function is also used to read a single character. It is also an uformatted input
function. It is found in the header file <conio.h>
Syntax:
variable = getche();
• getch() Function
This function is used to read an unformatted input funtion. Its prototype is found in
the header file <conio.h>
Syntax:
variable = getch();
C FUNDEMENTALS
• puts()Function
puts() function is used to print the whole string including blank spaces.
Syntax:
puts(array-variable);
puts(str);