Docsity

Understanding Variables and Loops in C Programming: A Beginner's Guide, Lecture notes of C programming

Partial preview of the text

Download Understanding Variables and Loops in C Programming: A Beginner's Guide and more Lecture notes C programming in PDF only on Docsity!

The C Language

C programC program C programC program example

exampleexampleexample

*/ This program prints a list of

  • integers, from 0 to 9. /*

{ int main (void)#include <stdio.h>

int i; for (i = 0; i < 10; i = i + 1)

printf (“i is %d\n”, i);

return 0;

}

Here’s example C program

it’s not much sophisticated

it also doesn’t do much of any practical use

variablesvariables variablesvariables

How we manipulate data in our program

Name associated with some memory

Has a specific data type

In this program:

a variable named

(^) i (^) is used to hold an integer value

  • as the program runs,

(^) i’ s value goes from 0 to 9

{ int main (void) #include <stdio.h>

int i; for (i = 0; i < 10; i

i

+ 1)

printf (“i is %d\n”,

i);

return 0;

loopsloops loopsloops

over and overA way of repeating the same instructions

deciding how many times to loop,C provides some flexible ways of

or when to exit a loop

second examplesecond example second examplesecond example

/ This program prints a list*

of

*** integers, from 0 to 9./*

#include <stdio.h> int main (void)

int i; for (i = 0; i < 10; i

i

+ 1)

printf (“i is %d\n”,

i);

return 0;

Here’s that include directive again.

like before, this allows printf to work

we’ll just do it now, without worrying about why

second examplesecond example second examplesecond example

/ This program prints a list*

of

*** integers, from 0 to 9./*

{ int main (void)#include <stdio.h>

for (i = 0; i < 10; i = i + 1)int i;

printf (“i is %d\n”,

i);

return 0;

  • And like before, this line introduces our

(^) main

(^) function.

  • remember that main is where the program starts running

second examplesecond example second examplesecond example

*/ This program prints a list of

  • integers, from 0 to 9.**

*/

{ int main (void)#include <stdio.h>

int i; for (i = 0; i < 10; i = i + 1)

printf (“i is %d\n”, i);

return 0;

}

This line introduces a variable.

it’s called a declaration

it tells the compiler that we’ll be using a variable

named i

it holds an integer

second examplesecond example second examplesecond example

/ This program prints a list*

of

*** integers, from 0 to 9.**

#include <stdio.h> int main (void)

for (i = 0; i < 10; i = i + 1)int i;

printf (“i is %d\n”,

i);

return 0;

  • }

Here is a loop

it will execute its contents 10 times

each time, i will have a different value

from 0

we could have writtenwe could have writtenwe could have writtenwe could have written

it like this…it like this…it like this…it like this…

*/ This program prints a list of

  • integers,**

from

*0 to 9. /

#include <stdio.h> int main (void)

{

int

i;

printf (“i is 9\n”);printf (“i is 8\n”);printf (“i is 7\n”);printf (“i is 6\n”);printf (“i is 5\n”);printf (“i is 4\n”);printf (“i is 3\n”);printf (“i is 2\n”);printf (“i is 1\n”);printf (“i is 0\n”);

return

0;

}

ok, what’s going on here?ok, what’s going on here?ok, what’s going on here?ok, what’s going on here?

We’ve just seen simple program

This is C program - not machine

language

something the machine understandsthe C compiler is used to turn them into

The C compiler is itself a program

  • it writes out (hopefully) a program the- it reads the C program you write

computer can run

so what’s a compiler?so what’s a compiler? so what’s a compiler?so what’s a compiler?

A compiler is a program that

  • languageconverts a C program into machine

remember, the computer can only run

machine language

so let’s look at the wholeso let’s look at the wholeso let’s look at the wholeso let’s look at the whole

processprocessprocessprocess

Write a program

text editor

Compile the program

C compiler

Will do one of two things:

print error messages

produce an executable program

Run the program

variable declarationsvariable declarationsvariable declarationsvariable declarations

must declare it.Before you can use a variable, you

a declaration introduces a variable name

associates it with a data type

qualifierssets some optional characteristics called