0% found this document useful (0 votes)
16 views51 pages

C Programming Language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views51 pages

C Programming Language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

C Programming

Language
Introduction
C is a general-purpose computer
programming language

It is developed in 1972 by Dennis


Ritchie at the Bell Telephone
Laboratories .

A Programming language (PL) is a


set of instructions used to communicate
with the computer.
The set of instruction written in a PL is
known as “program”

All PL language can be divided into


three categories

 Problem Oriented or High Level


Languages
 Machine Oriented or Low Level
Languages
 Middle Level Language
Language Translators

These are special programs, which


accepts the user program and
convert to corresponding machine
language instructions

There are two types of translators


Compilers
Interpreters
Structure of C program
Include files
Global variable and
function declaration

Main functions

Function subprogram
A SIMPLE C PROGRAM

#include<stdio.h>
main( )
{
printf("Programming in C is
easy.\n");
}

Output:
Programming in C is easy.
A NOTE ABOUT C
PROGRAMS
C is Case sensitive

The C programs starting point is


identified by the word main( )
 execution commences from
main().

The two braces, { and }, signify the


begin and end segments of the
program
#include <stdio.h> is to allow
the use of the printf statement
to provide program output

printf() is actually a function in


C that is used for printing
variables and text

All C statements must be


terminated by a semi-colon ( ; )
The C compilation model

Executable
Code
Comment
Comment should be enclosed
between /* */
It is used to increase the readability of
the program.
number of comments can be given at
any place in the program.
Comment cannot be nested.
It can be split over more than one line
Example:
#include<stdio.h> /*include information about
standard library */
/* program to print a message*/
main( ) /* define a function named main
that receives no argument values */
{
printf(“welcomes\n”);
/*main calls library function printf to print this sequence
of characters ; \n represents the newline character*/
}
STEPS IN LEARNING C

Alphabets
Digits Constants
Special-symbols Variables
Keywords

Instruction Program
The C character Set
 A character denotes any
alphabet, digit or special symbol
used to represent information.
Alphabets A,B, …. ,Y, Z
a,b, ….. ,y, z

Digits 0,1,2,3,4,5,6,7,8,9

Special Symbols ~‘!$@#%^&*( )_-+=


|\{}[] :;“‘< > , .? /
Constants, Variable and
keywords

The alphabets, numbers and special


symbol when properly combined form
constants, variables and keywords.

 A constant is a quantity that doesn’t


change

A variable is a name given to the location


in memory where the constant is stored.
Types of C Constants
C constants can be divided into two
major categories
 Primary Constants
 Secondary Constants
C Constants

Primary Constants Secondary constants

Integer Constant Array, Pointer


Real Constant Structure, Union
Character Constant Enum
Integer Constants

An integer constant must have at least


one digit
It must not have a decimal point
It could be either positive or negative
If no sign precedes an integer
constant, it is assumed to be positive
ex., 123, -321, +78
No commas or blanks are allowed
within an integer constant
Real Constants

Real constants(RC) must have at


least one digit
It must have a decimal point
It could be either positive or negative
Default sign is positive
No commas or blank are allowed
within a RC
Ex., 0.0083, -0.75, +247.0
Single Character
Constants

A Single character constant is either a


single alphabet, a single digit or a
single special symbol enclosed within
single inverted commas
The maximum length of a character
constant can be 1 character
Eg: ‘a’, ‘1’, ‘5’, ‘=‘ (Valid)
‘asd’, ‘12’ (Invalid)
String Constants
It is a sequence of characters
enclosed in double quotes.
Characters may be letters, numbers,
special characters enclosed in double
quotes.
Ex.,
“Hello!” “1987” “?...!”
“X”
Identifiers
Identifiers are names given to various
program elements, such as variables,
functions and arrays

A variable name is any combination of


alphabets, digits or underscores

The first character in the variable name


must be an alphabet
Keywords
Keywords are the reserved words
They cannot be used as variable
names.
There are
auto only 32
double if keywords
static available
do
break else int struct goto
in
case c enum long switch signed
char extern near typedef while
const float register union default
continue far return unsigned for
short void
C Instructions
 There are basically four types of
instructions in C
 Type declaration instruction
Eg: int sum;
float ave;
char name,code;

 Input/Output instruction
Eg: scanf(),printf()
Arithmetic instruction

Eg : x = sum +2;

Control instruction
Eg :
while do statement
for statement
if statement
Data Types
C Supports several different types of data,
each of which may be represented
differently within the computers memory.
Basic data types are listed below:

Data Type Description Typical Memory

int integer quantity 2 bytes


char single character 1 bytes
float floating point number 4 bytes
double double-precision 8 bytes
Escape Sequences in C
Certain non printing characters can be
expressed in terms of escape sequences

bell \a 007
backspace \b 008
horizontal tab \t 009
vertical tab \v 001
newline \n 010
formfeed \f 012
carriage return \r 013
quotation mark (“) \” 034
question mark(?) \? 063
backslash (\) \\ 092
null \0 000
Storage Classes
Auto – local variable declared within a function

Static – local variable which exist and retain


value even after control is transferred to calling
function

Extern – Global variable known to all function

Register – Local Variable which is stored in the


register

ex: auto int count;


Storage Class
Static and external variables
are automatically initialized to
zero.
Automatic (auto) variables
contains undefined values
called garbage unless they are
initialized.
Operators
An operator is a symbol (+,-,*,/)
that directs the computer to
perform certain mathematical or
logical manipulations and is
usually used to manipulate data
and variables.

Ex: a+b, 10+20


Operators in C
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Arithmetic
operators

Operator Example
Meaning
+ a+b Addition
- a–b
Subtraction
* a*b
Multiplication
/ a/b Division
% a%b Modulo division-
remainder
Relational Operators
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or
equal to
== Equal to
!= Not equal to
Logical Operators

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
 Logical expression or a compound
relational expression
 An expression that combines two or
more relational expressions
Ex: if (a==b && b==c)
Truth Table
a b a a || b !a
&&b
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Assignment operators
Syntax: v op = exp;
Where v - variable,
op = shorthand assignment
operator
exp - expression
Ex: x=x+3
x+=3
Shorthand Assignment
operators
Simple assignment operator
Shorthand operator
 a = a+1 a + =1
 a = a-1 a - =1
 a = a* (m+n) a * = m+n
 a = a / (m+n) a / = m+n
 a = a %b a %=b
Increment & Decrement
Operators

C supports 2 useful operators namely


1. Increment ++
2. Decrement -- operators
 The ++ operator adds a value 1 to the
operand
 The – operator subtracts 1 from the
operand

++a or a++
--a or a--
Examples for ++ & --
operators

Let the value of a =5 and b=++a then a


= b =6
Let the value of a = 5 and b=a++ then a
=5 but b=6

i.e.:
1. a prefix operator first adds 1 to the
operand and then the result is assigned to
the variable on the left
2. a postfix operator first assigns the value to
the variable on left and then increments the
operand.
Conditional operators
Syntax: exp1 ? exp2 : exp3
 Where exp1,exp2 and exp3 are expressions

Working of the ? Operator:


 Exp1 is evaluated first, if it is nonzero(1/true) then
the expression2 is evaluated and this becomes the
value of the expression,
 If exp1 is false(0/zero) exp3 is evaluated and its
value becomes the value of the expression
Ex: m=2;
n=3
r=(m>n) ? m : n;
Bitwise operators

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive
OR
<< Shift left
>> Shift right
Special operators

1. Comma operator ( ,)
2. sizeof operator – sizeof( )
3. Pointer operators – ( & and *)
4. Member selection operators – ( .
and ->)
Arithmetic Expressions

Algebraic expression C
expression

axb-c a*b-c

(m+n)(x+y) (m+n)*(x+y)

3x2+2x+1 3*x*x+2*x+1
Input and Output
One of the essential operations
performed in a C language programs is
to provide input values to the program
and output the data produced by the
program to a standard output device.
scanf, which can be used to read data
from a keyboard.
printf, which sends results out to a
terminal.
Each program that uses standard input /
out put function must contain the
statement.

#include<stdio.h> at the beginning.

Single character input output:


getchar()
putchar()
String input and output:
gets
puts
COMMONLY USED SCANF
FORMAT CODES

CODE CODE MEANING


%C Reads a single
character
%f Read a floating point
%s
value
%x Read a string
%o Read a hexadecimal
integer
Read an octal integer
%d Read an integer
Printing One Line

#include < stdio.h >


main ( )
{
printf (“Hello!”);
printf (“Welcome to all!”);
}
Simple Program
1 /* Program1.c
2 A first program in C */

3 #include <stdio.h>

5 int main()

6 {

7 printf( "Welcome to C!\n" );

9 return 0;

10}
Comments
Text surrounded by /* and */ is
ignored by computer
Used to describe program
#include <stdio.h>
Preprocessor directive
Tells computer to load contents of a
certain file
<stdio.h> allows standard
input/output operations
Simple Program
int main()
C programs contain one or more
functions, exactly one of which must be
main
Parenthesis used to indicate a function
int means that main "returns" an
integer value
Braces ({ and }) indicate a block
The bodies of all functions must be
contained in braces
printf( "Welcome to C!\n" );

Instructs computer to perform an action


Specifically, prints the string of
characters within quotes (“ ”)
Entire line called a statement
All statements must end with a
semicolon (;)
Escape character (\) Indicates that printf
should do something out of the ordinary
\n is the newline character
return 0;
A way to exit a function
return 0, in this case, means that the
program terminated normally

Right brace }
Indicates end of main has been
reached
Addition of Two
Numbers
1 /* Progrm2.c
2 Addition program */
3#include <stdio.h>
4
5 int main()
6 {
7 int integer1, integer2, sum; /* declaration */
8
9 printf( "Enter first integer\n" ); /* prompt */
10 scanf( "%d", &integer1 ); /* read an integer */
11 printf( "Enter second integer\n" ); /* prompt */
12 scanf( "%d", &integer2 ); /* read an integer */
13 sum = integer1 + integer2; /* assignment of sum */
14 printf( "Sum is %d\n", sum ); /* print sum */
15
16 return 0; /* indicate that program ended successfully */
17}

You might also like