0% found this document useful (0 votes)
2 views

C Programming Concepts

The document provides an overview of C programming concepts, including algorithms, data types, variables, and operators. It explains the structure of a C program and details various operators such as arithmetic, relational, logical, and bitwise operators. Additionally, it covers the rules for variable names, examples of simple C programs, and the hierarchy of operators.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Programming Concepts

The document provides an overview of C programming concepts, including algorithms, data types, variables, and operators. It explains the structure of a C program and details various operators such as arithmetic, relational, logical, and bitwise operators. Additionally, it covers the rules for variable names, examples of simple C programs, and the hierarchy of operators.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

C Programming

Concepts

By: Devayani Kalkundrikar


Algorithm
 An algorithm is a step by step procedure to solve a given
problem.

 CHARACTERISTICS OF AN ALGORITHM

1. Specify each step or instruction exactly.


There must be no ambiguity.
The instructions must be clear.
2. There must be a finite number of steps.
The algorithm must terminate.
There must be a stopping point.
3. There must be an output.
The algorithm must produce the correct result.
Example
An algorithm to find the area of circle.

step 1: start
step 2: Read radius
step 3: area=3.142* radius* radius
step 4: print “Area of a circle =“, area
step 5: stop
Introduction to C
Programming
 C is a high level programming language developed at AT & T’s
Bell Lab of USA in 1972.
 Evolved by Dennis Ritchie from two previous programming
languages, BCPL and B
 Originally develop to write UNIX Operating System.
 Hardware independent (portable)
 C is a structured programming language.
C-LANGUAGE CHARACTER
SET

Among the characters that a programmer can use are the following:

lowercase letters : a b c ... z


uppercase letters : A B C ... Z
digits : 0 1 2 ... 9
other characters : * / = ( ) { } < > ‘ “! @# $
%&_|^~\. ,;:?
white space characters such as blank, new line, and tab
Data Types

C language data types can be broadly classified


as:

 Primary data type


 Derived data type
 User Defined data types
Primary Data Types
All C compilers supports five fundamental data types:

Integer (int):
Ex: 245,-456, 0 etc.
 Character (char):
Ex: ‘a’ , ‘ p’ , ‘$’ etc.
 Floating point (float):
Ex: -23.45, 00.34, 2.34 etc.
 Double precision floating point (double):
Ex: 234.000000000000000, -0.000000011675
 Void: The void type has no value, the type of a function is said to be
void when it does not return any value to the calling function.
Variables
 Variables are data that will keep on changing

 Declaration:
data_ type variable_name;
Eg: int age;

 Assigning values to variables:


Var_name=value;
Eg: age=20;
Variable names- Rules

 Should not be a reserved word like int etc..


 Should start with a letter or an underscore(_)
 Can contain letters, numbers or underscore.
 No other special characters are allowed including space
 Variable names are case sensitive
• A and a are different.
Basic structure of C
program
The complete structure of C program is:

preprocessor statements
global declaration;
main()
{
declaration;
statements;
}
user-defined functions
A SIMPLE C PROGRAM

The following program prints the phrase “Introduction to C-


Language” on the screen:

#include <stdio.h>
main ()
{
/* printing message */
printf (“Introduction to C-Language\n”);
}
C-Tokens
 Keywords: All keywords are basically the sequence of character
that have fixed meaning.
Eg: auto, case, char, break, int, for, if, else etc.
 Identifiers: Identifiers are the names given to the program
elements such as variables, arrays and functions.
 Constants: Two types of constants in C:

Numeric Constant

Integer Constant

Floating-point Constant

Non-numeric Constant

Character Constant

String Constant
 Special Symbols
 Operators
Operators
Operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations.

C operators are classified as:


 Arithmetic Operator
 Relational Operator
 Logical Operator
 Assignment Operator
 Increment & Decrement Operator
 Conditional Operator
 Bitwise Operator
 Special Operator
Arithmetic Operator
There are five main arithmetic operators in ‘C’:

Operator Meaning

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

x + y = 32
Eg: Let x=27 and y=5 then x – y = 22
x * y = 115
x % y = 2
x / y = 5
Relational
Operator
C supports the following relational operators:

