c programming lecture c-language computer fundamentals
TRANSCRIPT
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"
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
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
Language Types, Cont.
3. High-level languages Codes similar to everyday English Use mathematical notations (translated via
compilers) Example:
grossPay = basePay + overTimePay
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.
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}
Character Set of C-Langauge
Alphabetic Letters A-Z A-z
Numbers 0-9
Special Characters +,-,*,/,% {,},[,],”,<,>,<=,>= ,(,) etc
C-language
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
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
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
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
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
My first C program!
C-language
#include<iostream>using namespace std;// program prints hello worldmain() {
cout<<"Hello world!";
}
Output: Hello world!
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
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
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;
}
Operators
C-language
Operators Symbols
Increment Operators ++ -- (Postfix), ++ -- (Prefix)
Asthmatic Operators * / % + -
Shift Operator << >>
Relational Operators < >
Bitwise Binary Operators
& | ~ ^
Logical Operators && || !
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;
Some more Data Types
C-language
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
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
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.