Download presentation
Presentation is loading. Please wait.
Published byClifford Rice Modified over 9 years ago
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/)Code::Blocks with MinGW 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 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 char1 byte-128 to 127 or 0 to 255 unsigned char1 byte0 to 255 signed char1 byte-128 to 127 int2 or 4 bytes-32,768 to 32,767 or - 2,147,483,648 to 2,147,483,647 unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295 short2 bytes-32,768 to 32,767 unsigned short2 bytes0 to 65,535 long4 bytes-2,147,483,648 to 2,147,483,647 unsigned long4 bytes0 to 4,294,967,295
9
Floating point types TypeStorage sizeValue rangePrecision float4 byte1.2E-38 to 3.4E+38(7 digits) double8 byte2.3E-308 to 1.7E+308(15 digits) long double10 byte or same as double 3.4E-4932 to 1.1E+493219 decimal places
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 (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.
13
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.