Top Banner
C Programming Language By: Yogendra Pal [email protected] Dedicated to My mother and Father
30

C basics

Sep 21, 2014

Download

Documents

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C basics

CProgramming Language

By:

Yogendra [email protected]

Dedicated to My mother and Father

Page 2: C basics

THIS IS C BASICS

Keep Watching Keep Learning

2

t Keep your notebook with you.

yWrite important point and questions that comes in your mind

Solve Mind band exercise.

Ask Questions by call or SMS or by mail

CRewind when not clear

Page 3: C basics

First C Program

• Print a line of text.

• To compile you need a “C” compiler.

• Some compilers are Borland C, Turbo C.

3

#include<stdio.h>void main(){

printf(“I am learning C”);}

Page 4: C basics

Install Compiler

• Borland C and C++ compiler.

• Turbo C and C++ compiler.

• Work from

– Command line interface (CLI).

– Integrated Development Environment (IDE).

4

Page 5: C basics

• Type, compile and run the previous programon your computer.

• First use CLI then run them in IDE.

• C______ L___ I________.

• I_________ D__________ E__________.

5

Mind Bend

Page 6: C basics

C Character Set

• You write your C program by using some characters.

• These characters are:– Alphabets (Uppercase & Lowercase)

• 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 (~ ` ! @ # $ % ^ & * ( ) _ - + = | \ } ] [ { ; : ‘ “ / ? . > , < )

6

Page 7: C basics

C Character Set…

7

AlphabetsdigitsSpecial SymbolsSpace

Page 8: C basics

Comments

• Comment a program for easy understanding.

• Two kind of comments are available

– Single line comment

• //

– Multiple line comment

• /* */

8

Page 9: C basics

Escape Sequence

Character Escape Sequence ASCII value

Newline \n 10

Horizontal tab \t 9

Vertical tab \v 11

Backspace \b 8

Bell alert \a 7

Quotation mark \” 34

Apostrophe \’ 39

Question mark \? 63

Backslash \\ 92

Null \0 0

Carriage return \r 013

9

Page 10: C basics

• Using escape sequence write a program thatprint following output (Use a single printf()function).

10

Mind Bend

***

********

*******

Page 11: C basics

Identifiers

• Identifiers are names given to Variables,Functions, arrays and other programmingelements.

• Used to uniquely identify each element.

• Can contain alphabets, digits and underscore.

• First character must be an alphabet orunderscore.

• Space and other special symbols are notallowed.

11

Page 12: C basics

Identifiers…

A o V

12

Page 13: C basics

Identifiers…

• First_name

• a1

• area_of_circle

• Pi

• TABLE

• _area

• First name

• 1a

• area-of-circle

• ^

• “TABLE”

• -area

13

Correct Incorrect

Try to make identifier meaningful and small.

Page 14: C basics

Keywords

• Keywords are reserved words.

• Have standard, predefined meaning.

• Cannot be used as identifiers.

14

Page 15: C basics

Keywords…

auto extern sizeof break float

static case for struct char

goto switch const if sizedef

continue int union default long

unsigned do register void double

return volatile else short while

enum signed

15

Page 16: C basics

Data types

• There are several data types in C.

• Memory representation of each data type is different.

• Memory requirement is also different.

• Range of each data type varies from data type to data type.

16

Page 17: C basics

Data Types…

• Character (char)

• Integer (int)

• Floating point (float)

• Double (double)

• Valueless (void)

17

Page 18: C basics

Character

• Character can be any single alphabet, digit or special symbol.

• There are total 256 characters.

• Value of character can be 0 to 255.

• 1 Character = 8 bits = 1 byte

• Represent with “char” in C programs.

• 8 bits means 28 = 256 possibilities.

• Format specifier : %c18

Page 19: C basics

Type Declaration

• Assign a data type to a variable.

• Declare a variable before use.

19

char choice;char c1,c2,c3;char c1=‘y’;char c1=‘y’, c2=‘n’;

Page 20: C basics

Input / Output instructions

• Output

– printf(“ “);

– printf(“format specifier”,variable);

• printf(“%d”,i);

• Input

– scanf(“format specifier”,&variable);

• scanf(“%d”,&i);

– scanf take input from console.

– & operator represents the address.20

Page 21: C basics

Working with data

• Create a variable of a data type.

– char c;

• Initialize it with a value.

– c = ‘a’;

• Use it in program.

– printf ( “%c” , c);

• An example

1009

cNo

value

a

21

Page 22: C basics

Character constant

• Single alphabet, digit or special symbolenclosed within single inverted comma.

• Maximum length is one character.

• Correct: ‘a’ ‘!’ ‘$’

• Incorrect: a ‘30’ 30

2222

Every character have a unique ASCII value.

Page 23: C basics

Integer

• Integer can be any number.

• integer : 2 bytes (16 bit) or 4 bytes (32 bit).

– 16 bit means 216 = 65536 numbers.

• Range (-32768 to 32767)

– 32 bit means 232 = 4,29,49,67,296 numbers.

• Range (-2147483648 to 2147483647)

• Represent with “int” in C programs.

• Format specifier : %d

23

Page 24: C basics

Working with integer

• Create a integer type variable.

– int i;

• Initialize it with a value.

– i = 10;

• Use it in program.

– printf ( “%d” , i);

• An example

No

value

2

bytes

10

24

Page 25: C basics

Integer Constant

• Must have at least one digit.

• Decimal point is not allowed.

• Can be positive or negative default is positive.

• Commas, blanks or any other symbol is notallowed.

• Correct: 12 -467 +098

• Incorrect: 12,120 12-10 13.09

25

Page 26: C basics

Floating Point

• Floating number can be any number withdecimal point.

• float : 4 bytes (32 bits)

– Range (-3.4e38 to 3.4e38)

• Represent with “float” in C programs.

• Format specifier : %f

26

Page 27: C basics

Float Constant

• Fractional form

– eg. 456.09

– Must have at least onedigit.

– Must have a decimalpoint.

– Positive or negativedefault is positive.

– Comma, space and otherspecial symbol is notallowed.

• Exponential form {mantissa, exponent}.

– mantissaeexponent eg.3.234e6

– Both parts can bepositive or negative.

– Default sign is positive.

– Must have at least asingle digit on both side

– Exponent can not real.

27

Page 28: C basics

Mind Bend

• Are uppercase letters equivalent to lowercaseletters? (yes/no)

• Can be use digits in an identifier name?

• Can be use any special character in identifiername?

• What are keywords?

• Write the range of character, integer and floatdata types used in C.

28

Page 29: C basics

• What are the escape sequences for newline,backspace and horizontal tab?

• What is the purpose of type declaration?

• Is declaration of each variable necessarybefore use?

• Can be use char as a variable name oridentifier? CHAR can be used or not?

Mind Bend

29

Page 30: C basics

NEXT IS ARITHMETIC INSTRUCTIONSKeep Watching Keep Learning

To get complete benefit of this tutorial solve all the quiz on

www.learnbywatch.com

For any problem in this tutorial mail me at

[email protected]

with the subject “C”

For Other information mail at

[email protected]

30