0% found this document useful (0 votes)
26 views17 pages

Lech - 1

This document provides an introduction to programming in the C language. It discusses what C is, the basic structure of a C program including comments, functions, variables and data types. It also covers input/output, arithmetic operators, relational operators, logical operators and control flow statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views17 pages

Lech - 1

This document provides an introduction to programming in the C language. It discusses what C is, the basic structure of a C program including comments, functions, variables and data types. It also covers input/output, arithmetic operators, relational operators, logical operators and control flow statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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> #include <stdio.h>.
This line includes the
i n t main ( void )
“standard input/otput library”
{
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 " ) ; int main(void).
return0; declares the main function.
}
Printf ().
Output to send output to screen
This is output from my first program! 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 ; • create a space called b that is able to hold


one integer value.
b=5 ;
• Put 5 value in b space .
printf ("%d " , b ) ; • Output is 5
Input and output
p r i n t f ( " The t emp e r a t u r e i s " ) ;
p r i n t f ("%d " , b ) ;
1. The printf statement
printf("degrees\n");

print all of the C types with printf by Output: The temperature is 5 degrees
using different placeholders :
• int (integer values) uses %d 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 ) ;
• float (floating point values) uses %f Output: The temperature is 5 degrees
• char (single character values) uses %c
• character strings (arrays of p r i n t f ("%d + %d = %d \ n " , a , b , c ) ;

characters, discussed later) use%s


Output: if a = 5 b = 10 C = a + b 5 + 10 = 15
Input and output
2. The scanf statement
# include < stdio. h>
The scanf function allows you to accept input int main ( void )
from standard in. {
s c a n f ("%d " , &a ) ; Int a , b , c ;
printf( " En t e r t h e f i r s t v a l u e : " ) ;
s c a n f ("%d " , &b ) ; scanf ("%d " , &a ) ;
printf ( " En t e r t h e s e cond v a l u e : " ) ;
• [ &] in front of a and b. This is the address scanf ("%d " , &b ) ;
operator in C. c=a+b;
• You must use the & operator in scanf on any printf ("%d + %d = %d \ n " , a , b , c ) ;
variable of type char, int, or float. return 0 ;
}
Defining Constants
• There are two simple ways in C to define constants:
1. Using #define preprocessor.
2. Using const keyword
#define const
#include <stdio.h> #include <stdio.h>
#define LENGTH 10 int main()
#define WIDTH 5 {
#define NEWLINE '\n' const int LENGTH = 10;
int main() const int WIDTH = 5;
{ const char NEWLINE = '\n';
int area; int area;
area = LENGTH * WIDTH; area = LENGTH * WIDTH;
printf("value of area : %d", area); printf("value of area : %d", area);
printf("%c", NEWLINE); printf("%c", NEWLINE);
return 0; return 0;
} }

Output: value of area : 50


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> Example
main()
{
int a = 21;
int b = 10; Output:
int c ;
c = a + b; Line 1 - Value of c is 31
printf("Line 1 - Value of c is %d\n", c ); Line 2 - Value of c is 11
c = a - b;
Line 3 - Value of c is 210
printf("Line 2 - Value of c is %d\n", c );
c = a * b; Line 4 - Value of c is 2
printf("Line 3 - Value of c is %d\n", c ); Line 5 - Value of c is 1
c = a / b;
Line 6 - Value of c is 21
printf("Line 4 - Value of c is %d\n", c );
c = a % b; Line 7 - Value of c is 22
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 );
}
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
printf("Line 2 - Condition is true\n" );
#include <stdio.h> }
main() /* lets change the value of a and b */
a = 0;
{ b = 10;
int a = 5; if ( a && b )
int b = 20; {
printf("Line 3 - Condition is true\n" );
int c ; } Output:
if ( a && b ) else
Line 1 - Condition is true
{
{ printf("Line 3 - Condition is not true\n" ); Line 2 - Condition is true
printf("Line 1 - Condition is } Line 3 - Condition is not true
true\n" ); if ( !(a && b) ) Line 4 - Condition is true
} {
printf("Line 4 - Condition is true\n" );
if ( a || b ) }
{ }
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

You might also like