1 / 16

C Programming

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/)

khuong
Download Presentation

C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Programming

  2. Chapter – 1 Introduction

  3. Study Book for one month – 25% Learning rate • Use Compiler for one month – 60%

  4. 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)

  5. C Program Structure • A C program basically consists of the following parts: • Preprocessor Commands • Functions • Variables • Statements & Expressions • Comments

  6. 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.

  7. 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

  8. 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

  9. Floating point types

  10. 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/

  11. Variables & Arithmetic Expressions Reference: Kernighan, Brian W. The C programming language

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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

More Related