Upload
clifford-rice
View
214
Download
2
Embed Size (px)
Citation preview
Environmental Setup
Install Microsoft Visual studio (Compiler)
OR
Code::Blocks with MinGW (http://www.cprogramming.com/code_blocks/)
Download Book – The C Programming Language – Brian W. Kernighan, Dennis M. Ritchie (Open source)
C Program Structure A C program basically consists of the following parts:
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
1. The first line of the program #include <stdio.h> is a preprocessor command.
It tells the compiler to include the standard I/O lirary.
2. The next line int main() is the main function where program execution begins. In this case function without any argument values.
Many cases – Main(int a, int b) – passing some arguments
3. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
4. The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.
"Hello, World!" – argument
Data type – string
\n – is also string, but its newline character.
5. The next line return 0; terminates main()function and returns the value 0.
“;” - semicolon is must for every statement.
Try exercise 1-1, 1-2
Data types In the C programming language, data types refer to an extensive system used for
declaring variables or functions of different types.
The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
Size of the objects are usually machine dependent.
Integer types
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
Floating point types
Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 (7 digits)
double 8 byte 2.3E-308 to 1.7E+308
(15 digits)
long double 10 byte or same as double
3.4E-4932 to 1.1E+4932
19 decimal places
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes.
%E – exponential form
Limits.h - ?
Float.h - ?
Cstdio – std::getchar(); any guess?
In c (gcc compiler) – stdio.h – getchar();
In c++(msvcom compiler) – cstdio – std::getchar();
Be aware of which compiler you are using and some function may vary.
There also Arrays, structures, void, unions etc.. (save your for time for another day!)
Reference: http://www.cplusplus.com/reference/cfloat/
All variables – declared before using it.
Usually it starts with the type name (int fahr, celsius)
Followed by the whitespace and then some name.
Assignment of values and initialize.
While(……) is called condition in parentheses
If the condition satisfies then it flows to the next line, if not it is terminated.
While(1){
……. } - any guess?
The reason for multiplying by 5 and then dividing by 9 instead of multiplying by 5/9 is because integer division truncates any fractional part. In this case the answer will be zero.
In printf %d, \t are used – decimal and tab space.
Try different for example - %s, %c, %f, %o, %X,...
% is always paired with some arguments and awaits another argument, instead of a decimal when a floating point is given it gives wrong answer.
Question – what datatype for fahr and Celsius?
5.0/9.0 is not truncated.
Tip: A Menu, Server
Symbolic Constants
Always the name is written in Uppercase (Good programming practice)
Replacement text can be anything
Sequence of characters
Numbers
No need for semicolon at the end of the define.
The For loop statement
Most of the variables are eliminated.
Its more compact than the while statement
Inside the parentheses three parts separated with semicolon, first is
Fahr = 0 (initialize)
Fahr<= 300 (controlling part)
Fahr = fahr+20 (increment step)
The loop terminates if the condition is false.