0% found this document useful (0 votes)
6 views24 pages

C Programing - 3

c programming

Uploaded by

allyknockghash2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

C Programing - 3

c programming

Uploaded by

allyknockghash2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C PROGRAMING

LT DS NJINDISONI

LT DS NJINDISONI 12/02/2024 1
C Program Structure

 A C program basically consists of the following parts


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

LT DS NJINDISONI 12/02/2024 2
 Let us look at a simple code that would print the words "Hello World“

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

LT DS NJINDISONI 12/02/2024 3
 Let us look various parts of the above program
 The first line of the program #include is a preprocessor command, which
tells a C compiler to include stdio.h file before going to actual compilation
 The next line int main() is the main function where program execution
begins
 The next line /*...*/ will be ignored by the compiler and it has been put to
add additional comments in the program,So such lines are called
comments in the program
 The next line printf(...) is another function available in C which causes the
message "Hello, World!" to be displayed on the screen
 The next line return 0; terminates main()function and returns the value 0

LT DS NJINDISONI 12/02/2024 4
Compile & Execute C Program

 Let’s look at how to save the source code in a file, and how to
compile and run it
 Following are the simple steps:
 Open a text editor and add the above-mentioned code
 Save the file as hello.c
 Open a command prompt and go to the directory where you saved the file
 Type gcc hello.c and press enter to compile your code
 If there are no errors in your code, the command prompt will take you to
the next line and would generate [Link] executable file
 Now, type [Link] to execute your program
 You will be able to see "Hello World" printed on the screen
LT DS NJINDISONI 12/02/2024 5
It will appear as

$ gcc hello.c
$ ./[Link]
Hello, World!

LT DS NJINDISONI 12/02/2024 6
 Make sure that gcc compiler is in your path and that you are running
it in the directory containing source file hello.c

LT DS NJINDISONI 12/02/2024 7
C Basic Syntax

 This chapter will give details about all the basic syntax about C
programming language including tokens, keywords, identifiers, etc
 You have seen a basic structure of C program, so it will be easy to
understand other basic building blocks of the C programming
language

LT DS NJINDISONI 12/02/2024 8
Tokens in C

 A C program consists of various tokens and a token is either a


keyword, an identifier, a constant, a string literal, or a symbol
 For example, the following C statement consists of five tokens
printf("Hello, World! \n");
 The individual tokens are
printf
(
"Hello, World! \n"
)
;

LT DS NJINDISONI 12/02/2024 9
Semicolons ;

 In C program, the semicolon is a statement terminator


 That is, each individual statement must be ended with a semicolon
 It indicates the end of one logical entity
 For example, following are two different statements
 printf("Hello, World! \n");
 return 0;

LT DS NJINDISONI 12/02/2024 10
Comments

 Comments are like helping text in your C program and they are
ignored by the compiler
 They start with /* and terminates with the characters */ as shown
below:
 /* my first program in C */ (multiline comments)
 Or just two slashes // as shown below:
 // my first program in C (single line comment)
 You cannot have comments within comments and they do not occur
within a string or character literals

LT DS NJINDISONI 12/02/2024 11
Identifiers

 A C identifier is a name used to identify a variable, function, or any


other user-defined item
 An identifier starts with a letter A to Z or a to z or an underscore _
followed by zero or more letters, underscores, and digits (0 to 9)
 C does not allow punctuation characters such as @, $, and % within
identifiers
 C is a case sensitive programming language,thus Manpower and
manpower are two different identifiers in C
 Here are some examples of acceptable identifiers
 Mohd, zara, abc, move_name, a_123, myname50 _temp, j, a23b9, retVal
 Example of invalid identifiers include: 5bc,int,rec#,avg no
LT DS NJINDISONI 12/02/2024 12
NOTE: RULE FOR NAMING
IDENTIFIER
 Name should consist of only alphabets, digits and underscore sign
 First character should be an alphabet or underscore
 The name should not be a keyword
 Since C is case sensitive, uppercase and lowercase letters are
considered different eg code, Code and CODE are three different
identifiers
 An identifier may be arbitrarily long, some implementation of C
recognize only the first eight characters, ANSI Standard compilers
recognizes 31 characters
 Identifiers are given meaningful names

LT DS NJINDISONI 12/02/2024 13
Keywords

 The following list shows the reserved words in C


 These reserved words may not be used as constant or variable or any
