Learning C/C++ Step-By-Step - Page 3
03. Step-by-Step C/C++ --- C Programming - Basic IO Statements
Contents
- Structure of a C program
- I/O Statements
- Printf
- Escape Characters
- Using Variables in programs
- Scanf
- More IO Statements
- gets
- puts
- getch
- putch
- getche
- getchar
As discussed, every program is a set of statements, and statement is an instruction to the computer, which is a collection of constants, variables, operators and statements.
Structure of a C program
<return type> main( arg-list )
{
<declaration part>
<Statement block>
<Return Values >
}
We are going to start with Input / Output Statements as they play important roles in our further programs.
I/O Statements
Printf
This statement displays the given literal / prompt / identifiers on the screen with the given format.
Syntax:
printf(<"prompt/literal/format id/esc char. ">, id1,id2, .....);
E.g.:
printf("Hello");
printf("Student number : %d", sno);
printf("Student name : %s", name);
printf("3Subjects Marks : %d, %d, %d", m1, m2, m3);
1. Program to print a message:
/* 02_print.c */
#include <stdio.h>
int main( )
{
printf("Hello");
return 0;
}
Escape Characters
Common Escape Sequences
Escape Sequence |
Character |
\a |
Bell(beep) |
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Return |
\t |
Tab |
\\ |
Backslash |
\’ |
Single quotation mark |
\” |
Double quotation marks |
\xdd |
Hexadecimal representation |
2. Program to print a message in a new line
- Compare with the last program.
/* 03_esc.c */
#include <stdio.h>
int main()
{
printf("\nHello");
return 0;
}
3. Program to display address of a person
- Multiple statements in main
/* 04_multi.c */
#include <stdio.h>
int main()
{
printf("\nName of the Person");
printf("\nStreet, Apartment//House No. ");
printf("\nzip, City");
printf("\nCountry");
return 0;
}
Using Variables in programs
Basic Variable Types
Keyword |
Range: low |
Range: high |
Digits of precision |
Bytes of memory |
Format-ID |
Char |
-128 |
127 |
n/a |
1 |
%c |
Int |
-32, 768 |
32, 767 |
N/a |
2 |
%d |
Long |
-2,147, 483, 648 |
2, 147, 483, 647 |
N/a |
4 |
%ld |
Float |
3.4 x 10-38 |
3.4 x 1038 |
7 |
4 |
%f |
Double |
1.7 x 10-308 |
1.7 x 10308 |
15 |
8 |
%lf |
long double |
3.4 x 10-4932 |
3.4 x 10-4932 |
19 |
10 |
%Lf |
4. Program to find the sum of two values
- Variables are introduced in this program
/* 05_var.c */
#include <stdio.h>
int main()
{
int a , b , c;
a = 5;
b = 10;
c = a + b;
printf("%d", c);
return 0;
}
5. Program to find the sum of two values with message
- Compare with the last program
#include <stdio.h>
int main()
{
int a, b, c;
a = 5;
b = 10;
c = a + b;
printf("\nSum is %d", c);
/* We have inserted extra text before printing the value*/
return 0;
}
Scanf
Using this statement we can accept and values to variables during the execution of the program.
Syntax:
scanf(<format id/esc char">, id1,id2, .....);
Eg.
scanf("%d", &sno);
scanf("%s", name);
scanf("%d%d%d", &m1, &m2, &m3);
6. Program to find the sum of two value using scanf
- When you run the program it shows you the cursor and waits for your input, enter a numeric value and press "Return", do this twice and you will get the output.
/* 07_scanf.c */
#include <stdio.h>
int main()
{
int a , b, c; scanf("%d", &a);
scanf("%d", &b);
c = a + b;
printf("\nSum is %d", c);
return 0;
}
More Excercises:
7. Program to find the sum of two values with message display
- Messages are optional but introduces user-friendly interaction
- Compare with the last program
/* 08_sum.c */
#include <stdio.h>
int main()
{
int a , b, c; printf("Enter A value "); scanf("%d", &a);
printf("Enter B value "); scanf("%d", &b);
c = a + b;
printf("\nSum is %d", c);
return 0;
}
8. Program to find the result of ( a+ b )2
- Similar to sum of two values program but the formulae is different
/* 09_formula.c */
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter A value "); scanf("%d", &a);
printf("Enter B value "); scanf("%d", &b);
c = a * a + b * b + 2 * a * b;
printf("Result is %d", c);
return 0;
}
9. Program to find the annual salary of an employee
- input : eno, name, sal
- Process : Asal = sal * 12
- Output : Eno, name, sal, asal
- This program introduces the different types of variable
/* 10_emp.c */
#include <stdio.h>
int main()
{
int eno;
char name[10]; /* name with 10 characters width */
float sal, asal; /* sal & asal as real values */
printf("Enter Employee number "); scanf("%d", &eno);
printf("Enter Employee name "); scanf("%s", name);
printf("Enter Employee salary "); scanf("%f", &sal);
asal = sal * 12;
printf("\nEmployee number %d", eno);
printf("\nEmployee name %s", name);
printf("\nEmployee salary %f", sal);
printf("\nAnnual Salary %f", asal);
return 0;
}
10. Write a program to find the total and average marks of a student
- Input : Sno, name, sub1, sub2, sub3
- process : total = sub1 + sub2 + sub3; avg = total / 3
- output : sno, name, total, avg
- Similar to the above program just accept, process, and print the values
/* 11_stud.c */
#include <stdio.h>
int main()
{
int sno, sub1, sub2, sub3, total;
char name[10];
float avg;
clrscr(); /* clear the screen before its output */
printf("Enter Student number "); scanf("%d", &sno);
printf("Enter Student name "); scanf("%s", name);
printf("Enter Subject1 marks "); scanf("%d", &sub1);
printf("Enter Subject2 marks "); scanf("%d", &sub2);
printf("Enter Subject3 marks "); scanf("%d", &sub3);
total = sub1 + sub2 + sub3;
avtg = total / 3;
printf("\nStudent number %d", sno);
printf("\nStudent name %s", name);
printf("\nTotal marks %d", total);
printf("\nAverage marks %f" , avg);
return 0;
}
More IO Statements
Gets:
To accept a string from the key board. It accepts string value up to the carriage return.
Syntax:
gets( <id.> );
E.g.:
gets(name);
gets(street);
puts
It displays the given string value on the screen.
Syntax:
puts( <id.> / <“prompt”>);
E.g.:
puts(name);
puts(street);
getch - Read char without echo
getche - read char with echo
getchar - read char and accept carriage return
putch
It can print a character on the screen.
Syntax:
putch(<char>).
E.g.:
putch(‘a’);
putch(65);
getch
It accepts a character from console.
Syntax:
char = getch().
E.g.:
ch = getch();
option = getch();