0% found this document useful (0 votes)
7 views23 pages

Introduction

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

Programming for Problem

Solving

Topic: C Programming Basics


By: Dr. Monisha Devi
Dept. of CSE

DR. MONISHA DEVI


Introduction:
• Identifier : used to identify variables, functions, arrays, structures
etc. Has letters, numbers and underscore.
• Keywords: words cannot be used as identifier. Have predefined
meanings. Eg: if, else, for, while, int, float etc.
• Variable: Name given to memory location which stores some form
of data. The stored value in a variable can be changed during
program execution. All variables must be declared with proper
datatype before assigning value.

DR. MONISHA DEVI


Variable declaration:
• In C all variables are declared before they are used.
• This is so that:
– A memory location is given a name. Datatype No. of bytes
– A suitable number of bytes can be allocated. char 1
– The compiler knows how to treat the data. int 2 or 4
short int 2
• There are several data types in 'C’. long int 4
float 4
• The syntax for declaring a variable is as follows:
double 8
datatype v1, v2, ……., vn;
v1, v2, …, vn are the names of variables. Variables are separated by
commas. A declaration statement must end with a semicolon (;).
• Example:
int count;
DR. MONISHA DEVI
double ratio;
Variable declaration:
Variable names must obey the following rules:
1. Names can consist of letters, digits, "_“ (underscore).
2. Names must start with a letter.
3. Names can start with the "_", underscore character but this is
not recommended as many system macros and functions are
given names in this format.
4. Case is significant i.e. C is Case Sensitive, eg. Xyz is not the
same as xyz
5. Names must not clash with the C reserved words.

DR. MONISHA DEVI


Variable Initialization:
• Initialize a variable in c is to assign it a starting value.
• C does not initialize variables automatically. So if you do not
initialize them properly, you can get unexpected results.
• We can initialize variables during declaration.

• Example:
int width, height = 20;
char value = ‘F’;
float base;
width = 10;
base = 30.65;

DR. MONISHA DEVI


Constants:
• A variable that cannot be modified once it is declared in the
program. We can not make any change in the value of the
constant variables.
• Types:
– Numeric constants
– Character constants
• Numeric constants are of Integer type and Floating type.
Integer type constants are further classified as Decimal, Octal
and Hexadecimal.
Decimal: Base 10, Digits are: 0 – 9
Octal: Base 8, Digits are: 0 – 7
Hex: Base 16, Digits are: 0 – 9, A, B, C, D, E, F
• Examples of Integer Constant:

DR. MONISHA DEVI


426 , 786 , -34 (decimal)
037, 0345, 0661 (octal)
0X2, 0X9F, 0XA6B (hexadecimal)
Constants:
• Floating constant values include fractional numbers.
• Single character constant contains a single character enclosed
within a pair of single quote marks.
For ex. ‘A’ , ‘5’ , ’ ’ , ‘ , ’
Note that the character constant’5’ is not same as the number 5. The last
constant is a blank space. Character constant has integer values known as
ASCII values. For example, the statement:
printf(“%d”, a); would print the number 97, the ASCII value of the letter a.
Similarly, the statement printf(“%c”, 97); would output the letter ‘a’
• String constants: A string constant is a sequence of character
enclosed in double quotes. the characters may be letters, numbers,
special characters and blank space.
Examples are:
DR. MONISHA DEVI
“HELLO!”
“1979”
Structure of C program:
• Documentation section:
This section consists of a set of comment lines
Ex:- /* …………… */

• Link section:
Includes headers files. It provides
instructions to the compiler to link functions
from the system library.
Ex:- # include<stdio.h>
# include<conio.h>

• Definition section:
Defines all symbolic constants
Ex:- # define A 10.

• Global declaration section:


Variables which can be accessed throughout the program are called global
DR. MONISHA DEVI
variables and declared outside of all the functions. This section declares global
variables and all the user-defined functions.
Structure of C program:
• Main function:
Every C program must have one main ( ) function section. This contains two parts.
▪ Declaration part: This part declares all the variables used in the executable
part. There may be no statement in declaration part of some programs.
Ex:- int a, b;
▪ Executable part: This part contains at least one statement. These two parts
must appear between the opening and closing braces. The program execution
begins at the opening brace and ends at the closing brace. All the statements
in the declaration and executable parts end with a semicolon (;).

• Sub program section:


This section contains all the user-defined functions, that are called in the main ()
function. User- defined functions generally places immediately after the main()
function, although they may appear in any order.

DR. MONISHA DEVI


