Intro To C
Intro To C
Example: 10100010
01011011
10101010
2) Assembly Languages:
• English-like abbreviations representing elementary computer
operations.
• translated to machine code by using assemblers.
3) High-level Languages:
• Codes similar to everyday English
• Use mathematical notations
• translated to machine code by using compilers.
• C, C++, PASCAL, FORTRAN, BASIC are high-level languages.
Example:
c=a+b;
if(a<b)
printf(“a is less than b\n”);
else
printf(“a is NOT less than b\n”);
2000 Prentice Hall, Inc. All rights reserved.
Basics of a Typical C Program Development Process
Program is created in
Editor Disk the editor and stored on disk.
• Phases of C Programs:
Preprocessor program
Preprocessor Disk processes the code.
1. Edit
Compiler creates object code
Compiler Disk and storesi it on disk.
2. Preprocess
Linker Disk Linker links the object
3. Compile code with the libraries
Primary Memory
4. Link Loader
Loader puts program in
memory.
5. Load Disk ..
..
..
Comments:
• Text surrounded by /* and */ is ignored by computer.
• Used to describe program.
#include <stdio.h>
Preprocessor directive:
• Tells computer to load contents of a header file <stdio.h>,
• which includes standard input/output functions.
• For example printf()is one of the standard input/output functions.
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
int main()
• C programs contain one or more functions,
• One of the functions must be main().
• Parenthesis used to indicate a function
• int means that main "returns" an integer value
• Braces ({ and }) indicate a block
• The bodies of all functions must be contained in braces.
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
printf("Hello World\n");
• printf()function print the string of characters within quotes (" ")
• All statements must end with a semicolon (;)
• \n is the newline character.
return 0;
• A way to exit a function.
• return 0, in this case, means that the program terminated normally.
Right brace }
• Indicates end of main has been reached.
Simple C Program:
• Example 1: Write a C program which displays your name and
surname in two consecutive lines .
day
C Program: Addition of two integer numbers
/* This program adds two integer numbers */
#include <stdio.h>
int main()
{
int a, b, sum; /* variable declarations */
printf("Enter first integer\n"); /* prompt the user */
scanf( "%d", &a); /* read first integer */
printf("Enter second integer\n"); /* prompt the user */
scanf( "%d", &b); /* read second integer */
sum = a + b; /* calculate the sum */
printf( "Sum = %d\n", sum ); /* print the calculated sum*/
return 0; /* indicate that program ended successfully */
}
• When executing the program the user responds to the scanf statement
by typing in a number, then pressing the enter (return) key.
• Operator precedence:
- Some arithmetic operators act before others
(i.e., multiplication before addition)
- Use parenthesis when needed
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while