C Programming Language
C Programming Language
Language
Introduction
C is a general-purpose computer
programming language
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
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
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:
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
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
++a or a++
--a or a--
Examples for ++ & --
operators
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
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.
3 #include <stdio.h>
5 int main()
6 {
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" );
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}