View
297
Download
0
Category
Preview:
Citation preview
A brief introduction to C
Mohamed Elsayed Head of Technical Committee IEEE ZC
Before the programming language:
A circuit which adds two numbers
But one problem arises here. To be able to write a computer program by typing out billions of 1s and 0s would require superhuman brainpower, and even then it would probably take you a lifetime or two to write.
Using programming language:
You can say that any programming language is just like a dictionary which contains words that mean certain circuits. For example : addition, multiplication, etc.
Structure of a simple program :
Make the things simpler !• #include <stdio.h> includes the standard input output library
functions. The printf() function is defined in stdio.h . (you can think of a library as a dictionary contains words and their meanings)
• void main() The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value. (It’ll be explained soon)
• printf() The printf() function is used to print data on the console.
What is #include <stdio.h>• stdio stands for standard
input/output which contains functions that help in the process of input or output.• It’s more or less like including a
dictionary which contains words and their meanings (function and their definition)• Without it you cannot write printf()
because it will be meaningless to the compiler.
Data Types:
Declaring variables :
int x;int x = 5;float x ;float x = 3.3;char x ;char x = ‘F’;
printfThe syntax :printf(“format string”,arguments_list);
Place holders can be %d(integer), %c(character), %s(string), %f(float)
• Example : int x=50;printf(“My number is integer and its value is %d”,x);
printf and ‘\n’
Hello WorldHello World Hello WorldHello World
Do you want to enter some input?
• Scanf is a function• The syntax: scanf("%d", &a);
Do you have a compiler ?
Go to • http://www.tutorialspoint.com/compile_c_online.php• http://www.learn-c.org/
Semi-colon :
in C is like in English
Comments in C :
• Ignored by the compiler• Are used to enhance the readability of your code
Making decisions ?
Example:
I go to the university ..
IF Else
If and if-else statement :used to execute the code if condition is true or false.
The from of if-else statement:if(condition1){ //code to be executed if condition1 is true } else { // code to be executed if all the conditions are false }
Examples: Try it yourself ..
The number is greater than 50 ! Here is the rest of my program
Here is the rest of my program
Can you make the user enter the number instead of writing it in the program ?
Operators to do complicated jobs :
• If (x>5&&x<10) // if x is greater than 5 and less than 10• If (x==6||x==5) // if x is equal 6 or 5• If(x!=6) // if x is not equal to 6
It might be more branched ..
Loops are all around us !
The structure of a loop:
For loop syntax:•It is good if number of iteration is known by the user.•Syntax:-
for(initialization;condition;incr/decr){
//code to be executed }
For loop :
Example: Try it yourself ..The output :The current value of x is 0The current value of x is 1The current value of x is 2The current value of x is 3The current value of x is 4The current value of x is 5The current value of x is 6The current value of x is 7The current value of x is 8The current value of x is 9
While loop :while(condition){ //code to be executed } • It is better if number of iteration is not known by the user.
Example: Try it yourself ..
Break the loop under a certain condition !
Break statement: it is used to break the execution of loop (while, do while and for) and switch case.
Syntax:-jump-statement; break;
Example:
Functions:
Function form :
• Syntax to call function:-variable=function_name(arguments...);
A simple function : Try it yourself ..
Void in functions:• It’s the function that doesn’t return
anything !• It’s the function that doesn’t
take or return anything !
What’s next ?
CS50: Introduction to Computer Science Embedded Systems - Shape The World YouTube Tutorials
Arduino Program:int main(void){ setup();
while (1) // infinite loop loop(); return 0;}
void setup(){
}
void loop (){
}
The main function which contains calling 2 functions : setup and loop
The setup function which will be called only one time
The setup function which will be called only one time
An Arduino program:
Recommended