Basics of C Programming
Structure:
Your First Program
Preprocessor: interact with
#include <stdio.h>
<stdio h> input/output of your computer
int main() You will see this at the
b i i off nearly
beginning l allll
{ programs
printf("Hello World\n"); Tells computer to load file
named <stdio.h>
<stdio h>
return 0; <stdio.h> allows standard
input/output operations
}
Your First Program
Preprocessor: interact with
#include <stdio.h>
<stdio h> input/output of your computer
int main() Start point of the program
{ C programs contain one
or more functions,
printf("Hello World\n"); exactly one of which
must be
b maini
return 0; int means that the
function main will
} " t " an iinteger
"return" t value
l
Your First Program
Preprocessor: interact with
#include <stdio.h>
<stdio h> input/output of your computer
int main() Start point of the program
{ Start and finish of function
printf("Hello World\n");
return 0;
}
Your First Program
Preprocessor: interact with
#include <stdio.h>
<stdio h> input/output of your computer
int main() Start point of the program
{ Start and finish of function
printf("Hello World\n"); Printing a line of Text
return 0;
}
Your First Program
Preprocessor: interact with
#include <stdio.h>
<stdio h> input/output of your computer
int main() Start point of the program
{ Start and finish of function
printf("Hello World\n"); Printing a line of Text
return 0;
}
New line character
Your First Program
Preprocessor: interact with
#include <stdio.h>
<stdio h> input/output of your computer
int main() Start point of the program
{ Start and finish of function
printf("Hello World\n"); Printing a line of Text
return 0; Finish and return value 0
} A way to exit a function
It means that the program
terminated normally in this
case
Comments for programs
• Why need comments
– Good habit
– Readable to others
– Remind yourself
• How to comment
– /* … */
– // …
• Effects on compiler
• Examples
Compiler
• What is compiler
– A computer program (or set of programs) that
translates text written in a computer language (
the source code) into another computer language
(most time the executable file)
• Why we need a compiler
• Available C compiler
Text Editor
• Edit your code Using notepad,
notepad wordpad on
your personal computer
• Save it as a text file with extension .cc
Procedure
helloworld.c
#include <stdio.h> This is your C program.
program Type the
int main() code in any standard text editor,
{ and save it as helloworld.c.
printf("Hello World\n");
return 0;
}
Type tcc helloworld.c or
cl helloworld.c
helloworld c
C‐compiler bcc32 helloworld.c
to compile helloworld.c into [Link]
using the
h tcc compiler
l
[Link] The compiler generate corresponding
0011 0000 1010 0110 executable code named [Link].
1100 0110 1011 0101 The computer can execute this machine
1010 1110 0110 1110 readable code if you type
[Link]
Introduction
• The C programming language was designed by
Dennis Ritchie at Bell Laboratories in the early
1970s
• Influenced by
– ALGOL 60 (1960),
– CPL (Cambridge
(Cambridge, 1963),
1963)
– BCPL (Martin Richard, 1967),
– B (Ken Thompson, 1970)
• Traditionally
d ll used
d for
f systems programming,
though this may be changing in favor of C++
• Traditional C:
– The C Programming Language, by Brian Kernighan
and Dennis Ritchie, 2nd Edition, Prentice Hall
– Referred to as K&R
Standard C
• Standardized in 1989 by ANSI (American National
Standards Institute) known as ANSI C
• International standard (ISO) in 1990 which was adopted
by ANSI and is known as C89
• As part of the normal evolution process the standard
was updated in 1995 (C95) and 1999 (C99)
• C++ and C
– C++ extends C to include support for Object Oriented
Programming and other features that facilitate large
software development projects
– C is not strictly a subset of C++, but it is possible to write
“Clean C” that conforms to both the C++ and C standards.
Elements of a C Program
• A C development environment includes
– System libraries and headers: a set of standard libraries and their
header files. For example see /usr/include and glibc (in
Li
Linux)or)
C:\tc\include and C:\tc\lib
– Application Source: application source and header files
– Compiler: converts source to object code for a specific platform
– Linker: resolves external references and produces the executable
module
• User program structure
– there must be one main function where execution begins when the
program is run. This function is called main
• int main (void) { ... },
• int main (int argc, char *argv[]) { ... }
• UNIX Systems have a 3rd way to define main(), though it is not POSIX.1
compliant
int main (int argc,argc char *argv[]*argv[], char *envp[])
– additional local and external functions and variables
Getting Started with C
Programming: Involves speaking the language the computer can understand
Alphabets Words Sentences Paragraph
Alphabets
Constants
Digits
Variables Instructions Program
Special
Keywords
Symbols
C Character Set
Alphabets A,B,…,Z, a,b, …,z
Digits 0,1,2,…,9
Special Symbols ~‘@#%^&*()_‐+ =|\{}[]:;“ ’ <>,.?/
Getting Started with C
Constants
Variables
Keywords
The Alphabets,
Alphabets Digits and Symbols properly combined form
Constants, Variables and Keywords
A Constant: A q
quantityy that doesn’t change
g stored at a memoryy
location
A Variable: Simply a name given to the memory location where
a value is stored.
Contents of the variable can be changed
Getting Started with C
Type of C Constant:
Primary: Integer, Real, Character, etc..
Primary
Secondary: Array, Pointer, Structure,….
Secondary
Some Rules:
Integer
g Constants:
* Have at least one digit,
* No decimal point,
* if no sign precedes it is assumed to be positive
* No comma or blanks are allowed
* Range ‐32768
32768 to +32767
Getting Started with C
Some Rules:
Real Constants:
* Have at least one digit,
* must have a decimal point,
p ,
* if no sign precedes it is assumed to be positive
* No comma or blanks are allowed
* In exponential form represents in two parts:
Mantissa is appeared before ‘e’ and exponent after ‘e’
Mantissa can be +ve or –ve
Range ‐3.4e38 to +3.4e38
Character Constants
* either a single alphabet, a digit or a symbol enclosed
within single inverted commas
Getting Started with C
Types of Variables
“A
A quantity which may vary during the program execution is a
variable”
Variable Names: Names given to the memory locations where
different constants are stored
• Particular type
yp of variable can hold onlyy that type
yp of data
• Some rules for constructing Variable names
• Start with a alphabet A…Z, a…z
• Names are case sensitive
• Can contain any combination up to 32 (in standard C)
characters of alphabet,
alphabet numbers
numbers, or underscores
• No commas or blanks are allowed
• No symbols are allowed except underscores ( _ )
Getting Started with C
• In c it is compulsory to declare variables before, to use them
• Using a proper naming conversion is a good programming
practice
• Eg: int averageMarks,
averageMarks totalMarksBySubjects
C Keywords
The reserved
Th d words
d whose
h meaning
i h has already
l d been
b
explained to the C compiler
Getting Started with C
C Instructions
Combine constants,
constants variables and keywords in logical manner
• Type declaration : to declare the type of variables used in a
program
• Input/Output: to perform the function of supplying input data
to a p
program
g and obtainingg the result from it
• Arithmetic: to perform arithmetic operation between
constants and variables
• Control: to control the sequence of execution of various
statements in a program
Getting Started with C
Type declaration
C provides a wide range of types. The most common are
There are also several variants on these types.