15-123 Effective Programming
in C and UNIX
Getting started with C
February 1st , 2012
Let us C
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int x;
int y = 34;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("x= %d y= %d\n", x, y);
return 0;
}
Variable Types
int
unsigned int
char
short
long
float
double
Assigning values
int i = 5;
double db = 4.1;
i = db;
You should use explicit casts to be safe
i = (int) db;
Boolean types
There are no boolean types in C
For all conditional tests
0 is interpreted as false
All other values are true
printf
printf("x= %d y= %d\n", x, y);
“x= %d y= %d\n” Format String
%d Placeholder for an int
d – int
u – unsigned int
f – double
x – unsigned int in hexadecimal
s – null terminated string (more on this later)
x replacement for %d (in order)
scanf
Read values from keyboard
scanf("%d", &x);
Read an int from the user and save it in x
scanf("%f", &x);
Read float from the user – x better be a float
scanf("%lf", &x);
Read doube from the user – x better be a double
scanf(“%d,%d”,&x,&y);
Read an int followed by a , followed by another in and
save in x and y.
Compiling your code
gcc demo1.c
gcc -ansi -pedantic -Wall -Wextra demo1.c
-ansi - only accept code in accordance with
the rules of ANSI C
-pedantic - issue all the warnings demanded
by strict ANSI C
-Wall - report all warnings
-Wextra - report a few things that -Wall
doesn't catch
Operations
Arithmetic
+, -, *, /, %
Comparison
== , != , > , < , >= , <=
Logical Operators
! , && , ||
Bitwise operators
~ , & , | , ^ , << , >>
Conditionals and Loops
if (condition)
{
If statements
}
if-else
if (condition)
{
if statements
} else
{
else statements
}
if-else if - else
if (condition 1)
{
condition 1 statements
} else if (condition 2
{
condition 2 statements
}
else
{
else statements
}
For loop
for (initialization; termination; update)
{
for block
}
for(int i =0; i < 100; i++)
{
printf(“%d\n”,i);
}
While loop
while (condition)
{
while block
}
int i = 0;
while(i < 100)
{
printf(“%d\n”,i);
i++;
Do-While loop
do
{
do-while block
} while (condition);
int i = 0;
do
{
printf(“%d\n”,i);
i++;
Functions
Defining a function
int add(int a, int b)
{
return a + b;
}
Using a Function
int main()
{
int x , y, sum;
printf (“Enter values you want to add > ”);
scanf(“%d %d”,&x, &y);
sum = add(x,y);
printf(“Sum of %d and %d is %d”,x,y,sum);
}
Putting it together
int add(int a, int b)
{
return a + b;
}
int main()
{
int x , y, sum;
printf (“Enter values you want to add > ”);
scanf(“%d %d”,&x, &y);
sum = add(x,y);
printf(“Sum of %d and %d is %d”,x,y,sum);
}
OR
int add(int a, int b);
int main()
{
int x , y, sum;
printf (“Enter values you want to add > ”);
scanf(“%d %d”,&x, &y);
sum = add(x,y);
printf(“Sum of %d and %d is %d”,x,y,sum);
}
int add(int a, int b);
{
return a + b;
}
Exercise
Create a function called CToF that takes a
temperature in Centegrade (double) as input
and returns the temperature in
Fahrenheit(double)
Create function called FtoC – It should be
obvious what this function does
Write a main function that
Asks the user to enter a temperature
Ask the user to enter the unit (c or f)
Converts the temperature from Celcius to Fahrenheir
or vice versa and prints the result
© Copyright 2026 Paperzz