23ECE105 COMPUTER PROGRAMMING
Prepared by,
Mrs. SUMITHRA R. P.
UNIT I
Introduction, structure of C program, data types, storage classes, constants,
enumeration constant, keywords, variables, operators, expressions, input/output
statements, assignment statement conditional statements;
Number system: binary, decimal, hexadecimal, conversion between number system
types;
Introduction to tools – IDE, compilation, linking, debugging.
Expressions
An expression in C consists of a syntactically valid
combination of operators and operands that
computes to a value.
Arithmetic Expressions : 6*2/( 2+1 * 2/3 +6) +8 * (8/4)
relational expression : x%2 = = 0
logical expression : ( x > 4 ) && ( x < 6 )
Input/Output Statements
The input/ output functions fall into two categories:
❑ non-formatted read (input) and display (output) functions and
❑ formatted read (input) and display (output) functions.
unformatted I/O
The following unformatted I/O functions:
1. getch()
2. getche()
3. getchar()
4. gets()
5. puts()
6. putchar()
7. putch()
formatted I/O
formatted I/O functions:
1. printf()
2. scanf()
Non-formatted input and output
Single Character Input
❑ getchar()
is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this
function reads one character at one time until and unless the
enter key is pressed.
char_variable = getchar();
#include <stdio.h>
int main()
{
char ch;
printf("Enter the character: ");
ch = getchar();
printf("%c", ch);
return 0;
}
Output:
Enter the character: abcde
a
getch()
This input function reads, without echoing on the screen, a
single character from the keyboard and immediately
returns that character to the program.
General statement form:
ch = getch();
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
// Reads a character but
// not displays
getch();
return 0;
}
getche()
This input function reads, with echo on the
screen, a single character from the keyboard
and immediately returns that character to the
program.
General statement form:
ch = getche();
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
// Reads a character but
// not displays
getche();
return 0;
}
gets()
int main()
{
char str[150];
printf("Enter a string");
gets(str);
printf("The string was :%s\n", str);
return 0;
}
Single Character Output
❑ putchar() is an output function that writes a single character on the
standard output device, the display screen.
int putchar(int c);
//function of character output
❑ putch() This output function writes the character directly to the screen. On
success, the function putch() returns the character printed. On error, it
returns EOF.
General statement form:
putch(ch);
#include <stdio.h>;
int main()
{
// Get the character to be written
char ch = 'G';
// Write the Character to stdout
putchar(ch);
return (0);de
}
puts()
#include <stdio.h>
int main()
{
char s[]=“Hello, World”;
puts(s);
return 0;
}
Example
#include<stdio.h> Input: a
int main(void) Output: b
{
int ch;
ch=getchar();
ch=ch++;
putchar(ch);
return 0;
}
FORMATTED INPUT AND OUTPUT
FUNCTIONS
When input and output is required in a specified format the
standard library functions scanf() and printf() are used.
The scanf() function allows the user to input data in a
specifi ed format. It can accept data of different data types.
The printf() function allows the user to output data of
different data types on the console in a specified format.
Output Function printf ( )
general form of a call to the printf() function is
printf(“control_string”,variable1,variable2,...);
• where the ‘...’ means a list of variables that can be written separated by
commas and this list may be as long as is desired.
• The control string is all-important because it specifies the type of each
variable in the list and how the user wants it printed.
• The control string is also called the format string.
The format string in printf(), enclosed in quotation marks, has three
types of objects:
❑ Ordinary characters: these are copied to output
❑ Conversion specifi er field: denoted by % containing the codes listed in
Table.
❑ Control code: optional control characters such as \n, \b, and \t
printf(“number=%3d\n”, 10);
printf(“number=%2d\n”, 10);
printf(“number=%1d\n”, 10);
printf(“number=%7.2f\n”,
5.4321);
printf(“number=%.2f\n”, 5.4391);
printf(“number=%.9f\n”, 5.4321);
printf(“number=%f\n”, 5.4321);
ex1
ASCII value = 75, Character = K
#include <stdio.h>
ASCII value = 76, Character = L
ASCII value = 77, Character = M
int main () {
ASCII value = 78, Character = N
int ch;
ASCII value = 79, Character = O
ASCII value = 80, Character = P
for( ch = 75 ; ch <= 100; ch++ ) {
ASCII value = 81, Character = Q
printf("ASCII value = %d,
Character = %c\n", ch , ch ); ASCII value = 82, Character = R
} ASCII value = 83, Character = S
return(0);
}
ex2
#include<stdio.h> Output
main() 7
{ 7
int a,b;
007
float c,d;
5.10
a = 15;
b = a / 2;
printf("%d\n",b);
printf("%3d\n",b);
printf("%03d\n",b);
c = 15.3;
d = c / 3;
printf("%3.2f\n",d);
}
Thank you