C Slide
C Slide
Programming
Outline
• History of C
• C Language Elements
• Executable Statements
• Arithmetic Expressions
History of C
• C was developed in 1972 by Dennis Ritchie at AT&T
Bell Laboratories.
• C was designed as a programming language to write
the Unix Operating System.
• C became the most commonly used language for
writing system software.
• C is machine independent: C programs can be
compiled to run on a wide variety of processors and
operating systems.
return 0;
} punctuations
Preprocessor Directives
• Preprocessor directives are commands that give
instructions to the C preprocessor.
• The preprocessor modifies a C program prior to its
compilation.
• Preprocessor directives begin with #
#include <stdio.h>
– Includes Standard I/O Library header file (.h file)
#include <math.h>
– Includes Standard Math Library header file (.h file)
#define PI 3.141593
– Defines the constant PI
#include Directives
• #include directive is used to include other source
files into your source file.
• The #include directive gives a program access to a
standard library.
• Standard Libraries contains useful functions and
symbols that are predefined by the C language.
– You must include <stdio.h> if you want to use the
printf and scanf library functions.
– stdio.h is called a header file (.h file). It contains
information about standard input and output functions that
are inserted into your program before compilation.
CSE 115 Programming Language I ECE@NSU
8
#define Directives
• The #define directive instructs the preprocessor to
replace each occurrence of a text by a particular
constant value before compilation.
• #define replaces all occurrences of the text you
specify with the constant value you specify
#define NAME value
#define KMS_PER_MILE 1.609
#define PI 3.141593
Reserved Words
• A word that has special meaning to C and can not be
used for other purposes.
Standard Identifiers
• Identifier - A name given to a variable or a function
• Standard Identifier - An identifier defined in a
standard C library and has special meaning in C.
– Examples: printf, scanf
– Standard identifiers are not reserved words
– You can redefine standard identifiers if you want to, but it is
not recommended.
– For example, if you define your own function printf,
then you cannot use the C library function printf.
Data Types
• Data Types: a set of values and a set of operations
that can be performed on those values
– int: Stores signed integer values: whole numbers
• Examples: 65, -12345
– double: Stores real numbers that use a decimal point
• Examples: 3.14159 or 1.23e5 (which equals 123000.0)
– char: Stores character values
• Each char value is enclosed in single quotes: 'A', '*'
• Can be a letter, digit, or special character symbol
– Arithmetic operations (+, -, *, /) and compare can be
performed on int and double variables. Compare
operations can be performed on char variables.
CSE 115 Programming Language I ECE@NSU
15
• Floating-Point Types in C
Type Size in Memory Approximate Range Significant Digits
float 4 bytes = 32 bits 10-38 to 10+38 6
double 8 bytes = 64 bits 10-308 to 10+308 15
Variable Declaration
• Variables: The memory cells used for storing a
program’s input data and its computational results
– The Value of a Variable can change at runtime
Executable Statements
• Executable Statements: C statements used to write or
code the algorithm. C compiler translates the
executable statements to machine code.
• Examples of executable Statements:
– Assignment Statements
– Function Calls, such as calling printf and scanf
– return statement
– if and switch statements (selection) - later
– for and while statements (iteration) - later
Assignment Statements
• Stores a value or a computational result in a variable
variable = expression;
Read = as "becomes"
The assignment operator does NOT mean equality
Placeholders
• Placeholders always begin with the symbol %
– % marks the place in a format string where a value will be
printed out or will be read
• Format strings can have multiple placeholders, if you
are printing multiple values
Displaying Prompts
• When input data is needed in an interactive program,
you should use the printf function to display a
prompting message, or prompt, that tells the user
what data to enter.
scanf("%c%c%c",
Letters
&letter1, C a r
Entered
&letter2, letter1
C
&letter3);
letter2
a
letter3
Return Statement
• Syntax: return expression ;
• Example: return (0);
• Returning from the main function terminates the
program and transfers control back to the operating
system. Value returned is 0.
• The return statement transfers control from a
function back to the caller.
• Once you start writing your own functions, you will
use the return statement to return the result of a
function back to the caller.
Comments
• Comments making it easier for us to understand the program,
but are ignored by the C compiler.
• Two forms of comments:
– /* C comment */ anything between /* and */ is
considered a comment, even if it spans on multiple lines.
– // C++ comment anything after // is considered a
comment until the end of the line.
• Comments are used to create Program Documentation
– Help others read and understand the program.
• The start of the program should consist of a comment that
includes programmer’s name, date, current version, and a brief
description of what the program does.
• Always Comment your Code!
CSE 115 Programming Language I ECE@NSU
31
Programming Style
• Why we need to follow conventions?
– A program that looks good is easier to read and understand
than one that is sloppy.
– 80% of the cost of software goes to maintenance.
– Hardly any software is maintained for its whole lifetime by
the original author.
– Programs that follow the typical conventions are more
readable and allow engineers to understand the code more
quickly and thoroughly.
• Check your text book and expert programmers on how
to improve your programming style.
CSE 115 Programming Language I ECE@NSU
32
White Space
• The compiler ignores extra blanks between words and
symbols, but you may insert space to improve the
readability and style of a program.
White Space
Bad: Good:
Arithmetic Expressions
• To solve most programming problems, you need to
write arithmetic expressions that compute data of type
int and double (and sometimes char)
• Arithmetic expressions contain variables, constants,
function calls, arithmetic operators, as well as sub-
expressions written within parentheses.
• Examples:
– sum + 1
– (a + b) * (c – d)
– (-b + sqrt(delta))/(2.0 * a)
Arithmetic Operators
Operator Meaning Examples
5 + 2 is 7
+ addition 5.0 + 2.0 is 7.0
'B' + 1 is 'C'
5 – 2 is 3
– subtraction 5.0 – 2.0 is 3.0
'B' – 1 is 'A'
5 * 2 is 10
* multiplication
5.0 * 2.0 is 10.0
5 / 2 is 2
/ division
5.0 / 2.0 is 2.5
% remainder 5 % 2 is 1
CSE 115 Programming Language I ECE@NSU
37
Operators / And %
Example Result Explanation
8/5 1 Integer operands integer result
8.0/5.0 1.6 floating-point operands and result
8 /-5 -1 One operand is negative negative result
-8 /-5 1 Both operands are negative positive result
8%5 3 Integer remainder of dividing 8 by 5
8 %-5 3 Positive dividend positive remainder
-8 % 5 -3 Negative dividend Negative remainder
int main(void)
{
int total; /* total score */
int students; /* number of students */
double average; /* average score */