Sei sulla pagina 1di 27

Introduction to C

Objectives
After this Session, you will be able to identify C tokens know C key words and identifiers write constants and variables of C language

Introduction
The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Influenced by ALGOL 60 (1960), CPL (Cambridge, 1963), BCPL (Martin Richard, 1967), B (Ken Thompson, 1970)

Standard C
Standardized in 1989 by ANSI (American National Standards Institute) known as ANSI C International standard (ISO) in 1990 which was adopted by ANSI and is known as C89 As part of the normal evolution process the standard was updated in 1995 (C95) and 1999 (C99)

Elements of a C Program
A C development environment includes
System libraries and headers: a set of standard libraries and their header files. Application Source: application source and header files Compiler: converts source to object code for a specific platform Linker: resolves external references and produces the executable module

Elements of a C Program
User program structure
there must be one main function where execution begins when the program is run. This function is called main int main (void) { ... }, int main (int argc, char *argv[])
{ ... }

additional local and external functions and variables

Header files
The files that are specified in the include section are called as header files These are precompiled files that have some functions defined in them We can call those functions in our program by supplying parameters Header file is given an extension .h C Source file is given an extension .c

Main function
This is the entry point of a program When a file is executed, the start point is the main function From main function the flow goes as per the programmers choice. There may or may not be other functions written by user in a program Main function is compulsory for any c program

Writing the first program


#include<stdio.h> int main() { printf(Hello); return 0; } This program prints Hello on the screen when we execute it

Running a C Program
Type a program Save it Compile the program This will generate an exe file (executable) Run the program (Actually the exe created out of compilation will run and not the .c file)

CHARACTER SET
C characters are grouped into the following categories.
1. 2. 3. 4. Letters Digits Special Characters White Spaces

Letters & Digits


Letters Uppercase A.Z Lowercase a..z Digits All decimal digits 0..9

Special characters
, Comma . Period ; Semicolon : Colon ? Question mark Apostrophe Quotation mark ! Exclamation | Vertical Bar / Slash \ Back slash ~ Tilde _ Underscore $ Dollar sign % Percent sign & Ampersand ^ Caret * Asterisk - Minus + Plus sign < Less than > Greater than ( Left parenthesis ) Right parentheses [Left bracket ] Right bracket { Left brace } Right brace # Number sign

C TOKENS
In C programs, the smallest individual units are known as tokens.

KEYWORDS AND IDENTIFIERS


Every C word is classified as either a keyword or an identifier. All keywords have fixed meanings and these meanings cannot be changed. Eg: auto, break, char, void etc., Identifiers refer to the names of variables, functions and arrays. They are user-defined names and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted. The underscore character is also permitted in identifiers.

Comments in C
Single line comment
// (double slash) Termination of comment is by pressing enter key

Multi line comment


/*. .*/ This can span over to multiple lines

Structure of C program
/* This is a comment */ // This is a one-line comment # include <stdio.h> /* includes header files */ main() /* Must have a main function. First function executed */ { printf ("Hello World!"); /* stdio functions */ }

Variables
Variables are data that will keep on changing Declaration
<<Data type>> <<variable name>>; int a;

Definition
<<varname>>=<<value>>; a=10;

Usage
a=a+1; //increments the value of a by 1

Variable names- Rules


Should not be a reserved word like int etc.. Should start with a letter or an underscore(_) Can contain letters, numbers or underscore. No other special characters are allowed including space Variable names are case sensitive
A and a are different.

CONSTANTS
Constants in C refer to fixed values that do not change during the execution of a program.

CONSTANTS
Integer Constants
An integer constant refers to a sequence of digits, There are three types integers, namely, decimal, octal, and hexa decimal.

Decimal Constant
Eg:123,-321 etc., Note: Embedded spaces, commas and non-digit characters are not permitted between digits. Eg: 1) 15 750 2)$1000

Octal Constant
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0. Eg: 1) 037 2) 0435

Hexadecimal Constant
A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A through F or a through f.Eg: 1) 0X2 2) 0x9F 3) 0Xbcd

Real Constants
Certain quantities that vary continuously, such as distances, heights etc., are represented by numbers containing functional parts like 17.548.Such numbers are called real (or floating point)constants. Eg:0.0083,-0.75 etc., A real number may also be expressed in exponential or scientific notation. Eg:215.65 may be written as 2.1565e2

Single Character Constants


A single character constants contains a single character enclosed within a pair of single quote marks. Eg: 5 , X , ; String Constants A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank space. Eg:Hello!, 1987, ?.!

Backslash Character Constants

C supports special backslash character constants that are used in output functions. These character combinations are known as escape sequences.

Backslash Character Constants

Check Your Understanding


1) Write a few numeric constants and character constants. 2) Write a few meaningful variable names you think . 3) Describe the character set of C language 4) What do you understand by C tokens? 5) Differentiate Keywords and Identifiers 6) Describe the constants of C language with examples

Summary
In this lesson we learnt described character set learnt the C tokens studied about constants variables of C

and

Potrebbero piacerti anche