Sie sind auf Seite 1von 36

Example ?

:
Declarations and Initializations
char a = a, b = b;
int i = 1, j = 2;
double x = 7.07;
Expression Value Type
i == j ? a 1 : b + 1
j % 3 == 0 ? i + 4 : x
j % 3 ? i + 4 : x
99
int
7.07 double
5.0 double
Types, Constants
Types are categories of values.
C provides four fundamental data types:
char, int, float and double.
These are all keywords.
Other data types like arrays, pointers and
structures are derived from the
fundamental data types.
Constants are values of basic types
a, 7, 65.0 etc
Character
Includes lower and upper case letters,
digits, punctuation and special
characters.
Size of a character is 1 byte.
There are no character constants, all
character constants are integers.
ANSI C provides three types of
character char, signed char, unsigned
char
Integers (int)
Natural nos. 0,1,2 .. and their negatives
also there are octal and hexadecimal
integers.
Size of an integer is dependent on the
m/c but usually 4 bytes.
If the value assigned to an integer
variable is greater than the maximum
allowable value, integer overflow occurs.
Integral Types
short
provides less storage, usually 2 bytes.
long
provides more storage (than int but not
necessary), usually 4 bytes.
unsigned
have no sign.
Floating Types
Use to hold real values
Three types float (4 bytes), double (8
bytes) and long double (10 bytes).
Suffix can be appended to specify its
type 1.2F, 1.2D, 1.2L
Precision and range.
typedef
Explicitly associate a type with an
identifier
typedef int integer;
typedef unsigned long size_t;
Allows abbreviating long declarations,
provide meaningful type names
Fundamental Data Types
Type Declarations
Character char/ signed
char
unsigned char
Integer short unsigned short
int unsigned
long unsigned long
float float
Double double long double
Data Types and their Size
Variables
Variables are name of a
location in memory that
hold values
int i, sum;
All variables must be
declared before they can
be used
Expressions
A constant is an expression, eg. 3.0
A variable is an expression, eg. a
A function call is an expression,Eg printf(..)
Any meaningful combinations of constants,
variables, operators and function calls,
eg. c=a +b
Statments
Expression Statement:
sum+=i;
Selection Statement:
if .. else
Control a programs flow of control
(Iteration Statement)
while, do .. while, etc
Empty statement, compound statement
;, { }
sizeof Operator
Syntax: sizeof(object)
object can be type (int, float etc) or an
expression (a+b)
Returns the number of bytes needed to
store an object.
printf(char: %u\n, sizeof(char));
printf(Int: %u%u%u\n, sizeof(int));
printf(char: %u%u%u\n, sizeof(a+b));
getchar() and putchar()
#include<stdio.h>
main(void) {
int c;
while((c=getchar()) != EOF) {
putchar(c);
putchar(c);
}
}
getchar() and putchar() (2)
#include<stdio.h>
main(void) {
int c;
while((c=getchar()) != EOF) {
putchar(c);
putchar(c);
}
}
getchar() and putchar() (3)
#include<stdio.h>
main(void) {
int c;
while((c=getchar()) != EOF) {
putchar(c);
putchar(c);
}
}
getchar() and putchar() (4)
#include<stdio.h>
main(void) {
int c;
while((c=getchar()) != EOF) {
putchar(c);
putchar(c);
}
}
Another getchar() Example
Write a program that takes a string of
characters as input and prints the number
of blanks, digits, letters, newlines and
others.
#include<stdio.h>
main() {
int count_blnk=0;
int count_digits=0;
int count_letters=0;
int count_nl=0;
int count_others=0;
int c;
.
}
Declarations
#include<stdio.h>
main() {
// Declaration
while((c=getchar())!=EOF) {
.
}
The Loop
while((c=getchar())!=EOF) {
if (c == ) ++count_blnk;
else if (c >= 0 && c <= 9) ++count_digits;
else if (c >= a && c <= z || c >= A && c <= Z)
++count_letters;
else if (c == '\n') ++count_nl;
else ++count_others;
}
printf("Blanks = %d\t ..);
}
The if else statement
Conversions and Casts
Type conversion or casting consists of
converting an entity of one data type to
another data type.
Implicit type conversion also known as
coercion is done by the compiler
Explicit type conversion is explicitly
defined within a program called casting
or casts
Integral promotion
In an expression
if all the values of the original type can be
represented by an int, then the value is
converted to an int. Example:
char c = A;
Otherwise it is converted to an unsigned
int.
Arithmetic Conversion Rules
Operand
(a)
Other op-
erand (b)
Result
long double ? (any) b is converted to long double
double ? (any but not ld) b is converted to double
float ? (.) b is converted to float
unsigned long ? (...) b is converted to unsigned long
long unsigned Either long or unsigned
long ? (..) long
unsigned ? (..) unsigned
The rules are applied in the following order
Conversion Example
char c; short s; int i;
long l; unsigned u; unsigned long ul;
float f; double d; long double ld;
Expression Type Expression Type
c s / i u * 7 i
u * 2.0 i 7 * s * ul
ld + c c + 3
2 * I / 1 d = i
int unsigned
double unsigned long
long double int
long
double
Casts
Explicit conversion is called casting
Syntax: (type) identifier
Examples
(double) i;
f = (float) ((int) d + 1);
Functions
Functions
Modularize a program
All variables defined inside functions are
local variables
Parameters
Communicate information between functions
Local variables
Benefits of functions
Divide and conquer
Software reusability
Avoid code repetition
Program to compute
factorial N
#include<stdio.h>
int factorial(int n);
int main(void) {
int input, output;
scanf("%d", &input);
output = factorial(input);
printf("Factorial of %d is %d\n", input, output);
}
int factorial(int n) {
int i, prod = 1;
for (i = 2; i <= n; ++i)
prod *=i;
return prod;
}
Program to compute
factorial N
#include<stdio.h>
int factorial(int n);
int main(void) {
int input, output;
scanf("%d", &input);
output = factorial(input);
printf("Factorial of %d is %d\n", input, output);
}
int factorial(int n) {
int i, prod = 1;
for (i = 2; i <= n; ++i)
prod *=i;
return prod;
}
Program to compute
factorial N main() function
#include<stdio.h>
int factorial(int n);
int main(void) {
int input, output;
scanf("%d", &input);
output = factorial(input);
printf("Factorial of %d is %d\n", input,
output);
}
Function Declaration
Function declarations are generated in
various ways:
function invocation
compiler assumes int f();
no assumption about parameter list
function definition
provides both declaration and defintion.
no assumption about parameter list
function declaration/prototype
Function Declaration (2)
Functions should be declared before
they can be used, called the function
prototype.
Tells the compiler the number and type
of argument that will be passed to the
function and the type of value that will
be returned.
Syntax: type function_name(parameter
type list)
Program to compute
factorial N
#include<stdio.h>
int factorial(int n);
int main(void) {
int input, output;
scanf("%d", &input);
output = factorial(input);
printf("Factorial of %d is %d\n", input, output);
}
int factorial(int n) {
int i, prod = 1;
for (i = 2; i <= n; ++i)
prod *=i;
return prod;
}
Program to compute
factorial N factorial() function
int factorial(int n) {
int i, prod = 1;
for (i = 2; i <= n; ++i)
prod *=i;
return prod;
}
Function Definitions
Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}
Function-name: any valid identifier
Return-value-type: data type of the result
(default int) will be converted if necessary
void indicates that the function returns nothing
Parameter-list: comma separated list,
declares parameters
36
Function Definitions (2)
Definitions and statements: function
body (block)
Variables can be defined inside blocks (can be
nested)
Functions can not be defined inside other functions
Returning control
If nothing returned
return;
or, until reaches right brace
If something returned
return expression;

Das könnte Ihnen auch gefallen