0% found this document useful (0 votes)
99 views

2 Basic Concepts of C Language

This document provides an overview of basic concepts in the C programming language for ELC 111: Computer Programming I. It covers topics such as comments, preprocessor directives, functions, output statements, escape sequences, and more. The document uses examples of simple C programs and errors to demonstrate concepts like variables, data types, and identifiers.

Uploaded by

Áhméd Yasser
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)
99 views

2 Basic Concepts of C Language

This document provides an overview of basic concepts in the C programming language for ELC 111: Computer Programming I. It covers topics such as comments, preprocessor directives, functions, output statements, escape sequences, and more. The document uses examples of simple C programs and errors to demonstrate concepts like variables, data types, and identifiers.

Uploaded by

Áhméd Yasser
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/ 25

ELC 111 : Computer Programming I

Basic Concepts of C language

Lecturer: Dr. Reham Samir


References
 Paul Deitel, Harvey Deitel, “C How to Program with
an introduction to C++ ”, Pearson Education, eighth
edition, 2016.

 Noel Kalicharan, “Learn to Program with C ”, Apress,


2015.
A simple C program
 Our first program prints a line of text:

 The output from this program will be:


A simple C program
 Comments

These lines are


called comments

 Comments are inserted to document programs and improve program


readability.
 Comments also help other people read and understand your program.
 Comments do not cause the computer to perform any action when the
program is run (they’re ignored by the C compiler).
 There’s another form of comment that you can use when you need to spread
a comment over several lines (multi-line comment), for example:
/* This comment is
over two lines. */
A simple C program
 #include Preprocessor Directive

 Lines beginning with # are processed by the preprocessor before


compilation.
 It tells the preprocessor to include the contents of the standard
input/output header (<stdio.h>) in the program.
 This header contains information used by the compiler when
compiling calls to standard input/output library functions such as
printf.
A simple C program
 The main Function

 The computer knows where to start executing a program by looking


for a function called main then it starts executing the instructions it
finds there.
 C program contains one or more functions, one of which must be
main to tell it where to start executing.
A simple C program
 The main Function
 In general, any function has four parts: return type, name, parameter
list, and function body.
 A return type: here int (meaning “integer”), which specifies what
kind of result, if any, the function will return to whoever asked for it
to be executed.
 A name: here main.
 A parameter list enclosed in parentheses: in this case, the parameter
list is void which means that main does not receive any information.
 A function body enclosed in a set of “curly braces,” { }: which lists
the actions (called statements) that the function is to perform.
A simple C program
 An Output Statement

 printf instructs the computer to perform an action, namely to print on


the screen the string of characters marked by the quotation marks.
 The entire line, including the printf function, its argument within the
parentheses and the semicolon (;), is called a statement.
 Every statement must end with a semicolon.
A simple C program
 Escape Sequences
 The backslash (\) is called an escape character. It indicates that printf
is supposed to do something out of the ordinary.
 When encountering a backslash in a string, the compiler looks ahead
at the next character and combines it with the backslash to form an
escape sequence.
 The escape sequence \n means newline, the newline causes the cursor
to position to the beginning of the next line on the screen.
The Linker and Executables
 The Linker and Executables
 Standard library functions like printf and scanf are not part of the C
programming language.
 When the linker runs, it locates the library functions and inserts the
proper calls to these library functions in the object program.
 Now the object program is complete and ready to be executed.
 Example: (What is the error in this program)
// A first program in C.
// function main begins program execution
int main( void )
{
printf( "Welcome to C!\n" );
} // end function main
 Sol:
 The compiler complains as we didn’t include something to tell the
compiler what printf was, so the compiler complains. To correct that,
let’s add a header file.
 Example: (What is the error in this program)
// A first program in C.
#include <stio.h>
// function main begins program execution
int main( void )
{
printf( "Welcome to C!\n" );
} // end function main
 Sol:
 The compiler complains as we misspelled stdio.h.
 Example: (What is the error in this program)
// A first program in C.
#include <stdio.h>
// function main begins program execution
int main( void )
{
printf( "Welcome to C!\n );
} // end function main
 Sol:
 The compiler complains as we didn’t terminate the string with a ".
 Example: (What is the error in this program)
// A first program in C.
#include <stdio.h>
// function main begins program execution
integer main( void )
{
printf( "Welcome to C!\n" );
} // end function main
 Sol:
 The compiler complains as the abbreviation int is used in C rather
than the word integer.
 Example: (What is the error in this program)
// A first program in C.
#include <stdio.h>
// function main begins program execution
int main( void )
{
printf( 'Welcome to C!\n' );
} // end function main
 Sol:
 The compiler complains as we used single quotes rather than double
quotes to delimit the string.
 Example: (What is the error in this program)
// A first program in C.
#include <stdio.h>
// function main begins program execution
int main( void )
{
printf( "Welcome to C!\n" )
} // end function main
 Sol:
 The compiler complains as we forgot to terminate the output
statement with a semicolon.
 Many C statements are terminated by a semicolon (;). The compiler
needs those semicolons to know where one statement ends and the
next begins.
Using printf
 Example: What is the output from the following program?

 Sol:
Using printf
 Example: What is the output from the following program?

 Sol:
Using printf
 Example: Write printf statement(s) to print the following:

 Sol:
printf("Where the mind is without fear\n\n");
printf("And the head is held high\n");
or
printf("Where the mind is without fear\n");
printf("\nAnd the head is held high\n");
or
printf("Where the mind is without fear\n");
printf("\n");
printf("And the head is held high\n");
Using printf
 Example: Write printf statement(s) to print the following:

 Sol:
printf("Use \" to begin and end a string\n");
 Prompting Messages

 This statement displays the literal "Enter first integer" and positions
the cursor to the beginning of the next line.
 This message is called a prompt because it tells the user to take a
specific action.
Variables
 All variables must be defined with a name and a data type before
they can be used in a program.
 The C standard allows you to place each variable definition
anywhere in main before that variable’s first use in the code.
 For example:

 Here we define two variables; whose names (locations in memory


where values can be stored for use by a program) are integer1 and
integer2.
 These variables are of type int, which means that they’ll hold integer
values.
Variables
 We can combine the definitions of integer1 and integer2 variables into a
single definition as follows:

 We can assign a value to a variable in its definition—this is known


as initializing the variable.
 For example:

 int sum = integer1 + integer2; // assign total to sum


 int x = 3;
identifiers
 The C programmer needs to make up names for things such as
variables, function names, and symbolic constants.
 There are a few simple rules to follow in naming an identifier:
 An identifier is a series of characters consisting of letters, digits and
underscores (_) that does not begin with a digit.
 Avoid starting identifiers with the underscore character (_).
 C is case sensitive (uppercase and lowercase letters are different in
C), so a1 and A1 are different identifiers.
 Choosing meaningful identifier names helps make a program self-
documenting.
 The length of an identifier cannot exceed 63 characters.
 you cannot use reserved keywords as identifiers.
identifiers
 Reserved Words
 The C language uses a number of keywords such as int, char, and
while.
 A keyword has a special meaning in the context of a C program and
can be used for that purpose only.
 All keywords are written in lowercase letters only.
 Thus int is a keyword but Int and INT are not.

 Keywords are reserved, that is, you cannot use them as your
identifiers.
 Examples of invalid Identifiers:
identifiers
 Examples of valid Identifiers:

You might also like