Lecture 2 - w1 - HA
Lecture 2 - w1 - HA
COMPUTER
PROGRAMMING
WEEK 1: LECTURE # 2
#include <stdio.h>
Syntax
void main ( ) • printf is a statement.
• A statement in C is terminated by a semi
{ colon ( ; ) .
printf(“Hello World”);
}
BASIC STRUCTURE OF C PROGRAMS
#include <stdio.h>
Delimiters
void main ( ) • Opening and Closing brace or Curly
brackets marks the start and end of the
{ code
• The start and end of a block ( such as
printf(“Hello World”);
loop, if else statement, switch statement)
} is also identified by the start and end of
curly brackets.
BASIC STRUCTURE OF C PROGRAMS
#include <stdio.h>
Syntax
void main ( ) • C pays no attention to the “whitespace”
characters encountered in a program like
{ space, carriage return(newline) and tab
• You can put as many whitespaces in your
printf(“Hello World”); program as you like; they will be invisible to
the compiler
}
Basic Structure of C programs
}
COMMENTS
• Any part of your program that starts with /* and ends with */ is called a comment.
• Look at the first line of code in the preceding example:
• /* Program 1.3 Another Simple C Program - Displaying a Quotation */
• This isn’t actually part of the program code, in that it isn’t telling the computer to do anything.
• It’s simply a comment, and it’s there to remind you, or someone else reading your code, what
the program does.
• Its good to have habit of documenting your programs, using comments as you go along.
• Your programs will, of course, work without comments, but when you write longer programs you
may not remember what they do or how they work.
• Program Output:
• Beware the Ides of March!
UNDERSTANDING THE STRUCTURE
PREPROCESSING DIRECTIVES
• Every C program consists of one or more functions, and every C program must contain a function called
main()—the reason being that a program will always start execution from the beginning of this function.
• So imagine that you’ve created, compiled, and linked a file called progname.exe.
• When you execute this program, the operating system calls the function main() for the program.
• int main(void)
• This defines the start of the function main().
• Notice that there is no semicolon at the end of the line.
• The first line identifying this as the function main() has the word int at the beginning.
• What appears here defines the type of value to be returned by the function, and the word int signifies that the
function main() returns an integer value.
The main() Function
• You end execution of the main() function and specify the value to be returned in the statement:
• return 0; /* This returns control to the operating system */
• This is a return statement that ends execution of the main() function and returns that value 0 to the operating
system.
• You return a zero value from main() to indicate that the program terminated normally; a nonzero value would
indicate an abnormal return.
• The parentheses that immediately follow the name of the function, main, enclose a definition of what
information is to be transferred to main() when it starts executing.
• In this example, however, you can see that there’s the word void between the parentheses, and this
signifies that no data can be transferred to main().
BASIC STRUCTURE OF C PROGRAMS
• A function prototype provides the C compiler with the name and arguments of the functions contained in a program.
• It must appear before the function is used.
• Syntax:
• return_type function_name(arg_name1,…..);
• A function definition is actual function. An independent, self contained section of code that is written to perform a certain
task.
• Syntax:
• return_type function_name( arg_name1,……)
• {
• /*statements*/
• }
• Every function has a name, and the code in each function is executed by including that function’s name in a program statement.
• This execution is known as calling a function.
FUNCTION PROTOTYPE & DEFINITION
• A function prototype provides the C compiler with the name and arguments of the functions contained in a program.
• It must appear before the function is used.
• Syntax:
• return_type function_name(arg_name1,…..);
• A function definition is actual function. An independent, self contained section of code that is written to perform a certain
task.
• Syntax:
• return_type function_name( arg_name1,……)
• {
• /*statements*/
• }
• Every function has a name, and the code in each function is executed by including that function’s name in a program statement.
• This execution is known as calling a function.
Keywords & Identifiers
• Keywords are the reserved words used in programming. Each keywords has fixed meaning and that
cannot be changed by user. They are also known as reserved words.
• As, C programming is case sensitive, all keywords must be written in lowercase. Here is the list of all keywords
predefined by ASCII C.
• Identifiers
• In C programming, identifiers are names given to C entities, such as
variables, functions, structures etc.
• Identifier are created to give unique name to C entities to identify it during
the execution of program.
VARIABLES & DATA TYPES
VARIABLES
• Data types are the keywords, which are used for assigning a type to a variable.
• Data types in C
• Fundamental Data Types
• Integer types
• Floating Type
• Character types
• Double
• Void
• Unsigned integer types are used when you are dealing with values that cannot be
negative.
FLOATING-POINT VALUES
• When you declare a variable, you instruct the compiler to set aside storage space for the variable.
• However, the value stored in that variable isnt defined.
• It might be zero, or it might be some random “garbage” value.
• Examples
• int num=2;
• int char=‘d’,
• float length=34.5677;
• By initializing a variable we provide:
• Declaration of variable
• Definition of a variable
• Initial value loaded in the variable
CONSTANT
• A constant starting with any digit other than 0 is interpreted as a decimal integer.
• A constant starting with the digit 0 is interpreted as an octal integer (the base 8). e.g. 015
• A constant starting with 0x or 0X is interpreted as a hexadecimal constant (bas 16). e.g. 0x1ae
• A conversion specifier consists of the percent sign followed by a character. e.g. %d is used for integers.
• printf(“\n The value of y is %d”, y);
/* DEMONSTRATION USING PRINTF() TO DISPLAY
NUMERICAL VALUE */
• #include <stdio.h>
• int a=2, b=10, c=50;
• float f=1.05, g= 25.5, h= -0.1;
• int main ()
• {
• printf(“\nDecimal values without tabs: %d %d %d”, a, b, c);
• printf(“\nDecimal values with tabs: \t%d \t%d \t%d”, a, b, c);
• printf(“\nThree floats on 1 line: \t%f\t%f\t%f”, f, g, h);
• printf(“\nThree floats on 3 line: \n\t%f\n\t%f\n\t%f”, f, g, h);
• printf(“\nThe rate is %f%%”,f);
• printf(“\nThe result of %f/%f=%f\n”, g, f, g/f);
• return 0;
• }
/* DEMONSTRATION USING PRINTF() TO DISPLAY
NUMERICAL VALUE: OUTPUT */
• Decimal values without tabs: 2 10 50
• Decimal values with tabs: 2 10 50
• Three floats on 1 line: 1.05000 25.00000 -0.100000
• Three floats on 3 lines:
• 1.050000
• 25.50000
• -0.100000
• The rate is 1.05000%
• The result of 25.500000/1.050000= 24.285715
FORMAT SPECIFIERS
• Tells the compiler where to put the value in string and what format to use for printing
the value
• The scanf() function is another function that requires the <stdio.h> header file to be
included.
• This function reads data from the keyboard according to a specified format and assign
the input data to one or more program variables.
• Syntax of scanf function is
scanf (“format string”, argument list);
• scanf(“%d”, &x); /*Takes int value of x from keyboard*/
• The & symbol is C’s address of operator.
SCANF() FUNCTION
int a, b,
float c;
char d;
scanf(“ %d %d %f %c” , &a, &b, &c, &d);