160 likes | 426 Vues
C Programming. Chapter – 1 Introduction. Study Book for one month – 25% Learning rate Use Compiler for one month – 60%. Environmental Setup . Install Microsoft Visual studio (Compiler) OR Code::Blocks with MinGW (http://www.cprogramming.com/code_blocks/)
E N D
Study Book for one month – 25% Learning rate • Use Compiler for one month – 60%
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
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/
Variables & Arithmetic Expressions Reference: Kernighan, Brian W. The C programming language
All variables – declared before using it. • Usually it starts with the type name (intfahr, 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.
Tip: A Menu, Server • 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.
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.
Write a C program to convert Celsius to Fahrenheit between 0 to 200 with a step size of 20 and print the value in reverse order. You can use any loop statement. • arealisticdreamer.weebly.com