Вы находитесь на странице: 1из 27

# C Programming

## Design By: Solutioncab.com

What is a language?
A Language is the Spoken and signed forms of communication among Humankind. A formal language is a set of words, i.e. finite strings of letters, symbols, or tokens. The set from which these letters are taken is called the alphabet. Eg. English, Hindi etc.

What is a language?

Communicating with a Human Being involves speaking the language that human being understands.
Communicating with a computer involves speaking the language the computer and human being both understands,

What is C ?

C is a programming language developed at AT & Ts Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. C is often called a middle-level computer language, because it has the features of both high-level languages, such as BASIC, PASCAL etc., and low-level languages such as assembly language.

## Steps Of Learning English Languages.

Alphabets Words Sentences Paragraphs

Steps Of Learning C.
Alphabets Digits Special Symbols Constants Variables Key words Instructions Programs

## The C Character Set

A character denotes any alphabet, digit or special symbol used to represent information. Like:
Alphabets: A,B,C.X,Y,Z a,b,c,.....x, y, z Digits: 0,1,2,3,4,5,6,7,8,9. Special Symbols: ~!@#\$%^&*()_+|-=\`[{}];:<>?/

## Constants, Variables & Keywords

Constants: A constant is an entity that does not change. eg. 2, 3, 4, 900,8.675. Variables: A variable is an entity that may change. eg. X, f, Ram, Var_iable, x u z Variable x = 40 Constant 40 20 40 Whose value is changing ? x u z x = 20 20 40 20 40

Types of Constants
C constants can be divided into two major categories:

Primary Constants
Integer Constant. Real or Float Constant. Character Constant.

Secondary Constants
Array. Pointers Structure. Union Enum etc.

## (we will study later.)

Integer Constant.
Rules of 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.

Integer Constant.

## Examples of Valid Integer Constant.

Valid : 0, 1, 743, -67, 5478, 9999, etc

## Example of Invalid Integer Constants.

12,245 36.4 12 34 0900 illegal Character (,)

## illegal Character(.) illegal Character (Blank Space)

The first digit cannot be zero

## Real or Float Constant.

Rules for Constructing Real Constants A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant.

## Real or Float Constant

Example of valid Real or Float Constant +325.34 , 426.0 , -32.76, -48.5792 Example of in valid Real or Float Constant
1 1,000.0 Must Have (.) illegal Character (,)

2E+10.2
3 .20

## Power cannot be in decimal (10.2)

Blank space Not allow

## Real constants expressed in exponential form:

The mantissa part and the exponential part should be separated by a letter e. The mantissa part may have a positive or negative sign. Default sign of mantissa part is positive. The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

## Ex.: +3.2e-5 4.1e8 -0.2e+3 -3.2e-5

Mantissa Separator
Exponent

3.2 e -5

Character Constants
Rules for Constructing Character Constants A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, A is a valid character constant whereas A is not. The maximum length of a character constant can be 1 character.

Character Constants
Example of Character Constants. 'A' 'I' '5' '=' Invalid character Constants.

a AB
Commas are not in same direction More than one alphabet is declared.

Variable Constant

Variable names are names given to locations in memory. These locations can contain integer, real or character constants. The types of variables that it can support depend on the types of constants that it can handle. This is because a particular type of variable can hold only the same type of constant. For example,

An integer variable can hold only an integer constant. A real variable can hold only a real constant. A character variable can hold only a character constant.

Variable Constant
Rules of Variable declaration. Variables must be declared before use immediately after {. Valid characters are letters, digits and _ . First character cannot be a digit. 31 characters recognized for local variables. (more can be used, but are ignored). Upper and lower case letters are distinct.

Variable Declaration
Variable type Variable Name int b; double x; int b; unsigned int a; char c; C is a typed language that means a variable has a

Type Name

C standard types

int, double, char, float, short, long, long double unsigned char, unsigned short, unsigned int, unsigned long

Variable Constant

## Example of Variable constants.

int a; int a = 10; float b = 12.6; float b; char c; char c = y; /* General declaration */ /* Valued Declaration */ /* Valued Declaration */ /* General declaration */ /* General declaration */ /* Valued Declaration */

Keywords.

In Standard C, the keywords have special significance to the compiler and therefore can not be used as identifiers.
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

The Format of C
Statements are terminated with semicolons(;) Indentation is ignored by the compiler C is case sensitive - all keywords and

Standard Library functions are lowercase Strings are placed in double quotes () New lines are handled via \n Programs are capable of flagging success or error, those forgetting to do so have one or other chosen randomly!

## printf and scanf

printf writes

integer values to screen when %d is used float values to screen when %f is used character values to screen when %c is used integer values from the keyboard when %d is used. float values from the keyboard when %f is used. character values from the keyboard when %c is used.

& VERY important with scanf & not necessary with printf because current value of parameter is used

## The Basic Structure of a C Program

/* Program documentation
#Preprocessor directives

*/

main ()

{
Global constants Declaration

## Instructions & Statements

First C program.
#include <stdio.h> int main() { printf(Hello World\n); } // send string to standard output // input-output library // function main

## Output: Hello world

First C program.

#include <stdio.h> includes the standard I/O library which allows you to read from the keyboard (standard in) and write to the screen (standard out) int main() declares the main function. Every C-program must have a function named main somewhere in the code { ... } The symbols { and } mark the beginning and end of a block of code. printf(Hello World\n) Sends output to the screen. The portion in quotes ... is the format strings and describes how the data is formatted.

Second program.
#include <stdio.h> standard I/O file void main() main function Code block starts/ Main start { int a; declare a variable of type integer. float x; declare a variable of type float. declare a variable of type character. char c; printf(Enter integer:); display on screen Enter integer. scanf(%d,&a); store the integer value in a. printf(\nEnter float:); display on screen Enter Float. scanf(%f,&x); store the Float value in x. printf(\nEnter character:); display on screen Enter character. store the character value in c. scanf(%c,&c); printf(\nValue of a is %d, of x is %f, of c is %c\n,a,x,c); gives output } Code block end or Main end

End of lecture 1