Operator Meaning Eg: Let x=2, y=3, z=4


then
< is less than
x<y true 1
<=
is less than or equal to (x+y) >=z true 1
(y+z)>(x+7) false 0
> is greater than
z!=4
>= is greater than or equal false 0
to y ==3
== is equal to true 1
!= is not equal to
Logical Operator
C has the following logical operators:

Operator Meaning

&& Logical AND


|| Logical OR
! Logical NOT

Eg: Let x=7, y=5.5 , z= 'w‘ then

(x>=6) &&(z= =’w’) true


1
(x>=6)|| (y = =119) true
! (x >= y) true 1
1
Assignment
Operator
 The Assignment Operator evaluates an expression on the right
of the expression and substitutes it to the value or variable on
the left of the expression.

 Syntax: var oper = exp;

where, var is a variable


oper is a C arithmetic operator
exp is an expression

 Example: a+=1
is same as a=a+1
Increment &
Decrement
Operator
 The Increment Operator (++): The increment
operator (++) is a unary operator that increments the
contents of a variable by 1.
For example, the statement

++ age;

increments the contents of the variable age by 1. If age


= 5, then ++age will make age = 6. In other words, +
+age is the same as age = age + 1.

 The Decrement Operator (--): The decrement


operator (--) is a unary operator that decrements the
contents of a variable by 1. In other words, --age or
age-- is the same age = age - 1.
Example
The postfix and prefix notations of the increment and
decrement operators differ when used in expressions.
Examples:

a = ++age;
Assume age = 5. After execution of this statement, a
= 6 and age = 6. In other words, the value of age is
incremented first and then its value is assigned to variable a.

a = age++;
Assume age = 5. After execution of this statement, a
= 5 and age = 6. In other words, the value of age is assigned
first to variable a and then its value is incremented.
Example

#include <stdio.h>
main()
{
int a, b, c, d;
c = 1;
d = 1;
a = ++c;
b = d++;
printf (“The value of a is %d and the value of c is %d.\n”, a, c);
printf (“The value of b is %d and the value of d is %d.\n”, b, d);
}
Conditional
Operator
 There is one conditional operator( ?: ) in ‘C’ language. An
expression that makes use of the conditional operator is
called a conditional expression.

 Syntax:
expression 1 ? expression 2 : expression 3

 Example:
x=(a>b) ? a : b;
Bitwise Operator
There are six bit operators:

Operator Meaning

& Bitwise AND

| Bitwise OR

~ Bitwise NOT

^ Bitwise XOR

<< Left shift

>> Right shift


Special Operator
C supports some special operators,
 The Comma Operator: The comma operator can be
used to link related expressions together.
Example:
value = (x = 10, y = 5, x + y);

 The size of Operator: The operator size of gives the


size of the data type or variable in terms of bytes
occupied in the memory. The operand may be a variable,
a constant or a data type qualifier.
Example:

m = sizeof (int);
Hierarchy of
Operators
 The hierarchy of commonly used operators is shown below:

Priority Operator Description


s
1st */% Multiplication, division, modulus
2nd +- Addition, subtraction
3rd = assignment
Examples:
1. on=link * act / 2 + 3 / 2 * act + 2 + tig;
(where ink=3,act=2 ,tig=3.2,assume on to be an int)

on= 3 * 2 / 2 + 3 / 2 * 2 + 2 + 3.2; operation *


= 6 / 2 +3 / 2 * 2 + 2 + 3.2; operation /
= 3 + 3 / 2 * 2 + 2 +3.2; operation /
= 3 + 1 * 2 + 2 + 3.2; operation *
= 3 + 2 + 2 + 3.2; operation +
= 5 + 2 + 3.2; operation +
= 7 + 3.2; operation +
on=10 operation +
Exercise

Evaluate the following expressions:


1. g=big/2+big*4/big-big+abc/3;
(where abc=1.5, big=3, assume g to be float)

2. s= qui * add / 4 – 6 / 2 + 2 / 3 *6 /god;


(where qui=2, add=4, god= 3, assume s to be int)
Thank you

You might also like