unit 1 c programming
unit 1 c programming
DISADVANTAGE:
⮚ Size of the Program is large
⮚ Difficult to debug and maintain
⮚ Can be used only for small and simple applications.
Global Data
subroutine
Program
Disadvantage:
⮚ No reusability
⮚ Requires more time to write programs
⮚ Programs are difficult to maintain
⮚ Global data is shared and therefore may get altered mistakenly
Global Data
Modules that
have local
data and code
Program
Reema Thareja ―Programming in C, Oxford University Press, Second Edition, 2016
3. Structured Programming Contd…
⮚ Modularization makes a program easier to write, debug and
understand.
⮚ It is always easy to understand 10-single page module instead single 10-
page program.
⮚ In a structured programming the program flow follows a simple sequence
and usually avoids the use of ‘goto’ statements.
⮚ Besides sequential flow structured programming also supports selection
and repetition as mentioned below,
⮚ Selection statement contains the keywords such as ‘if’, ‘then’, ‘end if’ or
‘switch’ that help to identify the order as a logical executable.
⮚ Repetition statement include such as ‘repeat’, ‘for’ or ‘do….until’
which instructs the program as how long it needs to continue.
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}
• Integers are whole numbers that can have both zero, positive and
negative values but no decimal values. For example, 0, -5, 10
• We can use int for declaring an integer variable.
Example :int id;
• Here, id is a variable of type integer.
• You can declare multiple variables at once in C programming. For
example,
int id, age;
• Identifiers are names given to program elements such as variables, arrays and
functions.
Rules for forming identifier name
▪ it cannot include any special characters or punctuation marks (like #, $, ^, ?, .,
etc) except the underscore"_".
▪ There cannot be two successive underscores
▪ Keywords cannot be used as identifiers
▪ The names are case sensitive. So, example, “FIRST” is different from “first”
and “First”.
▪ It must begin with an alphabet or an underscore.
▪ It can be of any reasonable length. Though it should not contain more than 31
characters.
Example: roll_number, marks, name, emp_number, basic_pay, HRA, DA, dept_code
Local: Accessible
within the function
Accessible within Accessible within Accessible within or block in which it
the function or all program files the function or is declared
Accessibility
block in which it is that are a part of block in which it is Global: Accessible
declared the program declared within the program
in which it is
declared
void main()
{
int a=30;
func1(); //function 1 call
func2(); //function 2 call
printf(“\n a = %d”, a);
}
int main()
{ Output:
int a=3, b=5, res;
res=exp(a,b); 3 to the power of 5 = 243
printf(“\n %d to the power of %d=%d”, a, b, res);
return 0;
}
int main()
{
return 0;
}
result = a /
Divide / a / b
b
3
result = a +
Addition + a + b
b
12
Subtracti - a - b
result = a –
6
on b
result = a %
Modulus % a % b
b
0
OPERATOR MEANING
A B A &&B A B A || B
A !A
0 0 0 0 0 0
0 1
0 1 0 0 1 1
1 0 0 1 0
1 0 1
1 1 1 1 1 1
+ - Addition/subtraction left-to-right
|| Logical OR left-to-right
⮚ streams
⮚ printf()
⮚ Scanf()
Keyboard Data
Streams in a C program
Monitor Data
Flags Description
- Left-justify within the data given field width
+ Displays the data with its numeric sign
(either + or -)
# Used to provide additional specifiers like o,
x, X, 0, 0x or 0X for octal and hexa decimal
values respectively for values different than
zero.
0 The number is left-padded with zeroes (0)
instead of spaces
Length Description
h When the argument is a short int or
unsigned short int.
l When the argument is a long int or
unsigned long int for integer specifiers.
L When the argument is a long double (used
for floating point specifiers)
Statement x
Reema Thareja ―Programming in C, Oxford University Press, Second Edition, 2016
if Statement Example
#include<stdio.h>
int main()
{
int x=10;
if ( x>0)
x++;
printf("\n x = %d", x);
return 0;
}
FALSE
SYNTAX OF IF STATEMENT
Test
if (test expression) TRU Expression
{ E
statement_block 1;
} Statement Block 1 Statement Block 2
else
{
statement_block 2;
}
statement x;
Statement x
#include<stdio.h>
main()
{
int a;
printf("\n Enter the value of a : ");
scanf("%d", &a);
if(a%2==0)
printf("\n %d is even", a);
else
printf("\n %d is odd", a);
return 0;
}
Statement x
while (condition)
{
statement_block;
} Update the
Condition
condition
statement x;
expression
TRUE
Statement Block
FALSE
Statement y
#include<stdio.h>
int main()
{
int i = 0;
while(i<=10)
{
printf(“\n %d”, i);
i = i + 1; // condition updated
}
return 0;
}
Statement x;
Statement x
do
{
statement_block;
Statement
}
Block
while (condition);
statement y; Update the condition expression
TRUE
Condition
FALSE
Statement y
#include<stdio.h>
int main()
{
int i = 0;
do
{
printf(“\n %d”, i);
i = i + 1;
} while(i<=10);
return 0;
}
// code given below which print first n numbers using a for loop.
#include<stdio.h>
int main()
{
int i, n;
printf(“\n Enter the value of n :”);
scanf(“%d”, &n);
return 0;
}
Preprocessor Directive
Unconditional Conditional
.
if else elif ifdef ifndef endif
⮚ This variant is used for system header files. When we include a file using
angular brackets, a search is made for the file named filename in a standard
list of system directories.
#include "filename"
⮚ This variant is used for header files of your own program. When we
include a file using double quotes, the preprocessor searches the file named
filename first in the directory containing the current file, then in the quote
directories and then the same directories used for <filename>.
INSTRUCTION DESCRIPTION
• In the first step, the preprocessor program reads the source file as text, and
produces another text file as output.
• Source code lines which begin with the hash symbol are actually not written
in C but in the preprocessor language.
• The output of the preprocessor is a text file which does not contain any
preprocessor statements.
• The linker combines the object file with library routines (supplied with the
compiler) to produce the final executable file.
Reema Thareja ―Programming in C, Oxford University Press, Second Edition, 2016
• Sourc • Pre- • Com • Obje
pro
e File cess piler ct
Files
• Librar
y Files