0% found this document useful (0 votes)
14 views13 pages

Lecture 2

This document is a lecture on the basics of C programming. It introduces the typical parts of a C program, including preprocessor commands, functions, variables, statements, and expressions. It then presents the code for a simple "Hello World" C program and explains each part, including the #include statement, main function, printf statement, and return value. It also shows some variations of the basic "Hello World" program.

Uploaded by

spondon384
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)
14 views13 pages

Lecture 2

This document is a lecture on the basics of C programming. It introduces the typical parts of a C program, including preprocessor commands, functions, variables, statements, and expressions. It then presents the code for a simple "Hello World" C program and explains each part, including the #include statement, main function, printf statement, and return value. It also shows some variations of the basic "Hello World" program.

Uploaded by

spondon384
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

EEE 105

Computer Programming

Instructor: Dr. Tishna Sabrina


Fall 2023

Lecture 2
Introduction to C
Basic C program
A C program basically consists of the following
parts −

• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments

1/16/2024 Computer Programming : Lecture 2 2


Our first C program
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 3


The first line of the program
The first line of the program is a pre-processor command, which tells a C
compiler to include stdio.h file before going to actual compilation.

#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 4


The second line of the program

The keyword int indicates that main returns an integer value

main is the name of the primary (or main) function. All


ANSI C programs must have a main function named main
The () indicates that main is the name of a function. All
function references must be followed with ()
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 5


Our first C program

{ } enclose a "block". A block is zero or more C statements.


Note that code inside a block is typically indented for
readability—knowing what code is inside the current block is
quite useful.

#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 6


Our first C program
printf() is a "built-in" function (which is actually defined in
stdio.h).

"Hello World!" is the string to


print.
#include <stdio.h> More formally, this is called the
int main() control string or control specifier.
{
printf("Hello World!\n");
return 0;
}

Every executable statement must end with a ";"

1/16/2024 Computer Programming : Lecture 2 7


Our first C program

The \n is an escape character used by the printf


function; inserting this character in the control string causes
a “newline” to be printed—it’s as if you hit the “Enter” key

#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 8


Our first C program
The int tells the compiler our main() program will return
an integer to the operating system; the return tells what
integer value to return. This keyword could be void,
indicating that the program returns nothing to the OS.

#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 9


Variations #1 of first program

#include <stdio.h>
int main()
{
printf("Hello");
printf("there");
printf("World!");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 10


Variations #2 of first program

#include <stdio.h>
int main()
{
printf("Hello\n");
printf("there\n");
printf("World!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 11


Variations #3 of first program

#include <stdio.h>
int main()
{
printf("Hello\nthere\nWorld!\n");
return 0;
}

1/16/2024 Computer Programming : Lecture 2 12


Variations #4 of first program

#include <stdio.h>
int main(){printf ("Hello\nthere\nWorld!\n");return 0;}

Note while this is syntactically correct, it leaves much to be desired in terms of


readability.

1/16/2024 Computer Programming : Lecture 2 13

You might also like