Software and Systems Engineering Standards: Verification and Validation of Sy...
First c program
1. First C Program
Write a program to print hello on output screen.
// Program to print Hello on output screen
#include<stdio.h>
#include<conio.h>
main()
{
clrscr(); //clear output screen
printf(“n Hello!!!”); //print hello
getch(); //remain on o/p screen till user enters
any key
Save above program as hello.c
Explanation :
1. First line is comment.
2. “stdio.h” is a standard input output header file which contains function like printf and scanf
which we are going to use in our program. As we are using these functions we need to include this
header file. # include is preprocessor directive which allows to include header files.
3. “conio.h” is console input output header file which contains function like clrscr , getch
4. printf()
Syntax :
printf(“ string”);
This function is used to print any messge on output screen.
In above program “n” is newline character which brings cursor on next line on output screen.
Whatever we write in double quotes (“ ”) will exactly get printed on output screen.
Note: Every C statements (instruction) ends with semicolon(;)
2. Data Types Used in 'C'
Data type : It is a type of data which we want to use in our program . It can be of Integer ,
Real or character constanats.
Before using variable in C , we must declare it. To declare a variable we need
data type.
Data Type Storage Size Range Format
Specifier
char 1 byte -128 to 127 (for signed data)
0 to 255 (unsigned data)
%c
unsigned
char
1 byte 0 to 255 %c
signed
char
1 byte -128 to 127 %c
int 2 or 4
bytes
-32,768 to 32,767 or
-2,147,483,648 to
2,147,483,647
%d
unsigned
int
2 or 4
bytes
0 to 65,535 or 0 to
4,294,967,295
%u
short 2 bytes -32,768 to 32,767 %d
unsigned
short
2 bytes 0 to 65,535 %u
long 4 bytes -2,147,483,648 to
2,147,483,647
%ld
unsigned
long
4 bytes 0 to 4,294,967,295 %lu
float 4 byte 1.2E-38 to 3.4E+38 %f
double 8 byte 2.3E-308 to 1.7E+308 %lf
long
double
10 byte 3.4E-4932 to 1.1E+4932 %Lf
Data Types
Signed (+ve or -ve) Unsigned (always +ve)
3. // program to take a number as input through keyboard and display it on output screen
#include<stdio.h>
#include<conio.h>
main()
{
int num; // declaration of a variable
clrscr(); //clear output screen
printf(“n Enter any number : ”); // ask user to enter number
on o/p screen
scanf(“%d”, &num); // store entered number in variable num
printf(“n You have entered %d”, num); //retrive number
from memory location and display on screen
getch(); //remain on o/p screen till user enters any key
}
Explaination: In above program, num is a variable of type int. Using function scanf value of
variable num is stored in memory.
Syntax for scanf :
scanf(“format specifief” , &variable name);
Here , format specifier is %d for int , % f for float , %c for char (As given in
above Data type table) , & is address operator which stores data at given memory location.
e.g in above program num is a variable , whatever value we print on o/p screen that value is stored
in memory location called num.
Then using printf(); that value we are printing on output screen. So, another syntax for printf is
printf(“string + format specifiers”, list of variables);
e.g printf(“n You have entered %d”, num); in this case %d referes to variable
num. Here value of variable num is retrived from memory and printed on output screen.
4. Arithmetic Operators in 'C'
Arithmentic Operators :
Operator Description Example
Add + Add two variables (operands) a+b
Subtract - Subtract two variables (operands) a-b
Multiply * Multiply two variables (operands) a*b
Division / Divide two variables (operands) this operator
gives Quotient as a result of division
a/b
Modulus % Divide two variables (operands) this operator
gives Remainder as a result of division
a%d
Equal to
=
Assign value to variable on Left Hand Side
(LHS)
c= a+b
// Program using printf and scanf functions
//WAP to add two numbers and display result on output screen.
// program to add two number and display result on output screen
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c; // declaration of a variable
clrscr(); //clear output screen
printf(“n Enter two numbers : ”); // ask user to enter number
on o/p screen
scanf(“%d%d”, &a,&b); // store numbers in variable a and b
c= a+b; //addition of a and b
printf(“n Addition = %d”, c); //print addition
getch(); //remain on o/p screen till user enters any key
}