C Programming 01
C Programming 01
PROGRAMMING WITH C
FOR B.TECH. COURSE
DR. BRIJESH NAIK
SARVAJANIK COLLEGE OF ENGINEERING &
TECHNOLOGY, SURAT
E-Mail: [email protected]
What is programming?
●
Computer programming
●
Set of instructions..
●
A language/computer language
●
Choices of programming language
●
Purpose
●
What you want to get after all?
●
How to explain a computer?
●
Why C language is só important?
●
Learning C (C++ not for this course...)
2012-02-22 2
History and Importance of C language
●
A successor to the programming language B, C was
originally developed at Bell Labs by Dennis Ritchie
between 1972 and 1973 to construct utilities running on
Unix.
●
During the 1980s, C gradually gained popularity. It has
become one of the most widely used programming
languages, with C compilers from various vendors
available for the majority of existing computer
architectures and operating systems.
●
C has been standardized by ANSI since 1989 (ANSI C)
and by the International Organization for
Standardization (ISO).
Source: Wikipedia
ANSI: American National Standard Institute
2012-02-22 3
..
●
Dennis Ritchie (right), the inventor of the C
programming language, with Ken Thompson
2012-02-22 4
Why C is important
●
Faster
●
Close to hardware
●
Robust
●
Almost all the microcontrollers, microprocessors and
digital signal controllers are programmed in C language
still today!!
●
Supported by various operating systems like windows,
unix, mac os and linux(Linux kernel is writte in C).
●
Best suited for graphics programming.
●
Best suited for complex computations.
●
If you know C language, you can quickly learn other
languages.
2012-02-22 5
Termininology of (C) programming
●
High level language
●
Low level language
●
Compilers vs interpreters
●
Library
●
Syntax
●
IDE for programming
●
Variables
●
Hardware and software tools
●
Debugging
●
Program build and run
●
C IS A MIDDLE LEVEL LANGUAGE
2012-02-22 6
..
●
Header files
●
Machine code
●
Assembly language
●
Scripting
2012-02-22 7
Flow charts and pseudocode
2012-02-22 8
pseudocode
For Example: For Example:
2012-02-22 9
Structure of a simple C program
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
●
The void main() indicates that the main() function will not return any value, but the int main() indicates that the
main() can return integer type data. When our program is simple, and it is not going to terminate before
reaching the last line of the code, or the code is error free, then we can use the void main()
2012-02-22 10
Constants, variables and keywords
●
Variables: float, int, char, signed int, unsigned int,
double, long double
2012-02-22 11
C keywords
2012-02-22 12
Constants
Rules for Constructing Integer Constants
(a) An integer constant must have at least one digit.
(b) It must not have a decimal point.
(c) It can be any of zero, positive or negative. If no sign precedes an
integer constant, it is assumed to be positive.
(d) No commas or blanks are allowed within an integer constant.
(e) The allowable range for integer constants is -2147483648 to
+2147483647.
Ex.: +325.34 426.0 -32.76 -48.5792
Truly speaking, the range of an Integer constant depends upon the
compiler. For compilers like Visual Studio, GCC, it is -2147483648
to
+2147483647, whereas for compilers like Turbo C or Turbo C++,
the range is -32768 to +32767.
2012-02-22 13
..
Rules for Constructing Real Constants
Real constants are often called Floating Point constants.
Real constants could be written in two forms—Fractional
form and Exponential form.
Following rules must be observed while constructing real
constants expressed in fractional form:
(a) A real constant must have at least one digit.
(b) It must have a decimal point.
(c) It can be either positive or negative. Default sign is
positive.
(d) No commas or blanks are allowed within a real
constant.
Ex.: +325.34 426.0 -32.76 -48.5792
2012-02-22 14
..
The exponential form is usually used if the value of the constant is either too
small or too large. It, however, doesn't restrict us in any way from
using exponential form for other real constants.
In exponential form, the real constant is represented in two parts. The part
appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is
called exponent. Thus 0.000342 can be written in exponential form as 3.42e-4
(which in normal arithmetic means 3.42 x 10-4).
Following rules must be observed while constructing real constants expressed
in exponential form:
(a) The mantissa part and the exponential part should be separated by a letter
e or E.
(b) The mantissa part may have a positive or negative sign. Default sign is
positive.
(c) The exponent must have at least one digit, which may be a positive or
negative integer. Default sign is positive.
(d) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex.: +3.2e-5 4.1e8 -0.2E+3 -3.2e-5
2012-02-22 15
..
Rules for Constructing Character Constants
(a) A character constant is a single alphabet, a single digit
or a single special symbol enclosed within single inverted
commas.
(b) Both the inverted commas should point to the left. For
example, ‘A’ is a valid character constant, whereas ‘A’ is
not.
Ex.: ’A’ ’I’ ’5’ ’=’
2012-02-22 16
Rules for variable names
Rules for Constructing Variable Names
(a) A variable name is any combination of 1 to 31
alphabets, digits or underscores. Some compilers allow
variable names whose length could be
up to 247 characters. Do not create unnecessarily long
variable names as it adds to your typing effort.
(b) The first character in the variable name must be an
alphabet or underscore (_).
Ex.: si_int , pop_e_89, avg, basic_salary
2012-02-22 17
A C program
* Calculation of simple interest */
/
2012-02-22 19
..
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 (assuming i as integer
variable)
2012-02-22 20
..
Kk = 3 / 2 * 4 + 3 / 8 (assuming kk is float variable)
2012-02-22 21
C styles
2012-02-22 22