You are on page 1of 27

1 - Getting Started

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.

Getting Started with C


Communicating with a computer involves speaking the language the computer understands

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

The C Character Set


A character denotes any alphabet, digit or special symbol used to alphabet, represent information.

Constants, Variables and Keywords


The alphabets, numbers and special symbols when properly combined form constants, variables and keywords.

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

Rules for Constructing Integer Constants


An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is -32768 to 32767. Examples : 426 +782 -8000 -7605

Rules for Constructing Real Constants


A real constant must have at least one digit. It must have a decimal point. point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. The mantissa part and the exponential part should be separated by a letter e. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38. Examples : +325.34 +3.2e-5 +3.2e426.0 4.1e8 -32.76 -0.2e+3 -48.5792 -3.2e-5 3.2e8

Rules for Constructing Character Constants


A character constant is a single alphabet, a single digit alphabet, or a single special symbol enclosed within single inverted commas. For example, 'A' is a valid character commas. constant whereas `A` is not. The maximum length of a character constant can be 1 character. Examples : 'A' 'I' '5' '='
9

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

Rules for Constructing Variable Names


A variable name is any combination of 1 to 31 alphabets, digits or underscores. Do not create unnecessarily long variable names as it adds to your typing effort. The first character in the variable name must be an alphabet or underscore. No commas or blanks are allowed within a variable name. No special symbol other than an underscore (as in gross_sal) can gross_sal) be used in a variable name. Examples :
si_int m_hra pop_e_89 NoOfStudents Hairi66 my_hometown_special_recipe

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.

Type declaration instruction


To declare the type of variables used in a C program.

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

Type Declaration Instruction


Any variable used in the program must be declared before using it in any statement. Examples :
int bas ; float rs, grossSal ; char name, code ; int i = 10, j = 25 ;  float a = 1.5, b = 1.99 + 2.4 * 1.44 ;  float a = 1.5, b = a + 3.1 ;  float b = a + 3.1, a = 1.5 ; int a, b, c, d ; a = b = c = 10 ;  int a = b = c = d = 10 ;

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 is assumed to be present.


a = c.d.b(xy) usual arithmetic statement a = c * d * b * ( x * y ) C statement 

No operator for performing exponentiation operation. Thus following statements are invalid.
a = 3 ** 2 ; b=3^2;

15

Integer and Float Conversions


A C arithmetic statement could be of three types : Integer mode arithmetic statement Real mode arithmetic statement Mixed mode arithmetic statement An operation between an integer and integer yields an integer result. An operation between a real and real always yields a real result. An operation between an integer and real always yields a real result.

16

Type Conversion in Assignments


assumed that k is an integer variable and a is a real variable

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

The First C Program


comments

// Author : Tan Yee Chyan // Date : 14 July 2009 // Title : My First Program
return nothing accept nothing

void main (void) (void) { }

function always have brackets ()

opening and closing braces

21

Compilation and Execution

1. Compile 2. Build 3. Execute

Error and Warning Window Result window

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';

variables type declaration

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

You might also like