how sizeof works in c code example

Example 1: terminal size in c

#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

int main (int argc, char **argv)
{
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    printf ("lines %d\n", w.ws_row);
    printf ("columns %d\n", w.ws_col);
    return 0;  // make sure your main returns int
}

Example 2: size of file in c

// C program to find the size of file 
#include <stdio.h> 

long int findSize(char file_name[]) 
{ 
	// opening the file in read mode 
	FILE* fp = fopen(file_name, "r"); 

	// checking if the file exist or not 
	if (fp == NULL) { 
		printf("File Not Found!\n"); 
		return -1; 
	} 

	fseek(fp, 0L, SEEK_END); 

	// calculating the size of the file 
	long int res = ftell(fp); 

	// closing the file 
	fclose(fp); 

	return res; 
} 

// Driver code 
int main() 
{ 
	char file_name[] = { "a.txt" }; 
	long int res = findSize(file_name); 
	if (res != -1) 
		printf("Size of the file is %ld bytes \n", res); 
	return 0; 
}

Tags:

C Example