Uploaded by hosamshalghom

lech 1

advertisement
Programming in C language
Lecture 1: introduction
Omymah sarkez
Fall_2023
What is C?
• C is a computer programming language. That means that you can use C to
create lists of instructions for a computer to follow.
• C is a compiled language. when you write your C program, you must run it
through a C compiler to turn your program
• The form of a C program
• comments
• preprocessor commands
• functions
• declarations
• variables
• statements
The simplest C program
# i n c l u d e < s t d i o . h>
i n t main ( void )
{
p r i n t f ( " Thi s i s o u t p u t f rom my f i r s t program ! \ n " ) ;
return0;
}
Output
This is output from my first program!
#include <stdio.h>.
This line includes the
“standard input/otput library”
int main(void).
declares the main function.
Printf ().
to send output to screen
return 0.
to return an error code of 0
The form of a C program
• program is made up of functions.
• functions are made up of statements
between {}.
• The function main() does not have to be at
the top of a program so a C program does
not necessarily start at line.
Variables
In C, there are several standard types for variables:
• int - integer (whole number) values
• float - floating point values
• char - single character values (such as “m” or “Z”)
int b ;
b=5 ;
printf ("%d " , b ) ;
• create a space called b that is able to hold
one integer value.
• Put 5 value in b space .
• Output is
5
Input and output
p r i n t f ( " The t emp e r a t u r e i s " ) ;
1. The printf statement
print all of the C types with printf by
p r i n t f ("%d " , b ) ;
printf("degrees\n");
Output:
The temperature is 5 degrees
using different placeholders :
• int (integer values) uses %d
• float (floating point values) uses %f
p r i n t f ( " The t emp e r a t u r e i s %d d e g r e e s \ n " , b ) ;
Output:
The temperature is 5 degrees
• char (single character values) uses %c
• character strings (arrays of
characters, discussed later) use%s
p r i n t f ("%d + %d = %d \ n " , a , b , c ) ;
Output: if a = 5 b = 10 C = a + b
5 + 10 = 15
Input and output
2. The scanf statement
The scanf function allows you to accept input
from standard in.
s c a n f ("%d " , &a ) ;
s c a n f ("%d " , &b ) ;
• [ &] in front of a and b. This is the address
operator in C.
• You must use the & operator in scanf on any
variable of type char, int, or float.
# include < stdio. h>
int main ( void )
{
Int a , b , c ;
printf( " En t e r t h e f i r s t v a l u e : " ) ;
scanf ("%d " , &a ) ;
printf ( " En t e r t h e s e cond v a l u e : " ) ;
scanf ("%d " , &b ) ;
c=a+b;
printf ("%d + %d = %d \ n " , a , b , c ) ;
return 0 ;
}
Defining Constants
• There are two simple ways in C to define constants:
1. Using #define preprocessor.
2. Using const keyword
#define
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Output:
value of area : 50
const
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Arithmetic Operators
operator is a symbol that tells the compiler to perform specific mathematical
or logical manipulations
A holds 10 and variable B holds 20
#include <stdio.h>
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Line 1 - Value of c is %d\n", c );
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
Example
Output:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
Relational Operators
Assume variable A holds 10 and variable B holds 20, then:
Logical Operators
Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0
Example
#include <stdio.h>
main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
printf("Line 1 - Condition is
true\n" );
}
if ( a || b )
{
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b )
{
printf("Line 3 - Condition is true\n" );
}
else
{
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) )
{
printf("Line 4 - Condition is true\n" );
}
}
Output:
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
END
Questions
• Name the six basic things which make up a C program.
• Does a C program start at the beginning? (Where is the beginning?)
ref
• C Programming Tutorial, book
Download