Learning C Programming (Basic)
Learning C Programming (Basic)
Language
Table of Contents
• Introduction
• C Program Structure
• Variables, Expressions, &
Operators
• Input and Output
2
C Programming
Introduction
• Why Learn C?
3
C Programming
Why Learn C?
4
C Programming
C Program Structure
5
C Programming
Canonical First Program
#include <stdio.h>
main()
{
/* My first program */
printf("Hello World! \n");
}
6
C Programming
Canonical First Program Continued
#include <stdio.h>
main()
{
/* My first program */
printf("Hello World! \n");
}
7
C Programming
More on the Canonical First Program
#include <stdio.h>
main()
{
/* My first program */
printf("Hello World! \n");
}
/* My first program */
• Comments can be inserted into C programs by bracketing text with the /*
and
*/ delimiters. As will be discussed later, comments are useful for a variety of
reasons. Primarily they serve as internal documentation for program
structure and functionality.
9
C Programming
Header Files
• Header files contain definitions of functions and variables which can be
incorporated into any C program by using the pre-processor #include statement.
Standard header files are provided with each compiler, and cover a range of areas:
string handling, mathematics, data conversion, printing and reading of variables,
etc.
• To use any of the standard functions, the appropriate header file should be included.
This is done at the beginning of the C source file. For example, to use the function
printf() in a program, the line
#include <stdio.h>
• should be at the beginning of the source file, because the declaration for printf()
is found in the file stdio.h. All header files have the extension .h and generally
reside in the /usr/include subdirectory.
#include <string.h>
#include <math.h>
#include "mylib.h"
• The use of angle brackets <> informs the compiler to search the compiler’s include
directories for the specified file. The use of the double quotes "" around the
filename informs the compiler to start the search in the current directory for the
specified file. 10
C Programming
Comments
• Note that the /* opens the comment field and the */ closes the comment
field. Comments may span multiple lines. Comments may not be nested
one inside the another.
/* this is a comment. /* this comment is inside */ wrong */
• In the above example, the first occurrence of */ closes the comment
statement for the entire line, meaning that the text wrong is interpreted as a
C statement or variable, and in this example, generates an error.
11
C Programming
Why use comments?
Best programmers comment as they write the code, not after the fact.
12
C Programming
Variables, Expressions, and Operators
13
C Programming
Declaring Variables
14
C Programming
Basic Format
int i,j,k;
float
length,height; char
midinit;
15
C Programming
Basic Data Types: INTEGER
int
int age;
16
C Programming
Basic Data Types: FLOAT
• FLOATING POINT: These are numbers which contain fractional parts, both
positive and negative, and can be written in scientific notation.
float
• Typical floating point values are 1.73 and 1.932e5 (1.932 x 105). An example
of declaring a float variable called x is
float x;
17
C Programming
Basic Data Types: DOUBLE
double
double voltage;
18
C Programming
Basic Data Types: CHAR
char
• Typical character values might be the letter A, the character 5, the symbol “,
etc. An example of declaring a character variable called letter is
char letter;
19
C Programming
Expressions and Statements
sum = x + y + z;
printf("Go
Buckeyes!");
20
C Programming
The Assignment Operator
i=0;
x=34.8;
sum=a+b;
slope=tan(rise/run)
; midinit='J';
j=j+3;
• When used in this manner, the equal sign should be read as “gets”. Note
that when assigning a character value the character should be enclosed in
single quotes.
21
C Programming
The Assignment Operator Evaluation
a=7;
• two things actually occur. The integer variable a gets the value of 7, and the
expression a=7 evaluates to 7.
22
C Programming
Initializing Variables
• C Variables may be initialized with a value when they are declared. Consider
the following declaration, which declares an integer variable count which
is initialized to 10.
• In general, the user should not assume that variables are initialized to some
default value “automatically” by the compiler. Programmers must ensure
that variables have proper values before they are used in expressions.
23
C Programming
Initializing Variables Example
24
C Programming
Arithmetic Operators
• In C, specialized operators have been set aside for the incrementing and
decrementing of integer variables. The increment and decrement operators
are
++ and -- respectively. These operators allow a form of shorthand in C:
++i; is equivalent to i=i+1;
--i; is equivalent to i=i-1;
26
C Programming
Advanced Assignment Operators
28
C Programming
Precedence & Associativity of Operators Examples
• The programmer can use parentheses to override the hierarchy and force
a desired order of evaluation. Expressions enclosed in parentheses are
evaluated first. For example:
(1 + 2) * (3 - 4)
3 * -1
-3
29
C Programming
The float and double Data Types
float
double
long double
• In general, the accuracy of the stored real values increases as you move
down the list.
30
C Programming
The char Data Type
• Variables of type char take up exactly one byte in memory and are used to
store printable and non-printable characters. The ASCII code is used to
associate each character with an integer (see next page). For example the
ASCII code associates the character ‘m’ with the integer 109. Internally, C
treats character variables as integers.
31
C Programming
Automatic Type Conversion with Assignment Operator
x=i;
i=x;
32
C Programming
Input and Output
• Basic Output
• printf Function
• Format Specifiers Table
• Common Special Characters for Cursor Control
• Basic Output Examples
• Basic Input
• Basic Input Example
33
C Programming
Basic Output
35
C Programming
Format Specifiers Table
• The following table show what format specifiers should be used with
what data types:
Specifier Type
%c character
%d decimal integer
%o octal integer (leading 0)
%x hexadecimal integer (leading
%u 0x) unsigned decimal integer
%ld
long int
%f
%lf floating point
%e double or long double
%s exponential floating point
character string 36
C Programming
Common Special Characters for Cursor Control
\n newline
\t tab
\r
carriage return
\f
\v form feed
\b vertical tab
\” backspace
\nnn Double quote (\ acts as an “escape”
mark)
octal character value
37
C Programming
Basic Output Examples
#include <stdio.h>
main() {
int pin;
printf("Please type in your PIN\n");
scanf("%d",&pin);
printf("Your access code is %d\n",pin);}
• The format identifier used for a specific C data type is the same as for
the printf statement, with one exception. If you are inputting values
for a double variable, use the %lf format identifier.
• White space is skipped over in the input stream (including carriage
return) except for character input. A blank is valid character input.
40
C Programming