First C Program

Download Report

Transcript First C Program



Computer Model
Information presentation
◦ 2^n – 1



Programming languages
Programming
Algorithm examples:
◦ Echo
◦ Divide 2 numbers without using division and
multiplication


Program “Build” – compilation, link, run, debug
History of C language
Begin
Print Hello
World
End
#include <stdio.h>
/* My first program */
void main() {
printf ("Hello world\n");
}

int k
◦ Type of variable = size
◦ Adds address of ‘k’ to Symbol table
j

int j, k;
k

k = 2;
k
2

j = 7; <-- line 1
j
7

k = j; <-- line 2
k
7



Named computer memory area
Contain data of various types
The only correct way to access memory






int i;
int j,k;
char a;
char a[10];
float f;
unsigned int ui = 0;

First symbol can not be digit
◦ 5j – not valid

Case sensitive
◦ Max_num not equal to max_num;

Meaningful names
◦ Max_num, first_name, personal_record - good
◦ I, j, ii, aa, z - bad




char – a single byte character.
int – integer number – usually 4 bytes.
float – a single precision real number –
usually 4 bytes.
double – a double precision real number –
usually 8 bytes.




short int (or just short) – an integer number,
usually 2 bytes
long int (or just long) – an integer number,
4 or 8 bytes
long double - a double precision real number
– usually 8 bytes
unsigned vs. signed
Char
int
float
double


#include <stdio.h>
int main()
{
printf("size of a short is %d\n", sizeof(short));
printf("size of a int is %d\n", sizeof(int));
printf("size of a long is %d\n", sizeof(long));
}

int j, k;
◦ k = 2;
◦ j = 7; <-- line 1
◦ k = j; <-- line 2









int k;
int *p;
k = 2;
*p = 4;
p = &k;
printf(“%d”, *p);
Rvalue – address of &k – named region of storage
Lvalue – value of integer k
Ptr=NULL – zero pointer macro





#include <stdio.h>
int j, k;
int *ptr;
int main(void)
{
j = 1;
k = 2;
ptr = &k;
printf("j has the value %d and is stored at %p\n", j, (void *)&j);
printf("k has the value %d and is stored at %p\n", k, (void *)&k);
printf("ptr has the value %p and is stored at %p\n", ptr, (void
*)&ptr);
◦ printf("The value of the integer pointed to by ptr is %d\n", *ptr);
return 0;
◦
◦
◦
◦
◦
◦

}




ANSI compiler initializes global variables
Not initialized variables value is not defined
Is my compiler ANSI compiler?
Is my variable global?
Write a program that accepts as input ◦ The Dollar-Shekel exchange rate
◦ An integer amount of dollars
and outputs ◦ The equivalent amount in Shekels
#include <stdio.h>
int main()
{
double shekels, rate;
int dollars;
printf("Enter the US$-NIS exchange rate: ");
scanf("%lf", &rate);
printf("Enter the amount of dollars: ");
scanf("%d", &rate);
shekels = dollars * rate;
printf("%d dollars = %g shekels\n", dollars, shekels);
return 0;
}







A %<conversion code> in the printf/scanf
string is replaced by the respective variable.
%c – a character
%d – an integer, %u – an unsigned integer.
%f – a float
%lf – a double
%g – a nicer way to show a double (in printf)
%% - the ‘%’ character (in printf)


A char variable is used to store a text
ASCII - American Standard Code for
Information Interchange :
◦
◦
◦
◦
◦
Letters.
Digits.
Keyboard signs.
Non-printable characters.
But also small numbers (0 to 255 or -128 to 127).



The table above shows only 128 characters (7
bits). Some are non-printable.
Extended ASCII code contains 256 characters.
Note contiguous sets of numbers, upper case
and lower case characters.
#include <stdio.h>
int main()
{
char i = 'b';
printf("i as a character is %c\n", i);
printf("i as an integer is %d\n", i);
printf("The character after %c is %c\n",
i, i + 1);
return 0;
}
/* Convert a letter to uppercase */
#include <stdio.h>
int main()
{
char letter;
printf("Please enter a lowercase letter\n");
scanf("%c", &letter);
printf("This letter in uppercase is %c\n",
letter - 'a' + 'A');
return 0;
}