Beginning C Programming for Engineers
Lecture 1: Introduction to C Programming
- R. Lindsay Todd
Introduction – p. 1/22
Beginning C Programming for Engineers Lecture 1: Introduction to C - - PowerPoint PPT Presentation
Beginning C Programming for Engineers Lecture 1: Introduction to C Programming R. Lindsay Todd Introduction p. 1/22 Goals of this course Introduce general concepts of programming . Begin to think like a programmer . Learn to appreciate
Introduction – p. 1/22
Introduction – p. 2/22
Introduction – p. 3/22
Introduction – p. 4/22
Source: The program as written by the programmer. Object: The program as translated by the “compiler” into the
Introduction – p. 5/22
Introduction – p. 6/22
Introduction – p. 7/22
Introduction – p. 8/22
Introduction – p. 9/22
Introduction – p. 10/22
Introduction – p. 11/22
1
/* Hello, world program */
2
#include <stdio.h>
3 4
int
5
main()
6
{
7
printf("hello, ");
8
printf("world\n");
9
return 0;
10
}
Introduction – p. 12/22
1
/* Hello, world program */
2
#include <stdio.h>
3 4
int
5
main()
6
{
7
printf("hello, ");
8
printf("world\n");
9
return 0;
10
}
h e l l
w
l d \n \0
Introduction – p. 13/22
Introduction – p. 14/22
Introduction – p. 15/22
Introduction – p. 16/22
Introduction – p. 17/22
1
#include <stdio.h>
2 3
int
4
main()
5
{
6
printf("Ten: %d\n", 10);
7
printf("Ten and 5 tenths: %f,\n %e, %g\n",
8
10.5, 10.5, 10.5);
9
printf("Ten: %s\n", "ten");
10
printf("Oops! %d\n", 10.5);
11
return 0;
12
}
Ten: 10 Ten and 5 tenths: 10.500000, 1.050000e+01, 10.5 Ten: ten Oops! 0
Introduction – p. 18/22
Variable Name for a memory object. Can be used in
Declaration Tells compiler about variable and its type.
Assignment Inserts a value into a memory object. Don’t
Introduction – p. 19/22
1
/* Program using variables. */
2 3
#include <stdio.h>
4 5
int
6
main()
7
{
8
int a, b, c;
9 10
a = 3;
11
b = 4;
12
c = a + b;
13
printf("Sum: %d + %d -> %d\n", a, b, c);
14
return 0;
15
}
Introduction – p. 20/22
1
#include <stdio.h>
2 3
int
4
main()
5
{
6
int a1, a2, sum;
7
printf("Enter a1, a2: ");
8 9
scanf("%d, %d", &a1, &a2);
10 11
sum = a1 + a2;
12
printf("The sum is %d\n", sum);
13
printf("I can lie: %d - %d = %d\n",
14
a1, a2, sum);
15 16
return 0;
17
}
Introduction – p. 21/22
1
#include <stdio.h>
2 3
int
4
main()
5
{
6
int a1, a2, sum;
7
printf("Enter a1, a2: ");
8 9
scanf("%d, %d", &a1, &a2);
10 11
sum = a1 + a2;
12
printf("The sum is %d\n", sum);
13
printf("I can lie: %d - %d = %d\n",
14
a1, a2, sum);
15 16
return 0;
17
}
Enter a1, a2: 7, 3 The sum is 10 I can lie: 7 - 3 = 10
Introduction – p. 22/22