CSE115 Lec04 OverviewOfC
CSE115 Lec04 OverviewOfC
Lecture 4: Overview of C
Example:
#include <stdio.h>
int main()
{
/*my first program in C*/
printf(“Hello, World! \n”);
return 0;
}
Example:
• Preprocessor:
– A system program that modifies a C program prior to its
compilation.
• Preprocessor Directives:
– A C program line beginning with # that provides an instruction to
the preprocessor.
– The two most common directives are #include and #define
– The directive gives a program access to a library. This directive
causes the preprocessor to insert definitions from a standard
header file into a program before compilation.
– Example: In our “Hello World” program the header file stdio.h
contains information about standard input and output functions
such as printf.
At the beginning
After user enters: 10.5 to After this line is executed:
Do not assume that
uninitialised variables scanf("%f", &miles); kms = KMS_PER_MILE * miles;
contain zero! (Very
common mistake.)
• Valid Identifiers
letter_1, letter_2, inches, cent,
CENT_PER_INCH, Hello, variable
• Invalid Identifiers
• Example:
• Variable:
– A name associated with a memory cell whose value can change
• Variable declarations:
– Statements that communicate to the compiler the names of
variables in the program and the kind of information stored in
each variable.
– SYNTAX: <variable_type> variable_list;
– Examples:
int count;
double x, y, z;
char first_initial;
– C requires that every variable used in a program would be
declared.
• float
– Abstraction of numbers that has fractional parts.
– Keyword: float
float length;
length = 5.64;
• Double
– Abstraction of real numbers consisting both integral part and a
fractional part
– Scientific notation can also be used to represent real numbers as
‚double‘ type.
– Keyword: double
double value;
value = 7E5; //value = 700000
• char
– Represents an individual character value – a letter, a digit, or a
special symbol.
– Each type char value is enclosed in apostrophes (single quotes)
like this: ‘A’ ‘z’ ‘2’ ‘9’ ‘*’ ‘:’
‘”’ ‘ ‘
• ASCII code
– A particular code that specifies the integer representing each
char value.
– ASCII: American Standard Code for Information Interchange
• Input operation:
– An instruction that copies data from an input device into memory.
• Output operation:
– An instruction that displays information stored in memory.
• Input/output function:
– A C function that performs an input or output operation.
– Most common input/output functions are supplied as part of the
C standard input/output library to which access can be gained
through the preprocessor directive
#include <stdio.h>
– Some common input/output functions in stdio.h:
• printf()
• scanf()
• getchar()
• putchar()
• Example:
– printf(“Thank you”);
– printf (“Total sum is: %d\n”, sum);
• %d is a placeholder (conversion specifier)
–marks the display position for a type
integer variable
• \n is an escape sequence
–moves the cursor to the new line
• Format string:
– In a call to printf, a string of characters enclosed in double
quotes (“”), which specifies the form of the output line.
• Placeholder:
– A symbol beginning with % in a format string that indicateswhere
to display the output value.
• Escape Sequence
• If you want the user to enter more than one value, you
serialize the inputs.
• Example:
float height, weight;
printf(“Enter height and weight: ”);
scanf(“%f %f”, &height, &weight);