C program Execution:

DR. MONISHA DEVI


Operators:
• An operator is a symbol performs certain mathematical or
logical manipulations. Operators are used in programs to
manipulate data variables.
• Types are:
– Arithmetic Operators
– Relational Operators
– Logical Operators
– Assignment Operators
– Increment and decrement operators
– sizeof() operators
– Bitwise Operators
– Ternary operator
DR. MONISHA DEVI
Arithmetic operators:
• The arithmetic operators are:

• Here a and b are operands, assign values for a=14 and b=4 we
have the following results
a-b = 10
a+b = 18
a*b = 56
a/b = 3 (quotient)
a%b = 2 (remainder)
DR. MONISHA DEVI
Relational operators:
• Relational operators are used for comparing two quantities.
• An expression containing a relational operator is termed as a
relational expression.
• The value of a relational expression is either 1 or 0. It is 1 if the
relation is true and 0 if the relation is false.

• When arithmetic expression are used on either side of a relational


operator, the arithmetic expression will be evaluated first and then
the results compared, that means arithmetic operators have a
higher priority over relational operators. Eg.
DR. MONISHA DEVI
Logical operators:
• C has 3 logical operators. The logical operators are used when
we want to test more than one condition and make decisions.

• The logical operators && and || are used when we test more
than one condition and make decisions.
• != is a unary operator which tests one condition.

DR. MONISHA DEVI


Assignment operators:
• These operators are used to assign the result of an expression
to a variable. The usual assignment operator is ‘=’
• ‘+=’: This operator is the combination of the ‘+’ and ‘=’
operators. This operator first adds the current value of the
variable on left to the value on the right and then assigns the
result to the variable on the left.

(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.

• Similarly other are:


-= , *= , /=
DR. MONISHA DEVI
Increment and Decrement operators:
• ++ and - - are increment and decrement operators in C. The
operator ++ adds 1 to the operand, while - - subtracts 1.both
are unary operators.
Ex:- m=5;
Y=++m; the value of y=6 and m =6.
If we write the above statement as
m=5; y= m++; the value of y=5 and m=6.

• A prefix operator (++a) first adds 1 to the operand and then


the result is assigned to the variable on left. On the other
hand, a postfix operator (a++) first assigns the value to the
variable on left and then increments the operand.
DR. MONISHA DEVI
sizeof() operator:
• It is a compile-time unary operator which can be used to
compute the size of its operand.
• The output can be different on different machines like a 32-bit
system can show different output while a 64-bit system can
show different of same data types.
• Eg.
int a =10;
float b = 2.34;
printf(“%d”, sizeof(a)); -> Output is 2 bytes (in some machines 4 bytes)
printf(“%d”, sizeof(a+b)); -> Output is 4 bytes

DR. MONISHA DEVI


Ternary or Conditional operator:
• This operator has three operands.
• We use the ternary operator in C to run one code when the
condition is true and another code when the condition is false.
• Syntax is:
testCondition ? Expression1 : Expression2

The testCondition is a boolean expression that results in either true or false.


If the condition is
o true - Expression1 (before the colon) is executed
o false - Expression2 (after the colon) is executed

Eg: a = 5 > 2 ? 10 + 20 : 10 - 20
Here testCondition is 5 > 2 which is True, so Expression 1 which is 10 + 20
executes and the output is 30
DR. MONISHA DEVI
Bitwise operator:
• Bitwise operators are used for manipulation of data at bit level.
• Bitwise operators may not be applied to float or double.

• Bitwise AND (&): The output is 1 if the corresponding bits of


two operands is 1. If either bit of an operand is 0, the result of
corresponding bit is evaluated to 0.

DR. MONISHA DEVI


Bitwise operator:
• Bitwise OR (|): The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1.

• Bitwise XOR (^): The result of bitwise XOR operator is 1 if the


corresponding bits of two operands are opposite, i.e. only of
the operand is 1 and other has to be 0.

DR. MONISHA DEVI


Bitwise operator:
• Bitwise complement (~): It is a unary operator (works on only
one operand). It changes 1 to 0 and 0 to 1.
• Right Shift operator (>>): Right shift operator shifts all bits
towards right by certain number of specified bits.

• Left Shift operator (<<): It shifts all bits towards left by a


certain number of specified bits. The bit positions that have
been vacated by the left shift operator are filled with 0.

DR. MONISHA DEVI


Operator Precedence:

DR. MONISHA DEVI


THANK YOU

DR. MONISHA DEVI

You might also like