other identifier names
 Auto, else, Long, switch, break, enum
 Register, typedef
 Case, extern, return, union
 Char, float, short unsigned
 Const, for, signed, void, continue
 Goto, sizeof
 Volatile, default, if, static, while, do, int
 Struct, _packed, double
LT DS NJINDISONI 12/02/2024 14
Whitespace in C

 A line containing only whitespace, possibly with a comment, is known as a blank


line, and a C compiler totally ignores it
 Whitespace is the term used in C to describe blanks, tabs, newline characters
and comments
 Whitespace separates one part of a statement from another and enables the
compiler to identify where one element in a statement, such as int, ends and
the next element begins
 Therefore, in the following statement:
 int age; There must be at least one whitespace character (usually a space) between
int and age for the compiler to be able to distinguish them
 On the other hand, in the following statement:
 fruit = apples + oranges; // get the total fruit No whitespace characters are necessary
between fruit and =, or between = and apples, although you are free to include some
if you wish for readability purpose
LT DS NJINDISONI 12/02/2024 15
C Data Types

 In the C programming language, data types refer to an extensive


system used for declaring variables or functions of different types
 The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted
 The types in C can be classified as follows
 Basic Types: They are arithmetic types and consists of the two types:
 integer types and
 Floating point types
 Enumerated types: They are again arithmetic types and they are used to
define variables that can only be assigned certain discrete integer values
throughout the program
 The type void: The type specifier void indicates that no value is available
LT DS NJINDISONI 12/02/2024 16
 Derived types: They include
 Pointer types
 Array types
 Structure types
 Union types and
 Function types
 The array types and structure types are referred to collectively as the
aggregate types
 The type of a function specifies the type of the function's return value
 We will see basic types in the following section, whereas, other types will
be covered in the upcoming chapters

LT DS NJINDISONI 12/02/2024 17
Integer Types
 Following table gives you details about standard integer types with its
storage sizes and value ranges
type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
Unsigned char 1 byte 0 to 255
Signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -
2,147,483,648 to 2,147,483,647
Unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
Unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
Unsigned long 4 bytes 0 to 4,294,967,295

LT DS NJINDISONI 12/02/2024 18
 To get the exact size of a type or a variable on a particular platform, you can use the
sizeof operator
 The expressions sizeof(type) yields the storage size of the object or type in bytes
 Following is an example to get the size of int type on any machine
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}
When you compile and execute the program, it produces the following result on
Linux/windows: Storage size for int : 4
LT DS NJINDISONI 12/02/2024 19
Floating-Point Types

 Following table gives you details about standard floating-point types


with storage sizes and value ranges and their precision

Type Storage Value range Precision


size
float 4 byte 1.2E-38 to 3.4E+38 6 decimal
places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal
places
long double 10 byte 3.4E-4932 to 19 decimal
1.1E+4932 places

LT DS NJINDISONI 12/02/2024 20
 The header file float.h defines macros that allow you to use these values and
other details about the binary representation of real numbers in your programs
 Following example will print storage space taken by a float type and its range
values
#include <stdio.h>
#include <float.h>
int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG ); return 0;
}
LT DS NJINDISONI 12/02/2024 21
 When you compile and execute the above program, it produces the
following result on Linux/windows:
 Storage size for float : 4
 Minimum float positive value: 1.175494E-38
 Maximum float positive value: 3.402823E+38
 Precision value: 6

LT DS NJINDISONI 12/02/2024 22
The void Type
 The void type specifies that no value is available. It is used in three
kinds of situations

S.N Types and Description


.
1. Function returns as void There are various functions in C which do
not return value or you can say they return void. A function with no
return value has the return type as void. For example, void exit (int
status);
2. Function arguments as void There are various functions in C which
do not accept any parameter. A function with no parameter can
accept as a void. For example, int rand(void);
3. Pointers to void A pointer of type void * represents the address of
an object, but not its type. For example, a memory allocation
function void *malloc( size_t size ); returns a pointer to void which
can be casted to any data type.
LT DS NJINDISONI 12/02/2024 23
 The void type may not be understood to you at this point, so let us
proceed and we will cover these concepts in the upcoming chapters.

LT DS NJINDISONI 12/02/2024 24

You might also like