



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions Data set for C programming
Typology: Essays (university)
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Author: Saurabh Shukla
A program is a set of instructions. Instructions are also known as commands or statements. They are the sentences in program. Just like any other language C also provides variety of instructions.
Types of instructions l Data type declaration instruction l Input/Output instruction l Arithmetic instruction l Control instruction
Data type declaration instruction
From the list of keywords few are designed to use in declaration instruction. They are required to declare variables and functions. These keywords are int, float, char, double, void
Here we are only concern with variable declaration. Declaration of functions will be deal in later chapter.
Consider the following statements int x=5, y; float k=2.34; char a=’y’, ch;
These are examples of data type declaration instruction. Compiler when read these instructions, it would get aware of four things:
1) Name of the variables Here names are x ,y, k, a, ch 2) Size of memory block Size of int type block is 2 bytes, size of float block is 4 bytes, size of char block is 1 byte, size of double block is 8 bytes 3) Type of content int variable can contain integer constant, float and double variable can contain real constant and char variable can contain character constant. 4) Value in variable x contains 5, k contains 2.34 and a contains ‘y’. Variables y and ch are not initialized but they are not empty. They contain unpredictable values which are of no use in the program, so called them garbage values.
Author: Saurabh Shukla
Modifiers Modifiers are the keywords used to modify the properties of data type. Modifiers are short, long, signed, unsigned.
Here is the list of all possible combination of use of modifiers with data type: Create variables to store integers: int, short int, signed int, long int, unsigned int, unsigned short int, unsigned long int. Create variables to store real constant float, double, long double Create variables to store character constant char, unsigned char
Modifiers affect size of data type (short and long), data range (signed and unsigned)
Following data type chart helps you to learn different properties modified in data type
Type Size
Format specifier
Content Type Range
unsigned char 8 bits^ %c^ Character^ 0 to 255
Char 8 bits %c^ Character^ -128 to 127
unsigned int 16 bits^ %u^ Integer^ 0 to 65,
short int 16 bits %d^ Integer^ -32,768 to 32,
Int 16 bits^ %d^ Integer^ -32,768 to 32,
unsigned long 32 bits %lu^ Integer^ 0 to 4,294,967,
Long 32 bits^ %ld^ Integer^ -2,147,483,648 to 2,147,483,
Float 32 bits
%f Real (^) 1.1754(10^-38) to 3.4028 (10^+38)
Double 64 bits
%lf Real (^) 2.2250 * (10^-308) to 1.7976 * (10^+308)
long double 80 bits %Lf^ Real^ 3.4 * (10^-4932) to 1.1 * (10^4932)
Qualifier
const l const means that something is not modifiable l const variable must be initialized during its declaration volatile
Author: Saurabh Shukla
Second instruction is a call to a function getch() whose job is to take one character from keyboard. Until you press any key this line wouldn’t finish its job. As call to getch() is the last line of the program, termination of program depends on when you enter a character. This let you see output window till you press any key.
You probably wonder why your older output is still on the screen every time you run your program. This is due to your consecutive execution of program using same output window. This problem can be solved by using clrscr() before printf(). Call to function clrscr() erase content of output screen.
Modified program:
main() { clrscr(); printf(“Hello SCA”); getch(); }
Write a program to display values of two variables on the monitor. Declare these two variables of any type and assigned some values to them.
main() { int a=3,b=56; clrscr(); printf(“%d %d”,a,b); getch(); }
Output: 3 56
In this example printf is use to display contents of variables a and b. Notice that %d is a special symbol called format specifier. They are used to specify format of data to be printed. In the format string %d will be replaced by values of the variables.
We can write expressions in place of variable list. main() { int a=3,b=56; clrscr(); printf(“%d %d %d”,a,b, a+b); getch(); }
Author: Saurabh Shukla
Output 3 56 59 Notice that printf contains three format specifiers (%d) as there are three expressions in the variable list. First two are simply variables and third one is an expression which first solved then the result gets printed in the place of third %d.
Escape Sequences Escape sequences are special symbols prefixed with a back slash ( \ ). These symbols are used in printf to provide a formatted output.
Here is the list of escape sequences:
Escape Sequence Name Meaning
\a (^) Alert Produces an audible or visible alert.
\b (^) Backspace Moves the cursor back one position (non-destructive).
\f (^) Form Feed Moves the cursor to the first position of the next page.
\n (^) New Line Moves the cursor to the first position of the next line.
\r Carriage Return Moves the cursor to the first position of the current line.
\t Horizontal Tab Moves the cursor to the next horizontal tabular position.
\v Vertical Tab Moves the cursor to the next vertical tabular position.
' (^) Produces a single quote.
" (^) Produces a double quote.
\ (^) Produces a single backslash.
\0 (^) Produces a null character.
** ddd
Defines one character by the octal digits (base-8 number). Multiple characters may be defined in the same escape sequence, but the value is implementation-specific.
\x dd Defines one character by the hexadecimal digit (base- number).
Consider the following program
main() { int a=3,b=56; clrscr(); printf(“%d\n%d”,a,b); getch(); }
Author: Saurabh Shukla
}
For float variable format specifier should be %f. Here we can input two values, first an integer and second one is float.
Write a program to add two numbers.
main() { int a,b,c; clrscr(); printf(“Enter two numbers”); scanf(“%d%d”,&a,&b); c=a+b; printf(“Sum is %d”,c); getch(); }
Output Enter two numbers 6 Sum is 11
Now you can use scanf for user input. We will again study scanf in great detail during functions in later chapter.