C Programming Basic Lecture 1
C Programming Basic Lecture 1
TO C
C LANGUAGE HISTORY
Dennis MacAlistair Ritchie was C programming language
• The C programming
an American computer features were derived from an
language is a structure-
scientist. He created the C earlier language called “B”
oriented programming
programming language and, (Basic Combined
language, developed at Bell
with long-time colleague Ken Programming Language –
Laboratories in 1972 by
Thompson, the Unix operating BCPL)
Dennis Ritchie
system and B language.
C language is considered as
the mother language of all the
In 1978, Dennis Ritchie and modern programming
C language was invented to Brian Kernighan published the languages because most of
construct utilities running on first edition “The C the compilers, JVMs, Kernels,
UNIX operating system Programming Language” and etc. are written in C language,
commonly known as K&R C and most of the programming
languages follow C syntax, for
example, C++, Java, C#, etc.
LEVELS OF PROGRAMMING
LANGUAGES
Middle level languages do not provide all the built-in
Middle functions found in high level languages, but to
Level provide all building blocks needed to produce the
result desired. Examples: C, C++
High level languages provide almost everything that
High Level the programmer might need to do as already built
into the language. Example: Java, Python
Local
visible within function
describes data used only in function
PREPROCESSOR DIRECTIVES
Begin with #
Instruct compiler to perform some transformation to file before
compiling
Example: #include <stdio.h>
add the header file stdio.h to this file
.h for header file
stdio.h defines useful input/output functions
VARIABLES
Naming a Variable
Must be a valid identifier.
Must not be a keyword
Names are case sensitive.
Variables are identified by only first 32 characters.
Library commonly uses names beginning with _.
Naming Styles: Uppercase style and Underscore style
lowerLimit lower_limit
incomeTax income_tax
printf(
Can be“Enter
accessed by “any
the radius ); function defined below the declaration, in a file.
scanf(“%f” , &rad);
Examples:
include, main, printf, scanf, if, else, …
more as we cover C language
VALID/INVALID IDENTIFIERS
Valid
Invalid
sum
7of9
c4_5
x-name
A_NUMBER
name with spaces
longnamewithmanychars
1234a
TRUE
int
_split_name
AXYZ&
MAIN FUNCTION
Every program has one function main
Header for main: int main ()
Program is the sequence of statements between the { } following
main
Statements are executed one at a time from the one immediately
following to main to the one before the }
COMMENTS
Text between /* and */
Used to “document” the code for the human reader
Ignored by compiler (not part of program)
Have to be careful
comments may cover multiple lines
ends as soon as */ encountered (so no internal comments - /* An /* internal */
comment */)
COMMENT EXAMPLE
#include <stdio.h>
Derived types
composed of other types
INTEGER LITERAL
CONSTANTS
Syntax:
1 or more digits
Optional leading sign (+ or -)
Optional l or L at the end for long
Optional u or U for unsigned
Examples:
5, -35, 401, 4010L, -350L, 2000UL
FLOATING-POINT TYPE
Type names:
float
double
long double
Magnitude limited
cannot differentiate between values such as 1.00000000 and 1.00000001
FLOATING-POINT LITERALS
Syntax:
Zero or more digits, decimal point, then zero or more digits (at least one digit)
Whole numbers also treated as float
Optional sign at start
Can be followed by e and whole number (to represent exponent)
f or F at end for float
l or L at end for long double
scanf(“%c %d %f”,&cVar,&dVar,&fVar);
attempts to read first a single character, then a whole number, then a floating
point number from the keyboard
FORMATTED INPUT (CONT)
Generally only have field specifications and spaces in string
any other character must be matched exactly (user must type that char or
chars)
space characters indicate white-space is ignored
“white-space” - spaces, tabs, newlines
%d and %f generally ignore leading white space anyway (looking for numbers)
%d and %f read until next non-number char reached
FORMATTED INPUT (CONT)
More notes
can use width in field specifications to indicate max number of characters to
read for number
computer will not read input until return typed
if not enough input on this line, next line read, (and line after, etc.)
inappropriate chars result in run-time errors (x when number expected)
if end-of-file occurs while variable being read, an error occurs
ADDRESS OPERATOR
& - address operator
Put before a variable (as in &x)
Tells the computer to store the value read at the location of the
variable
More on address operators later
SCANF RULES
Conversion process continues until
end of file reached
maximum number of characters processed
non-number char found number processed
an error is detected (inappropriate char)
4.056 56
-543 stored in x, A stored in c, 4.056 stored in y, space and 56 still
waiting (for next scanf)
PROMPTING FOR INPUT
Using output statements to inform the user what information is
needed:
printf(“Enter an integer: “);
scanf(“%d”,&intToRead);