02-Introduction-to-C
02-Introduction-to-C
Topic 02
Dennis Ritchie
Sept 1941 – Dec 2011
Prepared by: Jay Vince D. Serato
Introduction to C
Why was it created?
• You might also see C89 referred to as "ANSI C," "ANSI/ISO C"
or "ISO C."
• Compiling
• Linking
• Executing
• The input to this stage is the file you produce during your editing,
which is usually referred to as a source file.
• The compiler can detect a wide range of errors that are due to
invalid or unrecognized program code, as well as structural
errors where, for example, part of a program can never be
executed.
Prepared by: Jay Vince D. Serato
Introduction to C
Compiling
• The linker can also detect and report errors, for example, if
part of your program is missing or a nonexistent library
component is referenced.
#include <stdio.h>
int main(void)
{
printf("Hello world!");
return 0;
}
Prepared by: Jay Vince D. Serato
Introduction to C
Step Two (Your second
• Type exactly what is here:
program)
#include <stdio.h>
int main(void)
{
printf(“Don\’t give up! Try and try again!");
return 0;
}
• The directive
#include <stdio.h> /* printf definitions */
notifies the preprocessor that some names used in the
program (such as printf) are found in the standard header
file <stdio.h>.
• The function body is the bit between the opening and closing
braces that follow the line where the function name appears.
#include <stdio.h>
int main(void) {
printf("\nMy formula for success?\nRise early.");
return 0;
}
• Detailed Design
• Implementation
• Testing
• Every variable has a name, and you can use that name to refer
to that place in memory to retrieve what it contains or store a
new data value there.
• The memory cell for a and b must contain valid information (in this
case, a real number) before the assignment statement is executed.
Prepared by: Jay Vince D. Serato
Introduction to C
Assignment
#include <stdio.h>
Operators
int main(void) {
int Total_Pets;
int Cats;
int Dogs;
int Ponies;
int Others;
/* Set the number of each kind of pet */
Cats = 2;
Dogs = 1;
Ponies = 1;
Others = 46;
/* Calculate the total number of pets */
Total_Pets = Cats + Dogs + Ponies + Others;
printf("We have %d pets in total", Total_Pets); /* Output the result */
return 0;
Prepared by: Jay Vince D. Serato
} Introduction to C
The / and % Operators
• When an int is divided by an int, its result is the closest
integer less than or equal to the answer.
• 49.0/10.0 = 4.9
• 49/10 = 4
• 15%10 = 5
• 16%4 = 0
• 91%9 = 1
/* Calculate how many cookies each child gets when they are divided up */
cookies_per_child = cookies/children; /* Number of cookies per child */
printf("You have %d children and %d cookies", children, cookies);
printf("\nGive each child %d cookies.", cookies_per_child);
/* Calculate how many cookies are left over */
cookies_left_over = cookies%children;
printf("\nThere are %d cookies left over.\n", cookies_left_over);
return 0;
}
Prepared by: Jay Vince D. Serato
Introduction to C
Input/Output Operations