23
C PROGRAMMING LECTURE C-language Computer Fundamentals

C PROGRAMMING LECTURE C-language Computer Fundamentals

Embed Size (px)

Citation preview

Page 1: C PROGRAMMING LECTURE C-language Computer Fundamentals

C PROGRAMMING LECTURE

C-language

Computer Fundamentals

Page 2: C PROGRAMMING LECTURE C-language Computer Fundamentals

History of C C

Evolved by Ritchie from two previous programming languages, BCPL and B

Used to develop UNIX Used to write modern operating systems

Hardware independent (portable) By late 1970's C had evolved to "traditional C"

Page 3: C PROGRAMMING LECTURE C-language Computer Fundamentals

History of C

Standardization Many slight variations of C existed, and were

incompatible Committee formed to create a "unambiguous,

machine-independent" definition Standard created in 1989, updated in 1999

Page 4: C PROGRAMMING LECTURE C-language Computer Fundamentals

Language Types

Three types of programming languages1. Machine languages

Strings of numbers giving machine specific instructions Example:

+1300042774+1400593419+1200274027

2. Assembly languages English-like abbreviations representing elementary computer

operations (translated via assemblers) Example:

MOV AL, NUM1 ADD AL, NUM2

MOV VAL, AL

Page 5: C PROGRAMMING LECTURE C-language Computer Fundamentals

Language Types, Cont.

3. High-level languages Codes similar to everyday English Use mathematical notations (translated via

compilers) Example:

grossPay = basePay + overTimePay

Page 6: C PROGRAMMING LECTURE C-language Computer Fundamentals

High-level Languages

“high-level” is a relative term C is a relatively low-level high-level language Pascal, Fortran, COBOL are typical high-level

languages Java, Python, Perl, VB are examples of high-level

high-level languages Application specific languages (Matlab,

Javascript, VBScript) are even higher-level.

Page 7: C PROGRAMMING LECTURE C-language Computer Fundamentals

C Language

C-language

C is a high-level language.

Writing a C code. {turbo C, DevC++} For DevC++

Compiling a C code. {F9} Compile & Execute. {F11}

Page 8: C PROGRAMMING LECTURE C-language Computer Fundamentals

Character Set of C-Langauge

Alphabetic Letters A-Z A-z

Numbers 0-9

Special Characters +,-,*,/,% {,},[,],”,<,>,<=,>= ,(,) etc

C-language

Page 9: C PROGRAMMING LECTURE C-language Computer Fundamentals

Word

Reserved Words Also called keyword Reserved for language and language has special

meanings for these reserved word auto if break int case long Register continue return default short do sizeof double static else struct switch extern typedef float union d signed for unsigned goto while enum void const Volatile char

C-language

Page 10: C PROGRAMMING LECTURE C-language Computer Fundamentals

Words

C-language

User Defined Words User / Programmer defines(creates, declares) to solve

his own problem Could variable, function, structure, class name

int radius;radius is a reserved word

char name[50];Name is a reserved word

Page 11: C PROGRAMMING LECTURE C-language Computer Fundamentals

Variable Naming Rules

Can’t be reserved word Must start with alphabetic letter or underscore

only Must not contain any special character even

space character is not allowed Not two variables can have same name within

same scope C/C++ is case sensitive etc

C-language

Page 12: C PROGRAMMING LECTURE C-language Computer Fundamentals

Codding Rules

Every statement must end with semicolon Every program must have a main() function We place two statements on the same line; but

they must be separated with semicolon Program must include respective header file if

some required to be used in the program Variable naming rules etc

C-language

Page 13: C PROGRAMMING LECTURE C-language Computer Fundamentals

Error

Syntax Errors Due to violation of grammatical rules on language int first number;// invalid

Logical Errors Due wrong use of logic or formula Area=3.14+radius+radius;// * must be there instead of

+ Run Time Errors

Occur when specific condition meets Divide by zero

C-language

Page 14: C PROGRAMMING LECTURE C-language Computer Fundamentals

My first C program!

C-language

#include<iostream>using namespace std;// program prints hello worldmain() {

cout<<"Hello world!";

}

Output: Hello world!

Page 15: C PROGRAMMING LECTURE C-language Computer Fundamentals

Example 1

C-language

#include<iostream>

using namespace std;

// program prints a number of type int

main() {

int number = 4;

cout<<“Number is”<< number;

}

Output: Number is 4

Page 16: C PROGRAMMING LECTURE C-language Computer Fundamentals

Example 2

C-language

#include <stdio.h>// program reads and prints the same thingint main() {

int number ;

cout<<“enter number: ”;

cin>>number;

cout<<“\nNumber is ”<< number;

return 0;

}

Output : Enter a number: 4 Number is 4

Page 17: C PROGRAMMING LECTURE C-language Computer Fundamentals

more and more

C-language

#include <stdio.h>

using namespace std;

int main() {

/* this program adds

two numbers */

int a = 4; //first number

int b = 5; //second number

int answer = 0; //result

answer = a + b;

}

Page 18: C PROGRAMMING LECTURE C-language Computer Fundamentals

Operators

C-language

Operators Symbols

Increment Operators ++ -- (Postfix), ++ -- (Prefix)

Asthmatic Operators * / % + -

Shift Operator << >>

Relational Operators < >

Bitwise Binary Operators

& | ~ ^

Logical Operators && || !

Page 19: C PROGRAMMING LECTURE C-language Computer Fundamentals

Some more Arithmetic Operators

C-language

Prefix Increment : ++a example:

int a=5; b=++a; // value of b=6; a=6;

Postfix Increment: a++ example

int a=5; b=a++; //value of b=5; a=6;

Page 20: C PROGRAMMING LECTURE C-language Computer Fundamentals

Some more Data Types

C-language

Page 21: C PROGRAMMING LECTURE C-language Computer Fundamentals

Contd…

C-language

Modulus (remainder): % example:

12%5 = 2;

Assignment by addition: += example:

int a=4; a+=1; //(means a=a+1) value of a becomes 5

Can use -, /, *, % also

Page 22: C PROGRAMMING LECTURE C-language Computer Fundamentals

Contd…

C-language

Comparision Operators: <, > , <=, >= , !=, ==, !, &&, || .

example: int a=4, b=5; a<b returns a true(non zero number) value.

Bitwise Operators: <<, >>, ~, &, | ,^ . example

int a=8; a= a>>1; // value of a becomes 4

Page 23: C PROGRAMMING LECTURE C-language Computer Fundamentals

Operator Precedence

C-language

Meaning of a + b * c ? is a+(b*c) basically

All operators have precedence over each other *, / have more precedence over +, - .

If both *, / are used, associativity comes into picture. (more on this later)

example : 5+4*3 = 5+12= 17.