C Language Intro
C Language Intro
INTRODUCTION
CSSE 120—Rose Hulman Institute of Technology
The C Programming
g g Language
g g
Invented in 1972 by y Dennis Ritchie at AT&T Bell
Labs.
Has been the main development language for UNIX
operating systems and utilities for a couple of
decades.
O Python
Our P th interpreter
i t t was written
itt iin C!
Used for serious coding on just about every
development platform.
platform
Especially used for embedded software systems.
Is usually compiled to native machine code.
code
Faster and less portable than Python or Java.
Why
y C in CSSE 120?
Practical
Several upper-level courses in CSSE, ECE, and Math
expect students to program in C.
None of these courses is a prerequisite for the others.
def printRootTable(n):
Parallel
f
for i in
i range(1,n):
(1 ) examples
print " %2d %7.3f" % (i, sqrt(i))
in Python
def main():
printRootTable(10) and C.
C
main()
#include <stdio.h>
#include <math.h>
void printRootTable(int n) {
int i;
for (i=1; i<=n; i++) {
printf(" %2d %7.3f\n", i, sqrt(i));
}
}
int main() {
printRootTable(10);
i tR tT bl (10)
return 0;
}
Q2-3
Recap:
p Comments in C
Python comments begin with # and continue until the
end of the line.
C comments begin with / /* and end with */.
/.
They can span any number of lines.
Some C compilers (including the one we are using)
also allow single-line comments that begin with //.
String
g constants in C
In Python, character strings can be surrounded by
single quotes (apostrophes), or double quotes
(quotation marks).)
(q
In C, only double quotes can surround strings (whose
type
yp in C is char*).)
char *s = "This is a string";
printf(s); /* more about printf() soon */
Single quotes indicate a single character, which is not the
same as a string whose length is 1. Details later.
char
h c = '
'x';
'
printf("%c\n", c);
Q4
printf statement
p
C: printf(" %2d %7.3f\n", i, sqrt(i));
Q5
Use If Statements or Else
if m % 2 == 0: if (m % 2 == 0) {
print "even" printf("even");
else: } else {
print "odd" printf("odd");
}
Python: C:
Colons and indenting Parentheses, braces
Or Else What?
if gpa > 2.0: if (gpa > 2.0) {
print "safe" printf("safe");
elif gpa >= 1.0: } else if (gpa >= 1.0) {
print "trouble" printf("trouble");
else: } else {
print "sqrt club" printf("sqrt club");
}
Python: C:
Colons and indenting Parentheses, braces
elif else if
Q6
Optional
p Braces
Braces group statements
Can omit for single
statement bodies
if (gpa > 2.0)
printf( safe );
printf("safe");
else if (gpa >= 1.0)
printf("trouble");
else
printf("sqrt club");
Danger,
g , Will Robinson!
What is printed in each if (n > 0)
case? if (a > 0)
Case n a printf("X");
1 1 1 else
2 -1 1 printf("Y");
3 1 -1
4 -1 -1
else goes with closest if Use braces
U b to
avoid confusion!
Indenting does not matter
to the compiler but use for
code readability!
Ahh. That's better!
What is printed in each if (n > 0) {
case? if (a > 0)
Case n a printf("X");
1 1 1 } else {
2 -1 1 printf("Y");
3 1 -1 }
4 -1 -1
Use braces
U b to
avoid confusion!
Does C have a boolean type?
yp 0
Enter the following C code in Eclipse:
void testBoolean(int n, int m) {
int p = n < m;
printf("Is
i tf("I %d lless th
than %d? %d\
%d\n",
" n,
m, p);
}
Add a couple of test calls to your main() function:
testBoolean(2,3); testBoolean(3,2);
0 in C is like False in Python
All other numbers are like True
Boolean operators
p in C
Python uses the words and, or, not for these
Boolean operators. C uses symbols:
&& means "and“
|| means "or“
! means "not“
Example uses:
if ( >= 3 && a <= 5)) { … }
(a
if (!same (v1, v2)) { …}
Q7
I Could While Away
y the Hours
How do you suppose the following Python code
would be written in C?
while n != 0:
n=n–1
print n
How do you break out of a loop in Python?
How do you suppose you break out of a loop in C?
Q8
A Little Input,
p , Please
To read input from user in C, use scanf()
Syntax: scanf(<formatString>, <pointer>, …)
Example:
int age;
scanf("%d",
scanf( %d , &age);
Another Example
p Pushes prompt string
to user before
b f asking
ki
for input.
To read input from user in C, use scanf()
Syntax: scanf(<formatString>, <pointer>, …)
Example:
double f, g;
printf("Enter two real numbers separated by a comma:");
fflush(stdout);
scanf("%lf,%lf", &f, &g);
printf("Average:
printf( Average: %5.2f\n",
%5.2f\n , (f + g)/2.0);
Comma is matched
ell-eff = "long
g float" against user input
Why not d for double?
Q9,10
Rectangular
g output
p in C
#include <stdio.h>
void rectangleSameNumEachRow(int numRows, int numCols) {
int i, j;
for (i=1; i<= numRows; i++) {
Output:
for (j
(j=1;
1; j<=numCols;
j< numCols; j++) { 11111111
printf ("%d", i); 22222222
}
printf ("\n");
33333333
}
}
int main() {
rectangleSameNumEachRow(3,
g ( , 8);
)
}
It's easier than Python because printf() does not
automatically add spaces like Python's
Python s print.
print
HW: try to finish nested loops (due next class), start
thatsPerfect (due in 2 classes)