C Programing - 3
C Programing - 3
LT DS NJINDISONI
LT DS NJINDISONI 12/02/2024 1
C Program Structure
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 a.out executable file
Now, type a.out 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
$ ./a.out
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
LT DS NJINDISONI 12/02/2024 9
Semicolons ;
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
LT DS NJINDISONI 12/02/2024 13
Keywords
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
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
LT DS NJINDISONI 12/02/2024 24