Professional Documents
Culture Documents
What is C Getting Started with C The First C Program Compilation and Execution Receiving Input C Instructions Control Instruction in C Summary Exercise
1
What is C?
designed and written by a man named Dennis Ritchie, 1972 reliable, simple and easy to use, and has survived more than 3 decades even today when it comes to performance (speed of execution) nothing beats C one should first learn all the language elements very thoroughly using C language before migrating to C++, C# or Java because C++ and Java still required to use the core C language elements popular operating systems like Windows, UNIX, Linux is still written in C Mobile devices like cellular phones and palmtops, also, common consumer devices like microwave oven, washing machines and digital cameras use microprocessor, an operating system and a program embedded in these devices. These programs not only have to run fast but also have to work in limited amount of memory. Such programs are written in C. Many popular gaming frameworks have been built using C language. The essence of all such games is speed and to match the expectations of the player the game has to react fast to the user inputs.
The C Character Set Constants, Variables and Keywords Types of C Constants Rules for Constructing Integer Constants Rules for Constructing Real Constants Rules for Constructing Character Constants Types of C Variables Rules for Constructing Variable Names C Keywords
3
Since the location whose name is x can hold different values at different times x is known as a variable. As against this, 3 or 5 do not change, hence are known as constants.
5
Types of C Constants
Types of C Variables
Variable names are names given to locations in memory. These locations can contain integer, real or character constants. An integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant. The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply.
10
11
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword.
12
C Instructions
There are basically 3 types of instructions in C: 1.
2.
Arithmetic instruction
To perform arithmetic operations between constants and variables.
3.
Control instruction
To control the sequence of execution of various statements in a C program.
13
14
Arithmetic Instruction
Only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal. leftA modular division operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. It cannot be applied on a float. An arithmetic instruction is often used for storing character constants in character variables.
char a, b, d ; a = 'F' ; b = 'G' ; d = '+' ;
ASCII values are used to represent any character in memory. The ASCII values of F and G are 70 and 71 (refer the ASCII Table in Appendix E). Arithmetic operations can be performed on ints, floats and chars. Thus the statements,
char x, y ; int z ; x = 'a' ; y = 'b' ; z=x+y;
No operator for performing exponentiation operation. Thus following statements are invalid.
a = 3 ** 2 ; b=3^2;
15
16
17
Hierarchy of Operations
The expression 2 * x - 3 * y correspond to (2x)-(3y) or to 2(x-3y)? (2x)2(xDoes A / B * C correspond to A / (B * C) or to (A / B) * C?
Exercise : i=2*3/4+4/4+8-2+5/8 kk = 3 / 2 * 4 + 3 / 8 + 3
18
Associativity of Operators
When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators.
a=3/2*5; a=b=3; z=a*b+c/d;
19
Control Instruction in C
Sequence Control Instruction ensures that the instructions are executed in the same order in which they appear in the program. Selection or Decision Control Instruction allow the computer to take a decision as to which instruction is to be executed next. Repetition or Loop Control Instruction helps computer to execute a group of statements repeatedly Case Control Instruction allow the computer to take a decision as to which instruction is to be executed next.
20
// Author : Tan Yee Chyan // Date : 14 July 2009 // Title : My First Program
return nothing accept nothing
21
22
Second program
// Author : Tan Yee Chyan // Date : 14 July 2009 // Title : My Second Program #include <stdio.h> void main (void) { i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8; kk = 3 / 2 * 4 + 3 / 8 + 3; }
23
Third program
#include <stdio.h> void main (void) { int principal, NoOfYears ; float rate, si ; char initial; principal = 1000 ; NoOfYears = 3 ; rate = 8.5 ; initial = 'H';
initialization
another way to comment /* formula for simple interest */ si = principal * NoOfYears * rate / 100 ; printf ( "%f\n" , si ) ; "%f\ %d\ printf ( "The principal = %d\n", principal ) ; printf ( "The number of years = %d\n", NoOfYears ) ; %d\ printf ( "The yearly interest rate = %f\n", rate ) ; %f\ %c\ printf ( "My initial is %c\n", initial ) ; printf ( "My initial is %c and The principal = %d\n", initial, principal ) ; %d\
} 24
Receiving Input
#include <stdio.h> void main(void) { int principle, NoOfYears ; float rate, si ; printf ( "Enter values of principle, NoOfYears, rate : " ) ; scanf ( "%d %d %f", &principle, &NoOfYears, &rate ) ; si = principle * NoOfYears * rate / 100 ; printf ( "Interest = RM %f\n" , si ) ; %f\ }
25
Summary
The three primary constants and variable types in C are integer, float and character. A variable name can be of maximum 31 characters. Do not use a keyword as a variable name. An expression may contain any sequence of constants, variables and operators. Operators having equal precedence are evaluated using associativity. Input/output in C can be achieved using scanf( ) and printf( ) functions.
26
Exercise
1. Rameshs basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary. 2. The distance between two cities (in km) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters. 3. If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100. 4. Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.
27